Raul Barros
Raul Barros

Reputation: 117

Use spring:message from JavaScript

I have a JQuery function who add a Table in the JSP dynamically:

$('#add').click(function(event) {

    event.preventDefault();

$('.tabela_procurador').before
    ('<table id="tabela_nova' + i + '" class="tabela_nova"> ' +
        '<tr> ' +
            '<td colspan="4" class="subTitulo_barra"> ' +
            '<spring:message code="representante_legal" /> '+ i +' ' +
            '</td> ' +
        '</tr> ' +
      '</table>');
     i++
   });
});

But when i added this table i lost the spring:message.

There is something i can do to jquery recognize this spring:message?

Upvotes: 8

Views: 22409

Answers (4)

Saakshi Aggarwal
Saakshi Aggarwal

Reputation: 558

In your JSP, you can assign the spring:message to a javascript variable, making available to your other jQuery code:

# In .JSP
<script type="text/javascript">
  var abc="<spring:message code="representante_legal"/>";
</script>

Upvotes: 2

gizit
gizit

Reputation: 180

As a workaround, put the message value in a hidden input on your jsp page. Then get its value in your javascript. In your case:

<c:set var="val"><spring:message code="representante_legal"/></c:set>
<input id="representante_legal" type="hidden" value="${val}"/>

In your javascript (using jquery) you can then use it as follows:

$('#representante_legal').val()

Upvotes: 12

Luke Camilleri
Luke Camilleri

Reputation: 41

Make sure <spring:message code="representante_legal" /> is in a JSP, if that tag is in a javascript file, it will never be translated to the localized string.

JSP files are compiled before they are sent to the requesting client, while javascript is served as static content.

Upvotes: 4

colestrode
colestrode

Reputation: 10658

There's no way for jQuery to have access to a spring tag. spring:message is processed server-side before the page is sent to the client, javascript/jQuery is processed later on the client side.

Upvotes: 7

Related Questions