日期:2014-05-18  浏览次数:20678 次

在struts2中如何将一个Arraylist中的所有数据值显示到jsp页面上。在线等!!!!!!!
小弟做了个表的映射,初始化后对ArrayList进行强制转化.可是传到页面上后只显示一行数据(将表中最后一行数据重复),其他都丢失了?以下是代码,小弟是新手请多多包涵;在线等!!
是否还有其他的方法?
表映射的代码:
public class Content {
private String name;
private String content;
private String time;
  public String getname(){ 
return name;
  }
  public void setname(String name){
this.name = name;
  }
  public String getcontent(){
return content;
  }
  public void setcontent(String content){
this.content = content;
  }
  public String gettime(){
return time;
  }
  public void settime(Date time){
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  String timeString = formatter.format(time);
this.time = timeString;
 
  }

这是从数据库中取数并存到ArrayList中
public String execute() throws Exception {
DataConnection da = new DataConnection();
int i = da.OpenConn();
Content cont = new Content();
ResultSet rs = null;// here ,it is only used to identify wether the password and name are correct

String sql = "select * from content";
System.out.println(i);
rs = da.SqlExecute(sql);
int j = 1;//here, i have to pay attention to 'Column Index out of range, 0 < 1.'
int k = 0;
while(rs.next())
{
cont.setname(rs.getString("name"));
cont.settime(rs.getDate("time"));
//here ,it could happen that the data may make collision just pay attention to it .this is a very important method
cont.setcontent(rs.getString("content"));
System.out.println("this is the current time "+cont.gettime());
arr.add(cont);
 
cont=(Content)arr.get(k);
System.out.println("this is"+cont.getcontent());
 
k++;
}
System.out.println("the number is " + k);
System.out.println("the number of the rs is" + arr.size());
da.closeConn();
   
  if(arr.size()>0)
  {
  HttpServletRequest Request = ServletActionContext.getRequest();
   
  System.out.println("check the number"+arr.size());
  System.out.print("success");
  Request.setAttribute("array", arr);
   
  return SUCCESS;
   
  } else {
  return ERROR;
  }
   
  } 
以下是jsp页面上的代码:
<s:form action="Show"> 
<%ArrayList arr = new ArrayList(); %>
<%arr = (ArrayList)request.getAttribute("array"); %>
<%for(int i=0; i<arr.size();i++) {%>
<%Content con =new Content(); %>
<% con = (Content)arr.get(i); %>
<%out.println(con.getname()); %>
<%out.println(con.getcontent()); %>
<%out.println(con.gettime()); %>
<%} %>

</s:form>


------解决方案--------------------
public String execute() throws Exception {
DataConnection da = new DataConnection();
int i = da.OpenConn();
Content cont = null;
ResultSet rs = null;// here ,it is only used to identify wether the password and name are correct

String sql = "select * from content";
System.out.println(i);
rs = da.SqlExecute(sql);
int j = 1;//here, i have to pay attention to 'Column Index out of range, 0 < 1.'
int k = 0;
while(rs.next())
{
con = new Content(