小弟做了个表的映射,初始化后对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>

解决方案 »

  1.   

    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;
              }
         
        } 
      

  2.   

    把你的
    Content cont = new Content(); 
    放到while(rs.next()) 中去.
      

  3.   

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

         Content cont = new Content(); 
        ......
      

  4.   

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

  5.   

    不是struts2的问题,他管不着的
      

  6.   


    意思就是struts2是管不了DAO的. 
      

  7.   

    就是利用struts2标签库是否可以传值
      

  8.   

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

  9.   

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

  10.   

    我可能理解错了,因为还没有问你的具体的要求是什么呢,呵呵,
    如果你就仅仅是显示出来,这个没有问题,直接一个list,没问题,而且效果也还可以,如果你还有其他的什么,就应该具体分析了,
      

  11.   

    这里给你写一个,你做一个interface()
    里面用声明一个方法(这里可以是一个dao类,),List findByAll(String ***);/或者是不用,因为你直接使显示,你就直接用List findByAll();,
    其次,在一个具体的(这里可以是一个impl类),具体的implements,这个接口,然后具体的实现,也就是,也就是select * from ×××;然后执行出来,返回一个List,比如:
    public List findByAll(String serviceName,int cltNodeId) {
    // TODO Auto-generated method stub
    String sql = "select * from NET_CLT_PARA_TBL where SERVICE_NAME = ? and CLT_NODE_ID = ? ";
    return getJdbcTemplate().queryForList(sql, new Object[] {serviceName,cltNodeId});
    },这里我用到了spring的东西,你可以不用,只要你查找出数据return就可以了,方法可以随便用,其次,就是action了,(这里过程有个转化,因为这里用到的是int,string,之类的,而前台全部都是string,所以需要new integer一下,大小写我就不顾及了,你应该能够理解,然后就在你的execute当中 来一个setList(类的实例。findByAll());就可以了,
    然后在前台的jsp当总,你就直接用上面的,就可以得到,