如何在jQuery中访问Struts 2变量 [英] How to access Struts 2 variables in jQuery
问题描述
我正在尝试通过jQuery访问Struts 2属性。
I'm trying to get access to a Struts 2 property via jQuery.
我的Common.js文件如下所示:
My Common.js file looks like this:
$(document).ready(function() {
alert("<s:property value='myVariable'/>");
});
当我加载视图时,它会提醒整个字符串而不是解析Struts 2变量。我猜我只缺少一些语法技巧......任何帮助都会受到赞赏。谢谢!
When I load my view, it alerts the whole string instead of resolving the Struts 2 variable. I'm guessing there's just some syntax tricks that I'm missing... any help would be appreciated. Thanks!
推荐答案
您应该将脚本放到JSP中
You should place the script to the JSP
<head>
<script type="text/JavaScript">
$(document).ready(function() {
alert(<s:property value="myVariable"/>);
});
</script>
</head>
你不能在js文件中使用struts或其他JSP标签。它不由服务器编译。但是,您可以在JSP中使用一个函数,通过向其传递参数来调用这些脚本。
you cannot use struts or other JSP tags inside the js files. It doesn't compile by the server. However, you could use a function in JSP that call those scripts by passing parameters to it.
<head>
<script type="text/javascript" src="<s:url value='/js/Common.js'/>"></script>
<script type="text/JavaScript">
$(document).ready(function() {
alertMyVariable(<s:property value="myVariable"/>);
});
</script>
</head>
在js中:
function alertMyVariable(myVariable) {
alert(myVariable);
}
这篇关于如何在jQuery中访问Struts 2变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!