我有下一个问题 - JSP(或Spring)在登录/注册页面上显示相同的错误消息两次甚至更多次。 为什么会这样?我还没有找到同样的问题。以前谢谢。
我还无法发布图片,所以我在这里发布pictures
我的 Login.jsp
<sf:form name="login"
method="POST"
action="${app}/login.do"
modelAttribute="loginForm"
enctype="application/x-www-form-urlencoded">
<label for="login">Login:</label>
<br><input name="login" id="login" type="text" value=""/> <br>
<sf:errors path="login" cssClass="error"/>
<br>
<br><label for="password">Password:</label>
<br><input name="password" id="password" type="password" value=""/> <br>
<sf:errors path="password" cssClass="error"/><br>
<br> <input type="submit" name="submit" value="Login"/>
</sf:form>
LoginController类
@Controller
public class LoginController {
//Log4j
private static final Logger logger = Logger.getLogger(LoginController.class);
public LoginController() {
}
@Qualifier("loginValidator")
@Autowired
private LoginValidator loginValidator;
public LoginController(LoginValidator loginValidator) {
this.loginValidator = loginValidator;
}
@InitBinder("loginForm")
private void initBinder(WebDataBinder binder) {
binder.setValidator(loginValidator);
}
@Qualifier("userServiceImpl")
@Autowired
private UserService userService;
@Qualifier("roleServiceImpl")
@Autowired
private RoleService roleService;
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView loginPage() {
return new ModelAndView("login", "loginForm", new LoginForm());
}
@RequestMapping(value = "/login.do", method = RequestMethod.POST)
public ModelAndView login(@ModelAttribute("loginForm")
@Valid LoginForm loginForm,
BindingResult bindingResult,
HttpServletRequest request,
HttpServletResponse response) {
BasicConfigurator.configure();
logger.trace("check authentication!");
String login = request.getParameter("login");
String password = request.getParameter("password");
new ModelAndView("login", "loginForm", loginForm);
loginValidator.validate(loginForm, bindingResult);
try {
if (bindingResult.hasErrors()) {
return new ModelAndView("login", "loginForm", loginForm);
} else {
// If the user details is validated then redirecting the user to
welcome page,
// else returning the error message on login page.
User user = userService.authorization(login, password);
if (user != null) {
request.getSession().setAttribute("user", user);
//Creating a redirection view to welcome page.
RedirectView redirectView = new RedirectView("welcome", true);
return new ModelAndView(redirectView);
} else {
bindingResult.addError(new ObjectError("Invalid", "Invalid
credentials. " + "Login or Password is incorrect."));
return new ModelAndView("login", "loginForm", loginForm);
}
}
} catch (Exception e) {
System.out.println("Exception in LoginController " + e.getMessage());
e.printStackTrace();
return new ModelAndView("login", "loginForm", loginForm);
}
}
LoginForm类
public class LoginForm {
@NotBlank
private String login;
@NotBlank
private String password;
...Getter and Setters
messages.properties文件
login.required=Login is required!!
password.required=Password is required!!
password.again=Passwords do not match
name.required=Name is required
email.required=Email is required
email.correct=Not correct recording format, example XYZ@gmail.com
email.again=Emails do not match
答案 0 :(得分:1)
就像@ M.Deinum提到的那样,你已经两次验证了表格。只需删除此行:
loginValidator.validate(loginForm, bindingResult);
一切都应该没问题。因为你有@Valid,所以你使用JSR-303来验证你的bean。您不必通过手动调用validate()
方法再次验证它。