如何在 javascript 中使用 scriptlet

发布于 2024-10-20 04:57:26 字数 559 浏览 1 评论 0原文

有人可以测试这个例子并分享结果吗? 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薔薇婲 2024-10-27 04:57:26

该行代码必须放置在 .jsp 文件中的 HTML

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

请注意,<%= %> 是打印变量的正确语法,<% %> 不会这样做。 em>

或者,如果打算在独立的 .js 文件中提供服务,那么您需要将其重命名为 .jsp 并将以下内容添加到文件顶部(并相应地更改

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

这样,JspServlet 将对其进行处理,并且将指示浏览器将 JSP 响应正文解释为 JavaScript 而不是 HTML 。


与具体问题无关,请注意 scriptlet 被视为

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

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).

<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):

<%@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.


Unrelated to the concrete problem, note that scriptlets are considered poor practice. Use EL.

var myVar = '${pageContext.request.contextPath}';
血之狂魔 2024-10-27 04:57:26

听起来好像您将 JSP 代码放置在 JavaScript 页面中,或者至少放置在非 JSP 页面中。 Scriptlet 只能包含在 JSP 页面中(通常配置为 *.jsp)。

所提供的语句,如果由 JSP 编译器处理,将导致 myVar 等于 '' 作为您正在使用的 scriptlet 格式 <% ... %>在标记之间执行 Java 代码,但不返回结果。

因此,要使用此标记,您需要手动将值写入请求输出流。要获得所需的功能,您需要执行以下操作:

  make sure your code is in a JSP page
  use var myVar = '<%= request.getContextPath() %>'; (note the equals sign)

尽管如此,在大多数情况下,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:

  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.

凑诗 2024-10-27 04:57:26

您无法通过赋予正常的 .js 扩展名来在 javascript 中运行 scriptlet。但是,您可以为您的 .js 提供 .jsp 的文件扩展名,然后直接链接到它:

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

现在您可以在其中使用 jsp your javascript like:

var someMessage = "${someMessage}"
var anotherMessage = "${anotherMessage}"/>"

此操作完全有效,因为决定文件是否为 javascript 文件的是 MIME 媒体类型。您可以使用以下方法从 JSP 设置 MIME:

<%@ page contentType="text/javascript" %>

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:

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

and now you can use jsp within your javascript like:

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:

<%@ page contentType="text/javascript" %>
痕至 2024-10-27 04:57:26
var myVar = '<%=request.getContextPath() %>';
alert(myVar);

您忘记在请求之前输出 put = 并删除 ;在 getContextPath() 之后

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

You forgot to out put = before request and remove ; after getContextPath()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文