从复选框页面将参数传递到Proto页面时可以看到,它将其放入投资组合对象中,然后用于检查数据的值并显示投资组合名称,以及原型页面何时刷新参数不会从复选框页面再次传递,组合对象的空值存储在其中,从而产生空指针异常。
所以,如果我将参数值存储一旦它们已经从复选框页面传递出来就能解决问题吗?如果是这样我怎么能去做呢?
以下是原型页面的代码
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="oracle.jdbc.pool.OracleDataSource"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<head>
<title>Live Data Tracking</title>
<style type='text/css'>
.wrapper {
margin: 0px auto;
width: 1379px;
background-color: #cccccc
}
.header {
float: left;
width: 100%;
background-color: #356aa0
}
.left1 {
float: left;
margin-right: 10px;
width: 338px;
background-color: #ffffff
}
.left2 {
float: left;
margin-right: 10px;
width: 337px;
background-color: #ffffff
}
.left3 {
float: left;
margin-right: 10px;
width: 337px;
background-color: #ffffff
}
.right {
float: right;
width: 337px;
background-color: #ffffff
}
.footer {
float: left;
width: 100%;
background-color: #00457b
}
body {
padding: 0px;
margin: 0px;
font-size: 90%;
background-color: #e7e7de
}
</style>
</head>
<script type="text/javascript">
<%
ArrayList<ArrayList<String>> testList = new ArrayList();
portfolio = request.getParameterValues("portfolio");
try{
OracleDataSource ds= new OracleDataSource();
//javax.sql.DataSource ds = new javax.sql.DataSource();
ds.setDriverType("thin");
ds.setServerName("localhost");
ds.setPortNumber(1521);
ds.setDatabaseName("orcl");
ds.setUser("system");
ds.setPassword("amex1234");
Connection connection = ds.getConnection();
System.out.println("Connected Successfully");
Statement statement = connection.createStatement() ;
if (portfolio!=null)
{
for(int i=0;i<portfolio.length;i++)
testList.add(new ArrayList());
//System.out.println(testList);
for(int i=0; i<portfolio.length;i++)
{ String temp="";
ResultSet resultset = statement.executeQuery("select num FROM "+portfolio[i]+"_Test") ; //Put in SQl command and Table name here
while(resultset.next()){
temp = resultset.getString(1);
testList.get(i).add(temp);
}
//System.out.println(testList);
}
}
} catch(SQLException e) {
e.printStackTrace();
}
%>
</script>
<body>
<%! String[] portfolio; %>
<div class="wrapper">
<div class="header">
<center>
<h4>Live Tracker</h4>
</center>
</div>
<div class="left1">
<h3>Portfolio</h3>
<%
if (portfolio != null)
{
for (int i = 0; i < portfolio.length; i++) //i=portfolio length proxy
{
out.println ("<b>"+portfolio[i]+"<b>"); %>
<br>
<% }
}
else out.println ("<b>none<b>");
%>
</div>
<div class="left2">
<h3>Pre-Stage</h3>
for (int j = 0; j < portfolio.length; j++)
{
response.setIntHeader("Refresh", 2);
//System.out.println(testList.get(j).size());
if (testList.get(j).size()!=0){ %>
Data is there <br>
<% } else { %>
No data <br>
<% }
}%>
</div>
<div class="left3">
<h3>Stage</h3>
</div>
<div class="right">
<h3>Mirror</h3>
</div>
<div class="footer">Something Funny and Witty Here</div>
</div>
</body>
</HTML>
这是复选框页面的代码。通过此页面运行
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<body>
<FORM method="POST" ACTION="Proto_1.jsp">
<center>
Select your portfolio(s): <br><br>
<table>
<tr>
<td>
<input TYPE=checkbox name=portfolio VALUE=X>
</td>
<td>
X
</td>
</tr>
<tr>
<td>
<input TYPE=checkbox name=portfolio VALUE=Y>
</td>
<td>
Y
</td>
</tr>
<tr>
<td>
<input TYPE=checkbox name=portfolio VALUE=W>
</td>
<td>
W
</td>
</tr>
<tr>
<td>
<input TYPE=checkbox name=portfolio VALUE=V>
</td>
<td>
V
</td>
</tr>
<tr>
<td>
<input TYPE=checkbox name=portfolio VALUE=S>
</td>
<td>
S
</td>
</tr>
</table>
<br> <INPUT TYPE=submit name=submit Value="Submit">
</center>
</FORM>
</BODY>
</HTML>
答案 0 :(得分:1)
有几种方法可以解决。
选项1:将参数设置为会话。而不是:
portfolio = request.getParameterValues("portfolio");
试试这个:
String[] portfolio = null;
if(null == request.getParameter("submit")) { //page reload, true
portfolio = (String[])session.getAttribute("portfolio");
} else {
portfolio = request.getParameterValues("portfolio");
session.setAttribute("portfolio", portfolio);
}
选项2:分离视图和业务逻辑,即将页面拆分为两个。首先从请求获取portfolio
的jsp并经常调用以从第二个jsp获取更新的记录。这种方法需要JavaScript
。
View.jsp:使用setInterval()
经常调用Records.jsp
<script>
var getRecords = function() {
// to do here
};
setInterval(getRecords, 2 * 1000); (in miliseconds)[every 2 sec]
</script>
Records.jsp:生成动态html和记录
答案 1 :(得分:0)
您可以通过session.setAttribute(“portfolio”,portfolio)将复选框数据保存到会话中。 您可以检索此值并通过document.getElementById(id).value或通过jsp中的value = {$ sessionVariable}将值设置回页面