据我所知,所有javascript文件都不是由服务器编译的,因为它只是直接提供。
但是,在我的后端,大多数变量被组织为静态最终变量,这样当事情发生变化时,可以在一个地方完全改变。
目前我没有办法维护javascript和后端动作类变量之间的协调。像网上唯一的建议就是在jsp文件中创建javascript。有没有其他选择?
答案 0 :(得分:0)
您可以从JSP动态创建javascript文件。
例如:在您的html或主jsp文件中,您有以下
dyanmicJavascript.action将调用您的动作类,然后返回一个jsp。 jsp只有javascript代码(NO HTML STUFF)。所以你的jsp看起来像。
//标记库的东西+其他jsp特定的东西 //简单的javascript代码如下 var globalVariable = 10; var dynamicVariables =
答案 1 :(得分:0)
替代从jsp文件创建js
在JSP中设置javascript变量,然后在javascript(.js)文件中使用它
例如
<强>的index.jsp 强>
<html>
<head>
<script>
var contextPath="${pageContext.request.contextPath}";
var otherStaticVariable="<s:property value="@ConstantsClass@GlobalConstantVar"/>";
</script>
<script src="js/other.js"></script>
</head>
//Rest of the page
</html>
现在other.js可以直接引用contextPath&amp;在其代码中的otherStaticVariable。
我在我的一些项目中使用了类似的代码。