Spring验证不从message.properties中选择消息

时间:2014-10-06 21:14:42

标签: spring validation spring-mvc

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?     

1 个答案:

答案 0 :(得分:0)

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

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

消息的语法应为:

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