【问题标题】:Spring or JSP shows several same error messages instead of oneSpring 或 JSP 显示多个相同的错误消息而不是一个
【发布时间】:2014-06-03 13:30:08
【问题描述】:

我有下一个问题 - 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

【问题讨论】:

  • 您还要验证两次。一次自动(由于initBinder),一次通过调用 validate 方法在您的代码中。

标签: java spring jsp spring-mvc messages


【解决方案1】:

就像@M.Deinum 提到的那样,您已经验证了表单两次。只需删除此行:

loginValidator.validate(loginForm, bindingResult);

一切都应该没问题。因为你有@Valid,你正在使用 JSR-303 来验证你的 bean。您不必通过手动调用validate() 方法再次验证它。

【讨论】:

  • 哦,谢谢!我认为这是一种验证方法是如此之大。如果我想使用改进的验证器,例如 "regexp = "^([a-zA-Z0-9_-])+$")" 我应该使用这种形式吗? loginValidator.validate(loginForm, bindingResult);
  • @Devour,你的意思是你想用正则表达式验证密码?那么你可以在 LoginForm 中使用@Pattern
  • 我试过了,还是不行。我已经完成了验证器类。谢谢大家的帮助。
猜你喜欢
  • 2012-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-03
  • 2014-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多