简体   繁体   English

如何在javascript中使用scriptlet

[英]How to use scriptlet inside javascript

Can someone test this example and share the results? 有人可以测试这个例子并分享结果吗? http://timothypowell.net/blog/?p=23 http://timothypowell.net/blog/?p=23
When I do: 当我做:

var myVar = '<% request.getContextPath(); %>';
alert(myVar);

I get : '<% request.getContextPath(); %>'. 我得到: '<% request.getContextPath(); %>'. '<% request.getContextPath(); %>'.

Removing the enclosing single quotes from '<% request.getContextPath(); 从'<%request.getContextPath();中删除封闭的单引号; %>'; %>'; gives syntax error. 给出了语法错误。 How can I use the scrptlet or expresion inside a js function? 如何在js函数中使用scrptlet或expresion?

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

That line of code has to be placed in a HTML <script> tag in a .jsp file. 该行代码必须放在.jsp文件中的HTML <script>标记中。 This way the JspServlet will process the scriptlets (and other JSP/EL specific expressions). 这样, JspServlet将处理scriptlet(以及其他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 scriptlets are considered poor practice . 与具体问题无关,请注意, scriptlet被认为是不好的做法 Use EL. 使用EL。

var myVar = '${pageContext.request.contextPath}';

It sounds like you are placing the JSP code within a JavaScript page, or at least in a non-JSP page. 听起来您将JSP代码放在JavaScript页面中,或者至少放在非JSP页面中。 Scriptlets can only be included in a JSP page (typically configured to be *.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. 尽管如此,在大多数情况下,scriptlet被视为不好的做法。 For most cases, your should be using JSTL expressions and custom tags. 在大多数情况下,您应该使用JSTL表达式和自定义标记。

You cannot run scriptlet inside javascript by giving it normal .js extension. 你不能通过给它正常的.js扩展名来运行javascriptscriptlet However you can give your .js the file extension of .jsp and then and link directly to it as: 但是,您可以为.js提供.jsp的文件扩展名,然后直接链接到它:

<script type="text/javascript" src="/script.jsp"></script>

and now you can use jsp within your javascript like: 现在你可以在你的javascript使用jsp

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. 此操作完全有效,因为确定文件是否是javascript文件的是MIME媒体类型。 You can set MIME from JSP using: 您可以使用以下命令从JSP设置MIME:

<%@ page contentType="text/javascript" %>
var myVar = '<%=request.getContextPath() %>';
alert(myVar);

You forgot to out put = before request and remove ; 你忘了在请求之前输出=并删除; after getContextPath() 在getContextPath()之后

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM