Ger*_*son 2 java validation spring spring-mvc
我正在尝试向我的 Spring MVC 应用程序添加验证。在尝试设置验证之前,我一直使用 ModelAndView 来提供 jsp 页面,但没有显示错误消息。
模型
@Entity
@Table(name = "employee")
public class Employee {
@Id
@NotEmpty(message = "Please enter your email addresss.")
@Email
private String email;
@NotEmpty
@Pattern(regexp="[a-zA-Z0-9]")
private String password;
@NotEmpty
private String firstName;
@NotEmpty
private String lastName;
@NotEmpty
private String phoneNum;
private boolean assigned;
public Employee() {
}
// getters and setters
}
Run Code Online (Sandbox Code Playgroud)
控制器
@RestController
@RequestMapping("/employee")
public class EmployeeController {
private static final Logger LOGGER = LogManager
.getLogger(EmployeeController.class.getName());
@Autowired
private EmployeeServiceImpl employeeService;
@RequestMapping(value = "/new", method = RequestMethod.GET)
public ModelAndView getRegisterView(Model m) {
return new ModelAndView("addEmployee", "employeeForm", new Employee());
}
@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView postUser(@Valid Employee employee, BindingResult result) {
ModelAndView model;
if (result.hasErrors()) {
model = new ModelAndView("addEmployee", "employeeForm", employee);
} else {
employeeService.addEmployee(employee);
model = new ModelAndView();
model.setViewName("displayEmployee");
model.addObject("employees", employeeService.getEmployees());
}
return model;
}
}
Run Code Online (Sandbox Code Playgroud)
形式
<form:form class="form-horizontal" role="form" method="POST"
action="register" commandName="employeeForm" modelattribute="employee">
<div class="form-group">
<form:label path="firstName"
class="control-label col-lg-2 col-lg-offset-2 col-md-2 col-md-offset-2 col-sm-12"
for="fname">First Name:</form:label>
<div class="col-lg-6 col-md-6 col-sm-12">
<form:input path="firstName" class="form-control" id="fname"
placeholder="First Name" />
<form:errors path="firstName" />
</div>
</div>
<div class="form-group">
<form:label path="lastName"
class="control-label col-lg-2 col-lg-offset-2 col-md-2 col-md-offset-2 col-sm-12"
for="lname">Last Name:</form:label>
<div class="col-lg-6 col-md-6 col-sm-12">
<form:input path="lastName" class="form-control" id="lname"
placeholder="Last Name" />
<form:errors path="lastName" />
</div>
</div>
<div class="form-group">
<form:label path="email"
class="control-label col-lg-2 col-lg-offset-2 col-md-2 col-md-offset-2 col-sm-12"
for="email">Email:</form:label>
<div class="col-lg-6 col-md-6 col-sm-12">
<form:input path="email" type="email" class="form-control"
id="email" placeholder="Email" />
<form:errors path="email" cssClass="error" />
</div>
</div>
<div class="form-group">
<form:label path="phoneNum"
class="control-label col-lg-2 col-lg-offset-2 col-md-2 col-md-offset-2 col-sm-12"
for="phoneNum">Phone Number:</form:label>
<div class="col-lg-6 col-md-6 col-sm-12">
<form:input path="phoneNum" class="form-control" id="phoneNum"
placeholder="Phone Number" />
<form:errors path="phoneNum" />
</div>
</div>
<div class="form-group">
<form:label path="password"
class="control-label col-lg-2 col-lg-offset-2 col-md-2 col-md-offset-2 col-sm-12"
for="pwd">Password:</form:label>
<div class="col-lg-6 col-md-6 col-sm-12 controls">
<form:input path="password" type="password" class="form-control"
id="pwd" placeholder="Password" />
<form:errors path="password" />
</div>
</div>
<div class="form-group form-actions">
<div
class="col-lg-offset-4 col-lg-1 col-md-offset-4 col-md-1 col-sm-1">
<button type="submit" class="btn btn-primary">Register</button>
</div>
<div class="col-lg-1 col-md-1 col-sm-1">
<button type="reset" class="btn btn-primary">Clear</button>
</div>
</div>
<form:hidden path="assigned" value="false" />
</form:form>
Run Code Online (Sandbox Code Playgroud)
model = new ModelAndView("addEmployee", "employeeForm", employee);
Run Code Online (Sandbox Code Playgroud)
您正在自己的代码中销毁模型,因此当返回页面时,基本上什么也没有留下。返回ModelAndViewSpring MVC 时假定您已经准备并添加了自己呈现视图所需的一切。
而是添加@ModelAttribute("employeeForm")到@Valid注释旁边的方法参数中,并使用 中已经存在的模型BindingResult来构造一个ModelAndView.
@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView postUser(@Valid @ModelAttribute("employeeForm" Employee employee, BindingResult result) {
ModelAndView model;
if (result.hasErrors()) {
model = new ModelAndView("addEmployee", result.getModel());
Run Code Online (Sandbox Code Playgroud)
尽管这会起作用,为什么不简单地返回 aString作为视图的名称并Model在需要时做一些准备。
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String postUser(@Valid @ModelAttribute("employeeForm") Employee employee, BindingResult result, Model model) {
if (result.hasErrors()) {
return "addEmployee";
} else {
employeeService.addEmployee(employee);
model.addObject("employees", employeeService.getEmployees());
return "displayEmployee";
}
}
Run Code Online (Sandbox Code Playgroud)
我甚至认为,填充Model的displayEmployee页面不属于这里,但在一个单独的方法准备该页面的模型。