【发布时间】: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" /> <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