如何在 javascript 中使用 scriptlet
有人可以测试这个例子并分享结果吗? http://timothypowell.net/blog/?p=23
当我这样做时:
var myVar = '<% request.getContextPath(); %>';
alert(myVar);
我得到: '<% request.getContextPath(); %>'.
从 '<% request.getContextPath(); 中删除单引号%>'; 给出语法错误。 如何在 js 函数中使用 scrptlet 或表达式?
编辑:此链接有一个对我有帮助的解释:
http://www.codingforums.com/showthread.php?t=172082
Can someone test this example and share the results?
http://timothypowell.net/blog/?p=23
When I do:
var myVar = '<% request.getContextPath(); %>';
alert(myVar);
I get : '<% request.getContextPath(); %>'.
Removing the enclosing single quotes from '<% request.getContextPath(); %>';
gives syntax error.
How can I use the scrptlet or expresion inside a js function?
EDIT: this link has an explanation that helped me:
http://www.codingforums.com/showthread.php?t=172082
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该行代码必须放置在
.jsp
文件中的 HTML标记中。这样,
JspServlet
将处理 scriptlet(以及其他 JSP/EL 特定表达式)。请注意,
<%= %>
是打印变量的正确语法,<% %>
不会这样做。 em>或者,如果打算在独立的
.js
文件中提供服务,那么您需要将其重命名为.jsp
并将以下内容添加到文件顶部(并相应地更改URL):
这样,
JspServlet
将对其进行处理,并且将指示浏览器将 JSP 响应正文解释为 JavaScript 而不是 HTML 。与具体问题无关,请注意 scriptlet 被视为
That line of code has to be placed in a HTML
<script>
tag in a.jsp
file. This way theJspServlet
will process the scriptlets (and other JSP/EL specific expressions).Note that
<%= %>
is the right syntax to print a variable, the<% %>
doesn't do that.Or if it is intended to be served in a standalone
.js
file, then you need to rename it to.jsp
and add the following to top of the file (and change the<script src>
URL accordingly):This way the
JspServlet
will process it and the browser will be instructed to interpret the JSP response body as JavaScript instead of HTML.Unrelated to the concrete problem, note that scriptlets are considered poor practice. Use EL.
听起来好像您将 JSP 代码放置在 JavaScript 页面中,或者至少放置在非 JSP 页面中。 Scriptlet 只能包含在 JSP 页面中(通常配置为 *.jsp)。
所提供的语句,如果由 JSP 编译器处理,将导致 myVar 等于 '' 作为您正在使用的 scriptlet 格式 <% ... %>在标记之间执行 Java 代码,但不返回结果。
因此,要使用此标记,您需要手动将值写入请求输出流。要获得所需的功能,您需要执行以下操作:
尽管如此,在大多数情况下,scriptlet 被视为不好的做法。对于大多数情况,您应该使用 JSTL 表达式和自定义标签。
It sounds like you are placing the JSP code within a JavaScript page, or at least in a non-JSP page. Scriptlets can only be included in a JSP page (typically configured to be *.jsp).
The statement as presented, if processed by the JSP compiler, would result in myVar being equal to '' as the scriptlet format you are using <% ... %> executes Java code between the tags, but does not return a result.
So, to use this tag you would need to manually write a value to the request output stream. To get the desired functionality you need to do the following:
With all that said, scriptlets are viewed as bad practice in most cases. For most cases, your should be using JSTL expressions and custom tags.
您无法通过赋予正常的
.js
扩展名来在javascript
中运行scriptlet
。但是,您可以为您的.js
提供.jsp
的文件扩展名,然后直接链接到它:现在您可以在其中使用
jsp
yourjavascript
like:此操作完全有效,因为决定文件是否为
javascript
文件的是 MIME 媒体类型。您可以使用以下方法从 JSP 设置 MIME:You cannot run
scriptlet
insidejavascript
by giving it normal.js
extension. However you can give your.js
the file extension of.jsp
and then and link directly to it as:and now you can use
jsp
within yourjavascript
like:This action is completely valid because what determines whether a file is a
javascript
file or not is what MIME media type. You can set MIME from JSP using:您忘记在请求之前输出 put = 并删除 ;在 getContextPath() 之后
You forgot to out put = before request and remove ; after getContextPath()