I would like to get values sent by the Servlet
using the HttpSession
in an external js file using requirejs' define
function:
我想使用requirejs的define函数在外部js文件中使用HttpSession获取Servlet发送的值:
define(function(){
return {
/*Get values from the java session*/
gisuniqkey:<%=session.getAttribute('gisuniqkey')%>,
scenario:<%=session.getAttribute('scenario')%>,
operation:<%=session.getAttribute('operation')%>,
objkey:<%=session.getAttribute('objkey')%>
}
});
Is it possible to do this? Any dependencies that I should include when doing this? Eclipse tells me that there is an error when doing this.
是否有可能做到这一点?这样做时我应该包含哪些依赖项? Eclipse告诉我这样做时出错。
The error message simply calls it a syntax error:
该错误消息只是将其称为语法错误:
Uncaught SyntaxError: Unexpected token < java_to_script.js:3
Uncaught TypeError: Cannot read property 'gisuniqkey' of undefined
gmaps_createScenario.js:15
All I care is that I do not use a global variable inside my jsp...and that it should work well with require.js,it would be splendid if it could be made a reusable module.
我所关心的只是我不在我的jsp中使用全局变量......并且它应该与require.js一起使用,如果它可以成为可重用的模块,那将是非常出色的。
1 个解决方案
#1
0
You can't use JSP in a static .js
file.
您不能在静态.js文件中使用JSP。
You can use JSP in a .jsp
file that outputs a JavaScript file.
您可以在输出JavaScript文件的.jsp文件中使用JSP。
Output an application/javascript
content type, and then write your JS as normal in the .jsp
file using JSP directives are you wish.
输出应用程序/ javascript内容类型,然后使用JSP指令在.jsp文件中正常编写JS。
Note that, since the JavaScript file will be requested separately to the HTML file you won't be able to read things like the query string used to request the HTML file from the JSP directives in the JavaScript file (although you could pass them via a session, or add them to the src
of the <script>
element).
请注意,由于JavaScript文件将单独请求HTML文件,因此您将无法读取用于从JavaScript文件中的JSP指令请求HTML文件的查询字符串(尽管您可以通过session,或将它们添加到
As a rule of thumb, however, it is often better to stick to static JS files and include dynamically generated JS as inline <script>
within the HTML document (although keeping such JS to a minimum - just enough to store the data you need). That makes sorting caching rules simpler.
但是,根据经验,通常最好坚持使用静态JS文件,并在HTML文档中包含动态生成的JS作为内联
#1
0
You can't use JSP in a static .js
file.
您不能在静态.js文件中使用JSP。
You can use JSP in a .jsp
file that outputs a JavaScript file.
您可以在输出JavaScript文件的.jsp文件中使用JSP。
Output an application/javascript
content type, and then write your JS as normal in the .jsp
file using JSP directives are you wish.
输出应用程序/ javascript内容类型,然后使用JSP指令在.jsp文件中正常编写JS。
Note that, since the JavaScript file will be requested separately to the HTML file you won't be able to read things like the query string used to request the HTML file from the JSP directives in the JavaScript file (although you could pass them via a session, or add them to the src
of the <script>
element).
请注意,由于JavaScript文件将单独请求HTML文件,因此您将无法读取用于从JavaScript文件中的JSP指令请求HTML文件的查询字符串(尽管您可以通过session,或将它们添加到
As a rule of thumb, however, it is often better to stick to static JS files and include dynamically generated JS as inline <script>
within the HTML document (although keeping such JS to a minimum - just enough to store the data you need). That makes sorting caching rules simpler.
但是,根据经验,通常最好坚持使用静态JS文件,并在HTML文档中包含动态生成的JS作为内联