Javascript 如何在javascript中使用scriptlet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5158400/
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
How to use scriptlet inside javascript
提问by Victor
Can someone test this example and share the results?
http://timothypowell.net/blog/?p=23
When I do:
有人可以测试这个例子并分享结果吗?
http://timothypowell.net/blog/?p=23
当我这样做时:
var myVar = '<% request.getContextPath(); %>';
alert(myVar);
I get : '<% request.getContextPath(); %>'.
我得到: '<% 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?
从 '<% request.getContextPath(); 中删除封闭的单引号;%>'; 给出语法错误。如何在 js 函数中使用 scrptlet 或表达式?
EDIT: this link has an explanation that helped me:
http://www.codingforums.com/showthread.php?t=172082
编辑:此链接的解释对我有帮助:http:
//www.codingforums.com/showthread.php?t=172082
采纳答案by Kris Babic
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).
听起来您将 JSP 代码放置在 JavaScript 页面中,或者至少放置在非 JSP 页面中。Scriptlet 只能包含在 JSP 页面中(通常配置为 *.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.
所提供的语句,如果由 JSP 编译器处理,将导致 myVar 等于 '',因为您使用的 scriptlet 格式 <% ... %> 在标记之间执行 Java 代码,但不返回结果。
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:
因此,要使用此标记,您需要手动将值写入请求输出流。要获得所需的功能,您需要执行以下操作:
make sure your code is in a JSP page
use var myVar = '<%= request.getContextPath() %>'; (note the equals sign)
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.
尽管如此,在大多数情况下,scriptlet 被视为不好的做法。大多数情况下,您应该使用 JSTL 表达式和自定义标记。
回答by BalusC
That line of code has to be placed in a HTML <script>
tag in a .jsp
file. This way the JspServlet
will process the scriptlets (and other JSP/EL specific expressions).
这行代码必须放在文件的 HTML<script>
标记中.jsp
。这种方式JspServlet
将处理小脚本(和其他 JSP/EL 特定表达式)。
<script>var myVar = '<%= request.getContextPath() %>';</script>
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):
或者,如果它打算在独立.js
文件中提供,那么您需要将其重命名为.jsp
并将以下内容添加到文件顶部(并相应地更改<script src>
URL):
<%@page contentType="text/javascript" %>
...
var myVar = '<%= request.getContextPath() %>';
This way the JspServlet
will process it and the browser will be instructed to interpret the JSP response body as JavaScript instead of HTML.
通过这种方式,JspServlet
将处理它,并指示浏览器将 JSP 响应正文解释为 JavaScript 而不是 HTML。
Unrelated to the concrete problem, note that scriptletsare considered poor practice. Use EL.
与具体问题无关,请注意scriptlet被认为是糟糕的做法。使用EL。
var myVar = '${pageContext.request.contextPath}';
回答by Raman Sahasi
You cannot run scriptlet
inside javascript
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:
你不能通过给它正常的扩展来跑scriptlet
进去。但是你可以给你的文件扩展名,然后直接链接到它:javascript
.js
.js
.jsp
<script type="text/javascript" src="/script.jsp"></script>
and now you can use jsp
within your javascript
like:
现在你可以jsp
在你javascript
喜欢的范围内使用:
var someMessage = "${someMessage}"
var anotherMessage = "${anotherMessage}"/>"
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:
此操作是完全有效的,因为决定文件是否为文件的是javascript
MIME 媒体类型。您可以使用以下方法从 JSP 设置 MIME:
<%@ page contentType="text/javascript" %>
回答by Arnold Parge
var myVar = '<%=request.getContextPath() %>';
alert(myVar);
You forgot to out put = before request and remove ; after getContextPath()
您忘记在请求和删除之前输出 = ;在 getContextPath() 之后