Normally I retrieve i18n messages via <spring:message code="" />
tag in jsp. But as response of ajax query I get message code in i18n property file. How can I get message by this code via JS?
There is no "normal" way to get messages from JS, but you have two solutions:
First solution: by an Ajax call.
Second solution : Send you're value at the loading of the page in hidden input html
<c:set var="val"><spring:message code="username"/></c:set>
<input id="username" type="hidden" value="${val}"/>
In your javascript (using jquery) you can then use it as follows:
$('#username').val()
You can inject MessageSource
to your controller and return a String
message from that controller. Simply:
@Autowired
MessageSource messageSource;
@RequestMapping(value="/myajax", method=RequestMethod.GET)
@ResponseBody
public String getMyAjaxMessage() {
return messageSource.getMessage(..); // use your proper arguments or extract from request parameters
}
javascript (assuming jquery is used):
$.get('/myajax', function(data){
// do whatever you want with data (will contain your message)
});
Here are docs:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.