使用 spring:来自 JavaScript 的消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16817964/
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
Use spring:message from JavaScript
提问by Raul Barros
I have a JQuery function who add a Table in the JSP dynamically:
我有一个 JQuery 函数,它动态地在 JSP 中添加一个表:
$('#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.
但是当我添加这张表时,我丢失了 spring:message。
There is something i can do to jquery recognize this spring:message?
我可以做些什么来让 jquery 识别这个春天:消息?
采纳答案by cfs
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.
jQuery 无法访问spring
标签。spring:message
在将页面发送到客户端之前在服务器端处理,稍后在客户端处理 javascript/jQuery。
回答by gizit
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:
作为解决方法,将消息值放在 jsp 页面上的隐藏输入中。然后在你的 javascript 中获取它的值。在你的情况下:
<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:
在您的 javascript(使用 jquery)中,您可以按如下方式使用它:
$('#representante_legal').val()
回答by Luke Camilleri
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.
确保<spring:message code="representante_legal" />
在 JSP 中,如果该标签在 javascript 文件中,它将永远不会被转换为本地化的字符串。
JSP files are compiled before they are sent to the requesting client, while javascript is served as static content.
JSP 文件在发送到请求客户端之前被编译,而 javascript 则作为静态内容。
回答by Saakshi Aggarwal
In your JSP, you can assign the spring:message
to a javascript variable, making available to your other jQuery code:
在您的 JSP 中,您可以将 分配spring:message
给一个 javascript 变量,使其可用于您的其他 jQuery 代码:
# In .JSP
<script type="text/javascript">
var abc="<spring:message code="representante_legal"/>";
</script>