我在弹簧验证期间从属性文件中获取键值对时遇到问题。
配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="controller"></context:component-scan>
<bean id="viewResolver"
class=" org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="messageResource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource" >
<property name="basename" value="messages"></property>
</bean>
</beans>
验证者
public class ValidatorPlayer implements Validator {
public boolean supports(Class<?> arg0) {
// TODO Auto-generated method stub
return false;
}
public void validate(Object obj, Errors errors) {
Player player = (Player) obj;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "id","test.to");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name","test.to");
}
}
JSP表格
<body>
<form:form commandName="command">
<table>
<tr>
<td>Enter Id</td>
<td><form:input path="id"/></td>
<td><form:errors path="id"/></td>
</tr>
<tr>
<td>Enter Name</td>
<td><form:input path="name"/></td>
<td><form:errors path="name"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="save"></td>
</tr>
</table>
</form:form>
</body>
</html>
控制器
@Controller
public class TestController {
@RequestMapping(value="/getForm.action")
public ModelAndView getForm(){
return new ModelAndView("formd","command",new Player());
}
@RequestMapping(value="/getForm.action", method=RequestMethod.POST)
public ModelAndView saveAdndValidateForm(@ModelAttribute("command") Player player,BindingResult errors){
new ValidatorPlayer().validate(player, errors);
if(errors.hasErrors()){
return new ModelAndView("formd");
}
return new ModelAndView("formd","command",new Player());
}
}
在messages.properties
文件中我定义了test.to =&#34;某些值&#34;这个文件在src文件夹中。
在bindingResult错误即将发生,但500错误正在网页上说上面提到的错误。