如何从 JSP 访问 javascript 中的 java 对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6577246/
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 access a java object in javascript from JSP?
提问by sara
I am having a dropdown box in JSP, listing java object (accessing the object through MVC controller addAttribute). Now, on selection of an option from the dropdown box, I would like to display the selected employee's other details (example - ${employee.employeeCV} , ${employee.employeeName}) in a div. I have a javascript function for that (displayCV()). But not sure how to do this.
我在 JSP 中有一个下拉框,列出了 java 对象(通过 MVC 控制器 addAttribute 访问对象)。现在,在从下拉框中选择一个选项时,我想在 div 中显示所选员工的其他详细信息(例如 - ${employee.employeeCV} 、 ${employee.employeeName})。我有一个 javascript 函数(displayCV())。但不确定如何做到这一点。
JSP -
JSP -
<c:forEach items="${employees}" var="employee">
<option value="${employee.id}" onclick="displayCV();">
${employee.employeeName}
</option>
</c:forEach>
<b>CV:</b>
JAVASCRIPT -
JAVASCRIPT -
function displayCV() {
var valueSelected = $('#employeeList').val();
var div = $('#candidateDiv');
}
Could anybody help me with this please?
有人可以帮我吗?
回答by LeleDumbo
You can't access Java classes directly from JavaScript, you have to use some kind of web service communication between the JavaScript (client) and Java (server), you can make use of onchange event which will send a request to the server to return XML/JSON which you can parse to get the data (I see you're using jQuery, it has parseJSON method already) and update the corresponding node in the DOM.
您不能直接从 JavaScript 访问 Java 类,您必须在 JavaScript(客户端)和 Java(服务器)之间使用某种 Web 服务通信,您可以利用 onchange 事件将请求发送到服务器以返回您可以解析 XML/JSON 以获取数据(我看到您正在使用 jQuery,它已经有 parseJSON 方法)并更新 DOM 中的相应节点。
Another easier though not multi user friendly (because it can't detect updates) is to "convert" the Java object to JavaScript and update the data using that object (still using onchange). Something like:
另一个更简单但不是多用户友好的(因为它无法检测更新)是将 Java 对象“转换”为 JavaScript 并使用该对象更新数据(仍然使用 onchange)。就像是:
// this is JavaScript code written in the JSP
var employees = {
<c:forEach items="${employees}" var="employee">
"${employee.id}": {
name:"${employee.employeeName}",
cv:"${employee.employeeCV}",
},
</c:forEach>
}
now when JSP parse this, it would generate, for instance:
现在当 JSP 解析这个时,它会生成,例如:
var employees = {
"1": {
name:"foo",
cv:"cv1",
},
"2": {
name:"bar",
cv:"cv2",
},
}
回答by Adeel Ansari
Meta of what LeleDumbosaid herealready.
First, you must understand that JSP is a server-side view technology, whereas JavaScript typically runs on the client (browser).
首先,您必须了解 JSP 是一种服务器端视图技术,而 JavaScript 通常运行在客户端(浏览器)上。
Now, how you solve the problem in hand. So, you can make an AJAX request from your JavaScript, which will fetch you the data in JSON/XML format. Then, you can present that data in the browser using JavaScript.
现在,你如何解决手头的问题。因此,您可以从 JavaScript 发出 AJAX 请求,该请求将以 JSON/XML 格式获取数据。然后,您可以使用 JavaScript 在浏览器中显示该数据。
Further reading:
进一步阅读:
回答by Devram Kandhare
Call the function on onchangeevent of select instead of onclickof oprions. and use
在select 的onchange事件上调用函数,而不是在 oprions的onclick上调用。并使用
document.getElementById('GrdDamagedstock_tplRowEdit_ctl00_cmbFromBin').options[ele.options.selectedIndex].innerHTML;
to get selected value.
获取选定的值。