【问题标题】:Spring MVC Validator without Message Properties File and Resource Bundle没有消息属性文件和资源包的 Spring MVC 验证器
【发布时间】:2018-04-11 08:33:22
【问题描述】:

我有这样的场景。

  1. 我有一个jsp文件,有6个字段:userId、userName、lastName、dob、emailId、password。

    <form:form method="POST"  modelAttribute="keyUserRegistration" action="checkRegistration.do">
    
    <table width="100%" border="0">
    
              <tr>
                <td><div class="form-control">
                <form:errors path="userId" ></form:errors>
            <label>
              <p>
                User ID<small class="required"></small>
              </p>  
              <form:input path="userId" id="userId"/>
                 </label> </div>
         </td>
      </tr>
    
      </table>
      </form:form>
    

我以类似的方式编写了所有其他属性。

  1. 我有一个控制器来处理来自我的页面的请求,如下所示:

       @RequestMapping(value = "/checkRegistration", method = 
       RequestMethod.POST)
       public String submitLogin( @ModelAttribute("keyUserRegistration") 
       UserRegistration userR, BindingResult result, Model model, 
         ) {
    
    if (userR != null && !userR.equals("null")) {
        registrationValidator.validate(userR, result);
        if (result.hasErrors()) {
    
            System.out.println("controller result has errors");
    
            return "registration";
        }
    }
    
  2. 我有 Validator 类来验证表单:

       @Component
         public class RegistrationValidator implements Validator{
    
         public boolean supports(Class<?> clazz) {
               return UserRegistration.class.isAssignableFrom(clazz);
        }
    
     public void validate(Object target, Errors errors) {
    
    UserRegistration registration = (UserRegistration) target;
    
    ValidationUtils.rejectIfEmpty(errors, "userId", "Please enter User Id");
    ValidationUtils.rejectIfEmpty(errors, "emailId", "Please enter Email Id");
    ValidationUtils.rejectIfEmpty(errors, "password", "Please enter Password");
    ValidationUtils.rejectIfEmpty(errors, "lastName", "Please enter Last Name");
    ValidationUtils.rejectIfEmpty(errors, "dob", "Please enter Date Of Birth");
    ValidationUtils.rejectIfEmpty(errors, "userName", "Please enter User Name");
    
      }
    }
    

我总是遇到例外:

org.springframework.context.NoSuchMessageException:在 locale 'en_US' 的代码“请输入用户 ID.keyUserRegistration.userId”下找不到消息'

我的问题:

有没有什么方法可以使用spring验证器在jsp上实现错误消息,但不声明消息属性文件和资源包?

【问题讨论】:

    标签: spring-mvc


    【解决方案1】:

    试试这个方法-

    ValidationUtils.rejectIfEmpty(errors, "your attribute","error.your attribute", "Error message")

    【讨论】:

      【解决方案2】:

      试试:

      ValidationUtils.rejectIfEmpty(errors, "userId","error.userId", "Please enter User Id");
      

      【讨论】:

        【解决方案3】:

        试试这个方法:

        UserRegistration registration = (UserRegistration) target;
        System.out.println(registration.getUserId());
        ValidationUtils.rejectIfEmpty(errors, "userId","error.userId", "Please enter User Id");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-10-04
          • 1970-01-01
          • 2023-03-08
          • 2012-07-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-25
          相关资源
          最近更新 更多