gpt4 book ai didi

java - 我如何将 JsonArray 从 jsp 解析为 Javascript

转载 作者:行者123 更新时间:2023-11-30 03:42:19 37 4
gpt4 key购买 nike

这是我的代码:

显示.jsp:

    <%
JSONArray jsonArray = new JSONArray();
String location = request.getParameter("location");
Class.forName(OracleDriver.class.getName().toString());
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","ram","ram");
PreparedStatement preparedStatement = connection.prepareStatement("select * from job where location = ?");
preparedStatement.setString(1,location);
ResultSet resultSet = preparedStatement.executeQuery();
int index = 0;
if(!resultSet.next()){
jsonArray.add(0, new Job("NA","NA","NA","NA","NA"));
out.println(jsonArray);
}else{
while(resultSet.next()){
index ++;
Job job = new Job(resultSet.getString("title"),resultSet.getString("location"),resultSet.getString("eligibility"),resultSet.getString("position"),resultSet.getString("pdate"));
jsonArray.add(index, job);
}
out.println(jsonArray);
}

%>

包含 Job 对象列表的 jsonArray,我如何在javascript中解析jsonArray

任何想法...

最佳答案

一个建议可能是将隐藏的 dom 元素附加到 JavaScript 变量,例如 p 或隐藏输入。

然后,将内部 html 设置为您的 json 数组

<p id="myJsonArray" style="display:none;"> <%= jsonArray %> </p>

最后,附加一些事件,以触发 javascript 函数来读取该元素的内部 html

...如果您希望自动执行,可能是一个定时间隔或其他内容来检查该元素是否发生更改.

老实说,我会忘记所有这些,只编写一个 Web 服务(使用 Jersey 或其他东西),然后创建一个 ajax 调用来获取你想要的:)但是,这是我的意见。

如果您需要任何进一步的想法,请告诉我!

Here's some working code. I would still like to emphasise that this is likely not the best approach. Having this much code mixed within a html page is very hard to maintain, and is not readable. I found this linked to on another StackOverflow post. It's worth a read. https://netbeans.org/kb/docs/javaee/ecommerce/design.html#architecture
The StackOverflow post: JSF vs Facelets vs JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<%@ page import="java.util.List,java.util.ArrayList,org.json.JSONArray,java.util.Arrays" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>test</title>
<!-- DataTables CSS -->
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.3/css/jquery.dataTables.css">

<!-- jQuery -->
<script type="text/javascript" charset="utf8" src="//code.jquery.com/jquery-1.10.2.min.js"></script>

<!-- DataTables -->
<script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.3/js/jquery.dataTables.js"></script>

<script>
$(function(){
//If your data won't change, you could just draw the table immediately after the page loads.
var testTable = $("#dynamicTable").DataTable({
data : JSON.parse($("#hiddenJson").html())
});

//If you're going to be updating data dynamically, you might
$("#refresh").click(function(){
var data = JSON.parse($("#hiddenJson").html());

testTable.clear();
var modifiedData;
for(r in data) {
modifiedData = data[r];
modifiedData[0] += " dynamically added";
modifiedData[1] += " dynamically added";
modifiedData[2] += " dynamically added";
testTable.row.add(modifiedData);
};
testTable.draw();
});
});

</script>
</head>
<body>
<%

JSONArray jsonArray = new JSONArray();
String location = request.getParameter("location");
// Class.forName(OracleDriver.class.getName().toString());
// Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","ram","ram");
// PreparedStatement preparedStatement = connection.prepareStatement("select * from job where location = ?");
// preparedStatement.setString(1,location);
// ResultSet resultSet = preparedStatement.executeQuery();
//int index = 0;
//if(!resultSet.next()){
// jsonArray.add(0, new Job("NA","NA","NA","NA","NA"));
// out.println(jsonArray);
//}else{
// while(resultSet.next()){
// index ++;
// Job job = new Job(resultSet.getString("title"),resultSet.getString("location"),resultSet.getString("eligibility"),resultSet.getString("position"),resultSet.getString("pdate"));
// jsonArray.add(index, job);
// }
jsonArray.put(0, ("NA,NA,NA").split(","));
jsonArray.put(1, ("NA2,NA2,NA2").split(","));
jsonArray.put(2, ("NA3,NA3,NA3").split(","));

%>

<p id="hiddenJson" style="display:none;"> <%= jsonArray.toString() %> </p>

<table id="dynamicTable">
<thead>
<th>column 1</th>
<th>column 2</th>
<th>column 3</th>
</thead>
</table>

<button id="refresh"> Update Data </button>
</body>
</html>

关于java - 我如何将 JsonArray 从 jsp 解析为 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26569907/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com