首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

在struts2中怎么将一个Arraylist中的所有数据值显示到jsp页面上。

2012-01-18 
在struts2中如何将一个Arraylist中的所有数据值显示到jsp页面上。在线等!!!!!!!小弟做了个表的映射,初始化

在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();
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;
}

}
[解决办法]
把你的
Content cont = new Content(); 
放到while(rs.next()) 中去.
[解决办法]
这不是struts2的问题,是你没有搞清楚java的引用关系,ArrayList里面存放的是cont的引用,而这些引用都指引的是同一片地址控件,所以每次你会把ArrayList里所有的值全部更改。

请把Content cont = new Content(); 放到while循环的里面,即
while(rs.next())

Content cont = new Content(); 
......
[解决办法]

探讨
这不是struts2的问题,是你没有搞清楚java的引用关系,ArrayList里面存放的是cont的引用,而这些引用都指引的是同一片地址控件,所以每次你会把ArrayList里所有的值全部更改。

请把Content cont = new Content(); 放到while循环的里面,即
while(rs.next())
{
    Content cont = new Content();
    ......

[解决办法]
探讨
那在struts2是否还有其他方法呀


[解决办法]
探讨
引用:

那在struts2是否还有其他方法呀



什么意思

[解决办法]
不知道楼主解决没有哦,用上面的方法,你其实可以在你的页面刷新的时候,在你的java里面设置一下断点,你就知道你取得的值,到底是多少,这样你也能够自己解决问题,
因为,如果你取得了所有,你肯定就知道,不过问个小问题,楼主知道简单调试不?如果知道的话,立即就能够解决了,还有,如果你知道上面代码的流程中的意思的话,也能够解决,因为,你实例化他放到了while的外面,这样,只是一次,而,你每次都需要取出下一条值,所以必须每次都来,,
不知道明白没,

[解决办法]
你的页面想得到所有的值的话,你就不要这样写了,你直接写一个查询语句就可以了,然后用一个List findByAll()的方法,把它都找出来,这样就ok了,然后在页面直接用一个iterator 指向这个List的一个实例list,,里面设置上property 就OK了,

热点排行