Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javaxbean验证未解析消息代码_Java_Spring_Validation_Spring Mvc - Fatal编程技术网

javaxbean验证未解析消息代码

javaxbean验证未解析消息代码,java,spring,validation,spring-mvc,Java,Spring,Validation,Spring Mvc,我正在SpringMVC项目中使用JavaXbean验证。 这是我的示例代码 User.java:- @NotEmpty(message = "{error.user.username.empty}") private String userName; public String handleSubmission(@ModelAttribute("user")@Valid User user, BindingResult errors) { .... } UserCo

我正在SpringMVC项目中使用JavaXbean验证。 这是我的示例代码

User.java:-

@NotEmpty(message = "{error.user.username.empty}")
private String userName;
public String handleSubmission(@ModelAttribute("user")@Valid User user, BindingResult errors) {
        ....
    }
UserController.java:-

@NotEmpty(message = "{error.user.username.empty}")
private String userName;
public String handleSubmission(@ModelAttribute("user")@Valid User user, BindingResult errors) {
        ....
    }
消息\u en.properties:-

error.user.username.empty=Username can not be empty
NotEmpty.user.userName=Username can not be empty
无论何时“用户名”字段为空,验证都会按预期失败,但应用程序会引发异常

Caused by: javax.servlet.jsp.JspTagException: No message found under code 'NotEmpty' for locale 'en_US'.
在日志中我可以看到

Field error in object 'user' on field 'userName': rejected value []; codes [NotEmpty.user.userName,NotEmpty.userName,NotEmpty.java.lang.String,NotEmpty]; 
arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.userName,userName]; arguments []; default message [userName]]; default message [Username cannot be empty]
验证批注中使用的消息键为“error.user.username.empty”,该键在messages\u en.properties中定义,但应用程序仅显示 对于键“NotEmpty”。即使添加“NotEmpty.user.userName”键也无济于事。 只有在属性文件中添加“NotEmpty”键时,异常才会消失,例如

NotEmpty=Username can not be empty
MessageSource配置如下:

@Configuration
@EnableWebMvc
public class AppConfig extends WebMvcConfigurerAdapter {
    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
        return messageSource;
    }
} 
显示错误消息的JSP代码是

user.jsp:-

<spring:hasBindErrors name="user">
   <ul>
       <li> <span class="error">Please fix all errors and try again.</span></li>    
      <c:forEach var="error" items="${errors.allErrors}">
       <li> <span class="error"><spring:message code="${error.code}"/></span></li>
      </c:forEach>
   </ul>
</spring:hasBindErrors>
这意味着正在从ValidationMessages.properties文件中正确读取defaultMessage“用户名不能为空”。因为这就是这里定义的消息。但错误代码仍然是“NotEmpty”。 所以总而言之,

  • 如果我试图通过迭代上面JSP代码中显示的所有错误来显示消息,我会得到一个异常
  • 如果我使用
    ,除了javaxbean验证消息之外,所有错误消息都会显示为bean“user”
  • 使用
    不显示任何内容
    在类路径中创建名为
    ValidationMessages.properties
    的文件,并在其中添加验证错误消息

    更新自定义您的
    验证程序
    以使用注册的
    消息源

    @Configuration
    @EnableWebMvc
    public class AppConfig extends WebMvcConfigurerAdapter {
        @Override
        public Validator getValidator() {
            return validator();
        }
    
        @Bean
        public LocalValidatorFactoryBean validator() {
            LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
            bean.setValidationMessageSource(messageSource());
    
            return bean;
        }
    
        @Bean
        public MessageSource messageSource() {
            ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
            messageSource.setBasename("messages");
            return messageSource;
        }
    } 
    

    哪个属性文件?添加您的配置和jsp。谢谢M.Deinum。使用配置和jsp代码更新了问题。我在messages_en.properties文件中定义了消息。请使用
    而不是您现在拥有的消息。
    ${error.code}
    将只解析单个代码。或者使用
    ${error}
    或者只使用
    标记,而不是现在的标记。我尝试了你的方法。在这种情况下,不会引发异常,也不会显示javax.validation错误消息。但是除了javaxbean验证之外,我还手动进行一些其他验证。这些消息将显示在JSP中。例如,-if(Utils.isDuplicateUserName(user)){errors.rejectValue(“username”,“error.username.take”);}-在jsp中,我现在有了。javaxbean验证没有显示出来,即使在调试模式下,我可以看到javaxbean验证错误。当你自己拒绝东西时,你只有一个代码,当你使用spring时,你有多个代码。现在调用
    getCode
    将只返回其中一个代码。路径应该与您在
    中使用的路径相同,因此它不应该是user,而应该是
    userName
    。谢谢。我没有提到这一点,但我也尝试过这种方法。我在“src/main/resources”文件夹中有一个名为ValidationMessages.properties的文件,该文件与定义了所需消息代码的messages_en.properties文件一起。但不幸的是,它不工作,与早期尝试你的建议相同的例外。但这并没有解决问题:-(