如何在 JavaScript 中使用 JSP 标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2131608/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:52:30  来源:igfitidea点击:

How to use JSP tags in JavaScript?

javascriptjsp

提问by user254582

How to use JSP tags in JavaScript file?

如何在 JavaScript 文件中使用 JSP 标签?

Is there any way?

有什么办法吗?

回答by BalusC

JSP is a view technology which runs at the server side which you can use to write template text like HTML/CSS/JS in. You can use JSP taglibs and EL to control the page flow and output dynamically using Java code. This also concerns the JavaScript content/output. Once JSP has run, it basically produces a HTML page and sends it to the client side. You can use JSP to dynamically output back-end data as if it are JavaScript variables. For example:

JSP 是一种运行在服务器端的视图技术,您可以使用它来编写 HTML/CSS/JS 等模板文本。您可以使用 JSP 标签库和 EL 来控制页面流并使用 Java 代码动态输出。这也涉及 JavaScript 内容/输出。一旦 JSP 运行,它基本上会生成一个 HTML 页面并将其发送到客户端。您可以使用 JSP 动态输出后端数据,就像它是 JavaScript 变量一样。例如:

<script>
    var foo = '${someBean.someProperty}';
</script>

Once the HTML page (with CSS/JS inside) has arrived at the client side (rightclick page and view source, you won't see any line of Java/JSP code), then the HTML will start to be interpreted, the CSS will start to be applied and the JS will start to be executed. There's no means of Java/JSP code at the client side. If you view the generated source in the client, the above example would now look like:

一旦 HTML 页面(内含 CSS/JS)到达客户端(右键单击页面并查看源代码,您将看不到任何一行 Java/JSP 代码),然后 HTML 将开始被解释,CSS 将开始应用,JS 将开始执行。客户端没有 Java/JSP 代码。如果您在客户端查看生成的源代码,上面的示例现在看起来像:

<script>
    var foo = 'somePropertyValue';
</script>

This way JavaScript has just instant access to server-side variables.

这样 JavaScript 就可以即时访问服务器端变量。

Now the other way round; the only way to let JavaScript access/invoke Java/JSP code is to actuallysend a HTTP request to the server side. This can be done several ways: doing a window.locationto do a synchronous GET request, or doing a form.submit()to do a synchronous GET or POST request, or doing a XMLHttpRequest#send()to do an asynchronous request (also known from Ajax).

现在反过来;让 JavaScript 访问/调用 Java/JSP 代码的唯一方法是实际向服务器端发送 HTTP 请求。这可以通过多种方式完成:执行 awindow.location以执行同步 GET 请求,或执行 aform.submit()执行同步 GET 或 POST 请求,或执行 aXMLHttpRequest#send()执行异步请求(也称为 Ajax)。

Alternatively you can also just let JavaScript set a (hidden) input field of a form so that it "automatically" get taken with the form submit whenever the user submits the form. Either way, the Java/JSP code at the server side will then be able to access JavaScript-controlled values the usual request parameter way.

或者,您也可以让 JavaScript 设置表单的(隐藏)输入字段,以便在用户提交表单时“自动”获取表单提交。无论哪种方式,服务器端的 Java/JSP 代码都将能够以通常的请求参数方式访问 JavaScript 控制的值。

To learn more about the wall between Java/JSP and JavaScript you may find this articleuseful.

要了解有关 Java/JSP 和 JavaScript 之间的壁垒的更多信息,您可能会发现这篇文章很有用。

回答by Vinodh Ramasubramanian

I have wondered about this when I have values in my session or context I wanted exposed in the client side. I create a jsp file with javascript mime type which just include global variable values. Which I then include at the top of my page and reuse the values where necessary.

当我想在客户端公开我的会话或上下文中的值时,我想知道这一点。我创建了一个 javascript mime 类型的 jsp 文件,它只包含全局变量值。然后我将其包含在我的页面顶部并在必要时重用这些值。

for eg:

例如:

**globalVar.jsp** 

var ctxPath = "<%=request.getContextPath()%>";

**script.js**

ajaxURL = ctxPath + "/path/to/call?param=value";

You can even namespace this as outlined here

您甚至可以按照此处概述的那样命名

回答by Ignacio Vazquez-Abrams

Yes, you can use JSP to generate JavaScript to send to the browser. Just point the <script>tag to a URL that leads to a JSP page that sets the MIME type of the response to "text/javascript".

是的,您可以使用 JSP 生成 JavaScript 以发送到浏览器。只需将该<script>标记指向一个 URL,该 URL 会指向一个 JSP 页面,该页面将响应的 MIME 类型设置为“text/javascript”。

No, you cannot use JSP tags in JavaScript in the browser. JSP is a server-side technology, which means that processing must be done on the server.

不,您不能在浏览器的 JavaScript 中使用 JSP 标记。JSP 是一种服务器端技术,这意味着处理必须在服务器上完成。

回答by Oluoch

You can easily use java tags to assign to a java variable which is later invoked inside the javascript code.

您可以轻松地使用 java 标记分配给 java 变量,该变量稍后在 javascript 代码中调用。

<% String name="Peter" %>

<% String name="Peter" %>

and then in javascript..

然后在javascript中..

<script type="text/javascript">

var _name = <%= name %>

var _name = <%= 名称 %>

回答by Raman Sahasi

@BalusC answer explain's well what is server-side and client-side programming. However I would like to highlight a point that if you really want to get run jspwithin your javascript, then you can give it file extension .jspinstead of .jsbecause what determines whether a file is a javascriptfile or not is what MIME media type. You can set MIME from JSP using:

@BalusC 的回答很好地解释了什么是服务器端和客户端编程。但是我想强调一点,如果你真的想要得到的运行jsp你内javascript,那么你可以给它的文件扩展名.jsp,而不是.js因为什么决定文件是否是一个javascript文件或也不是什么MIME媒体类型。您可以使用以下方法从 JSP 设置 MIME:

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

and now you can use jspwithin your javascriptlike:

现在你可以jsp在你javascript喜欢的范围内使用:

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

and now, you can link directly to it as:

现在,您可以直接链接到它:

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