Spring typeMismatch错误,即使messages.properties中存在typeMismatch=msg
当我试图在jsp表单的Spring typeMismatch错误,即使messages.properties中存在typeMismatch=msg,spring,spring-mvc,Spring,Spring Mvc,当我试图在jsp表单的int字段中键入String时,仍然出现一个致命错误,而不是正常的表单验证。我在messages.properties()中添加了typeMismatch=msg,但它不起作用。我还缺少什么?发现了问题:您无法编写以下内容: @RequestMapping(value = "/addSomething", method = RequestMethod.POST) public String addSomething( @ModelAttribute("som
int
字段中键入String
时,仍然出现一个致命错误,而不是正常的表单验证。我在messages.properties()中添加了typeMismatch=msg
,但它不起作用。我还缺少什么?发现了问题:您无法编写以下内容:
@RequestMapping(value = "/addSomething", method = RequestMethod.POST)
public String addSomething(
@ModelAttribute("something") Something something,
@Valid Something validSomething,
BindingResult result,
ModelMap map) {
...
某些属性必须从字符串转换为int,并且在验证之前进行转换。这不好!而且validSomething
也没用。这解决了问题:
@RequestMapping(value = "/addSomething", method = RequestMethod.POST)
public String addSomething(
@Valid @ModelAttribute("something") Something something,
BindingResult result,
ModelMap map) {
...
这也证明了您最好将代码放在问题中。“你”指的是我。Spring MVC应用程序通常使用两种上下文:ApplicationContext和Dispatcher Servlet上下文。检查Dispatcher Servlet上下文中定义并由他使用的
messages.properties
。感谢您的回复。我的messages.properties
实际上是messages\u fr.properties
并且可以很好地处理包括验证消息在内的许多其他消息。