Controller that simulates spring:message tag?
I'm designing help/hint system for my webpage, I'd like to incoroporate jQuery based contextual help.
My idea is that I would post request on event and show given repsonse in special div or so.
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 i开发者_StackOverflow社区t would use same resources.
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?
With the help of Teja Kantamneni I come with this Controller
@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);
}
}
Use ResourceBundleMessageSource to declare the messages and then autowire/inject to your controller.
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="messages"/>
alternatively you can use util: properties
to load a properties file.
<util:properties id="jdbcConfiguration" location="classpath:com/foo/jdbc-production.properties"/>
then access this resource in your controller using the id.
精彩评论