Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Validation Spring 3-在jsp中访问messages.properties_Validation_Spring Mvc - Fatal编程技术网

Validation Spring 3-在jsp中访问messages.properties

Validation Spring 3-在jsp中访问messages.properties,validation,spring-mvc,Validation,Spring Mvc,我是使用Spring3的新手,在这方面已经有一段时间了 您知道如何从jsp访问messages.properties吗。例如,在控制器中,我为模型设置了一个值: model.setError("user.not.found") messages.properties: user.not.found=Sorry, we haven't been able to found this user 在jsp中,我希望能够 ${model.error} 并显示“对不起…”。然而,我总是得到“user

我是使用Spring3的新手,在这方面已经有一段时间了

您知道如何从jsp访问messages.properties吗。例如,在控制器中,我为模型设置了一个值:

model.setError("user.not.found")
messages.properties:

user.not.found=Sorry, we haven't been able to found this user
在jsp中,我希望能够

${model.error}
并显示“对不起…”。然而,我总是得到“user.not.found”,即使在我使用@Valid…、bindingResult和表单时,这样做很好

谢谢,

使用
弹簧中的
标记库:

<spring:message code = "${model.error}" />

其中taglib作为

<%@ taglib prefix = "spring" uri = "http://www.springframework.org/tags" %>

如果将消息解析器放入控制器的模型(或ModelAndView)中,则可以在JSP中使用
${msg.getMessage('msg_code')}

// In a controller class

...

@Autowired
private MessageResolver messageResolver;

...

@RequestMapping(value="/edit")
public ModelAndView getSomething(MyFormData formData,
                                 ModelAndView mv) {
    mv.setViewName("TARGET_VIEW");

    // Do some controller things...

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("msg", messageResolver);

    mv.addAllObjects(map);

    return mv;
}

更好的做法是实现一个自定义拦截器(特别是postHandle方法),将messageResolver放入ModelAndView,而不是在所有控制器中编写相同的代码。

下面是taglib参考:可以不使用标记从messageSource获取消息,我指的是像${msg['code']}这样的表达式?@marioosh:我想,没有简单的方法。
<form:select path="domainObj1.property1" cssClass="form-control">
    <form:option value="" label="--${msg.getMessage('L01006')}--" />
    <form:options items="${selection.selectionList}" itemValue="code" itemLabel="codeVal"/>
</form:select>