我正在尝试登录示例。在控制器类中,我使用addError方法来显示错误。当我输入无效条目时,它会转到错误页面,但不会显示该消息。访问页面正常工作。
LoginController.java
@Controller
public class LoginController {
@Autowired
private LoginService loginService;
@RequestMapping("login.html")
public String toLogin(Model model) {
Login login = new Login();
model.addAttribute("login",login);
return "login";
}
@RequestMapping(value ="login.html", method = RequestMethod.POST)
public ModelAndView doLogin(@Valid @ModelAttribute ("login") Login login,
BindingResult bindingresult ) {
ModelAndView view = new ModelAndView("login");
if(!bindingresult.hasErrors()){
if(!loginService.authenticateUser(login)){
bindingresult.addError(new ObjectError("invalid", "Invalid Credentials"));
return new ModelAndView("error");
}
else{
view.setViewName("success");
}
}
return view;
}
}
error.jsp文件
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>LoginExample2-Error</title>
</head>
<body>
<table>
<tr>
<td><a href="login.html">Retry</a></td>
</tr>
</table>
</body>
</html>