Javascript 如何在javascript中设置JSTL变量值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3287114/
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 set the JSTL variable value in javascript?
提问by Tony
How to set the JSTL variable value in java script?
如何在java脚本中设置JSTL变量值?
<script>
function function1()
{
var val1 = document.getElementById('userName').value;
<c:set var="user" value=""/> // how do i set val1 here?
}
</script>
How do I set the 'user' variable (JSTL) value from 'val1' (Java script)?
如何从“ val1”(Java 脚本)设置“用户”变量 ( JSTL) 值?
回答by Marimuthu Madasamy
It is not possible because they are executed in different environments (JSP at server side, JavaScript at client side). So they are not executed in the sequence you see in your code.
这是不可能的,因为它们在不同的环境中执行(服务器端的 JSP,客户端的 JavaScript)。因此,它们不会按照您在代码中看到的顺序执行。
var val1 = document.getElementById('userName').value;
<c:set var="user" value=""/> // how do i set val1 here?
Here JSTL code is executed at server side and the server sees the JavaScript/Html codes as simple texts. The generated contents from JSTL code (if any) will be rendered in the resulting HTML along with your other JavaScript/HTML codes. Now the browser renders HTML along with executing the Javascript codes. Now remember there is no JSTL code available for the browser.
这里 JSTL 代码在服务器端执行,服务器将 JavaScript/Html 代码视为简单文本。从 JSTL 代码(如果有)生成的内容将与您的其他 JavaScript/HTML 代码一起呈现在生成的 HTML 中。现在浏览器在执行 Javascript 代码的同时呈现 HTML。现在请记住,浏览器没有可用的 JSTL 代码。
Now for example,
现在例如,
<script type="text/javascript">
<c:set var="message" value="Hello"/>
var message = '<c:out value="${message}"/>';
</script>
Now for the browser, this content is rendered,
现在对于浏览器,此内容已呈现,
<script type="text/javascript">
var message = 'Hello';
</script>
Hope this helps.
希望这可以帮助。
回答by Cyril Deba
one more approach to use.
另一种使用方法。
first, define the following somewhere on the page:
首先,在页面上的某处定义以下内容:
<div id="valueHolderId">${someValue}</div>
then in JS, just do something similar to
然后在 JS 中,做类似的事情
var someValue = $('#valueHolderId').html();
it works great for the cases when all scripts are inside .js files and obviously there is no jstl available
它适用于所有脚本都在 .js 文件中并且显然没有可用的 jstl 的情况
回答by Vikram
<script ...
function(){
var someJsVar = "<c:out value='${someJstLVarFromBackend}'/>";
}
</script>
This works even ifyou dont have a hidden/non-hidden input field set somewhere in the jsp.
这个工程,即使你没有一个隐藏/非隐藏输入字段集在JSP的某个地方。
回答by Erik Reppen
Just don't. Don't write code with code. Write a JSON object or a var somewhere but for the love of a sensible HTTP divide, don't write JavaScript functions or methods hardcoded with vars/properties provided by JSTL. Generating JSON is cool. It ends there or your UI dev hates you.
只是不要。不要用代码写代码。在某处编写 JSON 对象或变量,但出于对合理 HTTP 划分的热爱,不要编写使用 JSTL 提供的变量/属性硬编码的 JavaScript 函数或方法。生成 JSON 很酷。到此结束,否则您的 UI 开发人员讨厌您。
Imagine if you had to dig into JavaScript to find something that was setting parameters in the middle of a class that originated on the client-side. It's awful. Pass data back and forth. Handle the data. But don't try to generate actual code.
想象一下,如果您必须深入研究 JavaScript 才能找到在源自客户端的类的中间设置参数的东西。这太可怕了。来回传递数据。处理数据。但不要尝试生成实际代码。
回答by mohiz tank
This variable can be set using value="${val1}"inside c:setif you have used jquery in your system.
如果您在系统中使用了 jquery,则可以使用value="${val1}"inside设置此变量c:set。
回答by Kronass
You have to use the normal string concatenation but you have to make sure the value is a Valid XML string, you will find a good practice to write XML in this source http://oreilly.com/pub/h/2127, or if you like you can use an API in javascript to write XML as helmafor example.
您必须使用普通的字符串连接,但必须确保该值是一个有效的 XML 字符串,您会发现在此源http://oreilly.com/pub/h/2127 中编写 XML 的好做法,或者如果例如,您可以使用 javascript 中的 API 将 XML 编写为helma。
回答by shreedhar bhat
As an answer I say No. You can only get values from jstl to javascript. But u can display the user name using javascript itself. Best ways are here. To display user name, if u have html like
作为答案,我说不。您只能从 jstl 获取值到 javascript。但是您可以使用 javascript 本身显示用户名。最好的方法在这里。显示用户名,如果你有像 html
<div id="uName"></div>
You can display user name as follows.
您可以按如下方式显示用户名。
var val1 = document.getElementById('userName').value;
document.getElementById('uName').innerHTML = val1;
To get data from jstl to your javascript :
要将数据从 jstl 获取到您的 javascript :
var userName = '<c:out value="${user}"/>';
here ${user} is the data you get as response(from backend).
这里 ${user} 是您作为响应(从后端)获得的数据。
Asigning number/array length
分配数字/数组长度
var arrayLength = <c:out value="${details.size()}"/>;
Advanced
先进的
function advanced(){
var values = new Array();
<c:if test="${empty details.users}">
values.push("No user found");
</c:if>
<c:if test="${!empty details.users}">
<c:forEach var="user" items="${details.users}" varStatus="stat">
values.push("${user.name}");
</c:forEach>
</:c:if>
alert("values[0] "+values[0]);
});
回答by Dariush Jafari
You can save the whole jstl object as a Javascript object by converting the whole object to json. It is possible by Hymansonin java.
您可以通过将整个对象转换为 json 来将整个 jstl 对象保存为 Javascript 对象。Hyman逊在java中是可能的。
import com.fasterxml.Hymanson.databind.ObjectMapper;
public class JsonUtil{
public static String toJsonString(Object obj){
ObjectMapper objectMapper = ...; // Hymanson object mapper
return objectMapper.writeValueAsString(obj);
}
}
/WEB-INF/tags/util-functions.tld:
/WEB-INF/tags/util-functions.tld:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<tlib-version>1.0</tlib-version>
<uri>http://www.your.url/util-functions</uri>
<function>
<name>toJsonString</name>
<function-class>your.package.JsonUtil</function-class>
<function-signature>java.lang.String toJsonString(java.lang.Object)</function-signature>
</function>
</taglib>
web.xml
网页.xml
<jsp-config>
<tablib>
<taglib-uri>http://www.your.url/util-functions</taglib-uri>
<taglib-location>/WEB-INF/tags/util-functions.tld</taglib-location>
</taglib>
</jsp-confi>
mypage.jsp:
我的页面.jsp:
<%@ taglib prefix="uf" uri="http://www.your.url/util-functions" %>
<script>
var myJavaScriptObject = JSON.parse('${uf:toJsonString(myJstlObject)}');
</script>

