【问题标题】:Spring validation not picking messages from message.propertiesSpring 验证不从 message.properties 中选择消息
【发布时间】:2014-10-07 06:14:26
【问题描述】:
This is my project structure.

MySpringVAlidation
  -src/main/java
      -com.myproject.controllers
          -SearchCustomerController.java
      -com.myproject.model
          -SearchCustomer.java
      -applicationContext.xml
      -messages_en_US.properties
  -src/main/webapp/WEB-INF
   -mvc-dispatcher-servlet.xml
   -web.xml
  -src/main/webapp/WEB-INF/views
      -AddModifyCustomer.jsp


messages_en_US.properties ( properties file has custom messages)
==========================
NotEmpty.SearchCustomerForm.custId = Customer Id  must be 7 characters.

mvc-dispatcher-servlet.xml  (dispatcher has Spring configurations)
===========================
<mvc:annotation-driven />

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename">
        <value>messages_en_US.properties</value>
    </property>
</bean>


SearchCustomer.java  
======================-==========================================================

    @NotEmpty
    @XmlAttribute(name="CustomerID")
    public String getCustId() {
        return custId;
    }

SearchCustomerController.java 
=================================
@RequestMapping(value="/searchCustomer" , method = RequestMethod.POST )
public String processAdd( @ModelAttribute("searchCustomerForm") @Valid SearchCustomer sCust, 
            BindingResult result,   Map<String, Object> model, HttpSession session ) throws IOException, JMSException { 
        sCust.setOrganizationCode("SUPPLY");
        //System.out.println("Session ID:"+session.getId());
        System.out.println("Errors :- "+result.hasErrors()+result.getAllErrors());
        if(result.hasErrors()){
            Customer customerForm = new Customer();
            model.put("customerForm", customerForm);
            return "AddModifyCustomer";
        }else{
                postSearchRequest.postMessage(sCust, "SearchCustomer.xml",session.getId());     
                //For the second tab
                        Customer customerForm = new Customer();
                        model.put("customerForm", customerForm);

                return "AddModifyCustomer";
        }
    }

AddModifyCustomer.jsp
=====================

<form:form action="searchCustomer" method="post" commandName="searchCustomerForm">                              
<label style="padding-right: .25em;width: 7em;text-align: right;float: left;">
Customer ID : </label><form:input id="custId" path="custId" />

 <input style="float:center;" type="submit" name="Search" id="Search" value="Search" />&nbsp; <button type="button" name="add" id="add" value="add">add</button>    
 <br/><form:errors path="custId"></form:errors>                     
<form:hidden path="organizationCode" />        
</form:form>

当我点击搜索客户而不提供任何输入时的预期结果:
Customer Id 必须是 7 字符。
实际结果:-may not be empty 注意@NotEmpty,即使我尝试过@NotEmpty(message="{NotEmpty.SearchCustomerForm.custId}"),但仍然得到相同的结果

The messages in properties file is not get picked up. Its always showing default messages. Where am i going wrong?     

【问题讨论】:

  • 您使用的是 maven 和默认的 maven 结构吗?
  • 是的。我正在使用 Maven。
  • 最初我收到一个警告,无法找到捆绑包messages_en_US.properties。所以我把它保存在我的 src/main/java 文件夹而不是 src/main/webapp/resources
  • 如果我打印错误,我会收到以下消息: true [字段 'custId' 上的对象 'searchCustomerForm' 中的字段错误:拒绝值 [];代码 [NotEmpty.searchCustomerForm.custId,NotEmpty.custId,NotEmpty.java.lang.String,NotEmpty];参数 [org.springframework.context.support.DefaultMessageSourceResolvable: 代码 [searchCustomerForm.custId,custId];论据 [];默认消息 [custId]];默认消息[不能为空]]
  • 尝试将你的属性文件移动到 /src/main/resources

标签: spring validation spring-mvc


【解决方案1】:

您的类是 SearchCustomer,但在您的属性文件中您使用的是 SearchCustomerForm。

试试NotEmpty.SearchCustomer.custId = Customer Id must be 7 characters.

消息的语法应该是:

[Constraint].[Class name].[Property]=[Message]

【讨论】:

  • 即使我将语法更改为 NotEmpty.SearchCustomer.custId = 客户 ID 必须为 7 个字符,我仍然会收到相同的错误。
  • 您的 ReloadableResourceBundleMessageSource bean 定义不明确。您不应该指定完整的名称,Spring 在搜索时只添加“messages”、“_en_US.properties”。试试:
  • 仍然得到相同的结果。没有变化:字段“custId”上的对象“searchCustomerForm”中的字段错误:拒绝值 [];代码 [NotEmpty.searchCustomerForm.custId,NotEmpty.custId,NotEmpty.java.lang.String,NotEmpty];参数 [org.springframework.context.support.DefaultMessageSourceResolvable: 代码 [searchCustomerForm.custId,custId];论据 [];默认消息 [custId]];默认消息[不能为空]]
  • 您现在在属性文件 SearchCustomer 或 SearchCustomerForm 中使用什么?它正在寻找 searchCustomerForm,因为您使用的是 @ModelAttribute("searchCustomerForm")。
  • NotEmpty.SearchCustomer.custId = 客户 ID 必须为 7 个字符。
猜你喜欢
  • 1970-01-01
  • 2016-10-09
  • 1970-01-01
  • 1970-01-01
  • 2020-05-26
  • 2012-10-30
  • 1970-01-01
  • 2012-07-23
  • 2015-03-17
相关资源
最近更新 更多