将变量从 Scriptlets 传递给 Javascript。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9699202/
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
Passing a variable from Scriptlets to Javascript.
提问by Shantanu Tomar
I have this code snippet ::
我有这个代码片段::
<script type="text/javascript">
function gotoa(){
<%!
public void a(){
String temp1;
PopulateTextbox obj = new PopulateTextbox();
temp1 = obj.method();
request.setAttribute("variable", temp1);
}
%>
var myVar = <%=request.getAttribute("variable")%>
}
</script>
What i want to do is to get the value of variable temp1 in my JavaScript function gotoa(). In this particular code i am getting an error invalid request
我想要做的是在我的 JavaScript 函数 gotoa() 中获取变量 temp1 的值。在这个特定的代码中,我收到一个错误无效请求
request.setAttribute("variable", temp1);
My main aim is to call the function a() on some button click event so that my script let code runs again and fresh values are being passed in variable temp1. which will then passed on to gotoa() to act as a source for my data grid(not in this code). basically i want to refresh grid on some button click. Am i doing the right way. Please help. Thanks.
我的主要目标是在某个按钮单击事件上调用函数 a(),以便我的脚本让代码再次运行,并在变量 temp1 中传递新值。然后将传递给 gotoa() 作为我的数据网格的源(不在此代码中)。基本上我想在点击一些按钮时刷新网格。我做得对吗。请帮忙。谢谢。
回答by Ameya
When you need value of variable temp1
inside gotoa()
do the following:
当您需要temp1
内部变量的值时,gotoa()
请执行以下操作:
<% String temp1; // Store value in temp1 variable for later use
PopulateTextbox obj = new PopulateTextbox();
temp1 = obj.method();
%>
<script>
function gotoa(){
var temp1Val = document.getElementById("hiddenTemp1").value;
// put your logic here
document.getElementById("hiddenTemp1").value = tempVal1;
}
</script>
<body>
<form action="otherPage.jsp">
<!-- use the value of temp1 variable -->
<input type="hidden" name="hiddenTemp1" id="hiddenTemp1" value="<%=temp1%>">
<input type="button" onclick="gotoa()" value="GotoA">
<input type="submit" value="Submit New Value">
</form>
</body>
First you assign the value to variable temp1. And then you Render your JSP with a Hidden Input component with value=temp1 by using scriptlet. If you want to verify, just View the Source of generated HTML and you should see the value of input hidden equal to the variable.
首先,您将值分配给变量 temp1。然后使用 scriptlet 使用 value=temp1 的隐藏输入组件呈现 JSP。如果要验证,只需查看生成的 HTML 的源代码,您应该看到 input hidden 的值等于变量。
When the form is submitted the value of hiddenTemp1 will be available in Request. If you intend to change the value of this hidden component, you can set the value back in the component.
提交表单时,hiddenTemp1 的值将在请求中可用。如果您打算更改此隐藏组件的值,您可以在组件中重新设置该值。
回答by Sundhar
First let me tell you couple of things i have observed in this
首先让我告诉你我在这方面观察到的几件事
1) Setting and getting from request needs a page submission otherwise it will not be available in the parameter
1)设置和获取request需要页面提交否则在参数中不可用
2) Scriplet and jsp compiles in different ways, since your scriplet compiliation always happens (no matter where it is header body or footer) first
2)Scriplet和jsp编译方式不同,因为你的scriplet编译总是先发生(不管是header body还是footer)
Now the suggestions for how we can do this
现在关于我们如何做到这一点的建议
1) Use a EJB object instead of request object
1) 使用 EJB 对象而不是请求对象
2) Use a hidden input tag to set and get the value needed, assign the getter method to the value of input tag some like ', and when you need to varaible to be changed you need to submit the form, if you do not need to whole page to be reloaded refer ajax methods to change the value alone without reloading the page
2) 使用隐藏的输入标签设置和获取需要的值,将getter方法分配给输入标签的值,比如',当你需要改变变量时,你需要提交表单,如果你不需要要重新加载整个页面,请参考 ajax 方法来单独更改值而不重新加载页面