尝试从表单中获取数据时出现Null指针异常,我填写了表单并获得了名字。但是会抛出空指针异常。我正在使用play2.6
controllers / Register.java:
package controllers;
import models.SignUp;
import play.data.Form;
import play.data.FormFactory;
import play.mvc.Controller;
import play.mvc.Result;
import javax.inject.Inject;
public class Register extends Controller{
@Inject
FormFactory formFactory;
public Result index(){
Form<SignUp> signupform = formFactory.form(SignUp.class);
return ok(views.html.index.render(signupform));
}
public Result submit(){
Form<SignUp> signupform =formFactory.form(SignUp.class).bindFromRequest();
if(signupform.hasErrors()){
flash("danger","Please Correct the Form Below");
//send bad request error
}
SignUp details = signupform.get();
return ok("Hello"+ details.firstName);
}
}
模型/注册:
package models;
import play.data.validation.Constraints;
import javax.validation.constraints.Max;
public class SignUp {
public String firstName;
public String lastName;
public String emailId;
public String password;
}
@ views / index
@(signupform : Form[SignUp])
@import helper._
@main("SignUp") {
<h1>SignUp Form</h1>
@helper.form(action = helper.CSRF(routes.Register.submit())){
@helper.inputText(signupform("firsName"),'class -> "form-control")
@helper.inputText(signupform("lastName"),'class -> "form-control")
@helper.inputText(signupform("emailId"),'class -> "form-control")
@helper.inputPassword(signupform("password"),'class -> "form-control")
<button type="submit">Submit<button>
}
}