我已经建立了一个登录表单。我已经按照教程阅读了有关Spring MVC的验证。
该表单为空白并且不传递任何值,但是不显示任何错误消息。
这是我为控制器编写的代码:
@Controller
public class LoginController {
@RequestMapping("/login")
public ModelAndView login() {
return new ModelAndView("login", "user", new UserModel());
}
@RequestMapping("/submitLogin")
public ModelAndView submitLogin(@Valid UserModel user, BindingResult result) {
ModelAndView model = new ModelAndView();
model.addObject("user", user);
model.setViewName(result.hasErrors() ? "login" : "index");
return model;
}
}
这是我的模特
@Data
public class UserModel {
@NotBlank(message = "The field login name is required")
private String login;
@NotBlank(message = "The field password is required")
private String password;
}
这是我的表格:
<mvc:form modelAttribute="user" class="col s12" action="/submitLogin" method="POST">
<div class="container login-form">
<div class="row">
<div class="input-field col s12">
<mvc:input path="login" id="userName" type="text" class="validate" />
<mvc:label path="login" for="userName">Login name</mvc:label>
</div>
</div>
<div class="row">
<mvc:errors path="login" cssStyle="color: #ff0000;"/>
</div>
<div class="row">
<div class="input-field col s12">
<mvc:input path="password" id="password" type="password" class="validate" />
<mvc:label path="password" for="password">Password</mvc:label>
</div>
</div>
<div class="row">
<mvc:errors path="password" cssStyle="color: #ff0000;"/>
</div>
<div class="row">
<input type="submit" class="btn waves-effect waves-light pulse green" value="Entrar"/>
</div>
</div>
</mvc:form>
我是否错过了Spring Boot或注解的任何配置?