java 模拟 spring:message 标签的控制器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3526048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Controller that simulates spring:message tag?
提问by Hurda
I'm designing help/hint system for my webpage, I'd like to incoroporate jQuery based contextual help.
我正在为我的网页设计帮助/提示系统,我想合并基于 jQuery 的上下文帮助。
My idea is that I would post request on event and show given repsonse in special div or so.
我的想法是我会在事件上发布请求并在特殊的 div 左右显示给定的响应。
I would like to get similiar behaviour as <spring:message>tag, post message code and get String representation on localized message. It would be great if it would use same resources.
我想获得与<spring:message>标签类似的行为,发布消息代码并获取本地化消息的字符串表示。如果它使用相同的资源,那就太好了。
Is there a way to call this tag from Controller? (there is Java code behind this tag) Or what's best way to mimick this tag controller-side?
有没有办法从控制器调用这个标签?(这个标签后面有 Java 代码)或者模仿这个标签控制器端的最好方法是什么?
采纳答案by Teja Kantamneni
Use ResourceBundleMessageSource to declare the messages and then autowire/inject to your controller.
使用 ResourceBundleMessageSource 声明消息,然后自动装配/注入您的控制器。
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="messages"/>
alternatively you can use util: propertiesto load a properties file.
或者,您可以使用util: properties加载属性文件。
<util:properties id="jdbcConfiguration" location="classpath:com/foo/jdbc-production.properties"/>
then access this resource in your controller using the id.
然后使用 id 在您的控制器中访问此资源。
回答by Hurda
With the help of Teja Kantamneni I come with this Controller
在 Teja Kantamneni 的帮助下,我带着这个控制器
@Controller
public class HelpController {
protected Log log = LogFactory.getLog(getClass());
@Autowired
private MessageSource messageSource;
@RequestMapping(value = "ajax/gethelp")
public @ResponseBody String handleGetHelp(Locale loc, String code) {
return messageSource.getMessage(code, null, loc);
}
}

