当我将它初始化为 30 时,为什么我的数组列表大小为零?
我java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
在调用 addRecord() 时得到
(注意:从 jsp 调用 setInitialValues 没有帮助。)
(注意:ShootRecord 也实现了 Serializable)
Servlet 进程请求方法
protected void processRequest(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter pw = resp.getWriter();
String address = null;
HttpSession session = req.getSession();
ShootMeet meetBean = (ShootMeet)session.getAttribute("shootmeetbean");
if(meetBean == null){
pw.print("initialising meet \n");
meetBean = new ShootMeet();
meetBean.setInitialValues(30);
}
ShootRecord recordSent = (ShootRecord)session.getAttribute("shootrecordbean");
if(recordSent == null){
recordSent = new ShootRecord();
}
// **record sent created here**
try{ meetBean.addRecord(recordSent);
} ...
}
// doGet and doPost call processRequest
拍摄见面会
public class ShootMeet implements Serializable{
private ArrayList<ShootRecord> listOfRecords;
private String date;
private int numTargets;
public ShootMeet(){}
public void setInitialValues(int numTarg){
numTargets = numTarg;
listOfRecords = new ArrayList<ShootRecord>(numTargets);
}
public void addRecord(ShootRecord s){
if(listOfRecords.size() == 0){
System.out.println("size of list of records is zero before adding record");
}
listOfRecords.add(s.targetNumber, s);
}
//...other setters/getters
}
索引.jsp
<%@page import="servlet.ShootRecord"%>
<%@page import="servlet.ShootRecordMap"%>
<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="shootmeetbean" class="servlet.ShootMeet" scope = "session"/>
<%--<jsp:setProperty name = "shootmeetbean" property = "initialValues" value = "1"/>
</jsp:useBean>--%>
<jsp:useBean id="shootrecordbean" class="servlet.ShootRecord" scope = "session"/>
<html>
// ... jstl tags
</html>