Question

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?

Was it helpful?

Solution 2

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:

MessageSource

jquery get

OTHER TIPS

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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top