日期:2014-05-17  浏览次数:20837 次

在jsp页面中给List赋值
MemberStruts
Java code

package com.iconfluence.struts;

import com.iconfluence.dao.MemberInfoDao;
import com.opensymphony.xwork2.ActionSupport;

public class MemberStruts extends ActionSupport{

    private MemberInfoDao member = new MemberInfoDao();    
    
    public MemberInfoDao getMember() {
        return member;
    }
    public void setMember(MemberInfoDao member) {
        this.member = member;
    }
    public String execute(){
        String ssql = "select * from tb_memberinfo";
        try {
            member.setList(ssql);
        } catch (Exception e) {
            e.printStackTrace();
        }        
        return SUCCESS;
    }
}



MemberInfoDao
Java code


package com.iconfluence.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import src.Common;

import com.iconfluence.entity.MemberInfo;

    public void setList(String ssql) throws Exception {
        list = new ArrayList<MemberInfo>();
        Connection conn = ConnectionUtils.getConnection();
        Statement stmt = null;
        ResultSet rs = null;
        try {
            stmt = conn.createStatement();
            rs = stmt.executeQuery(ssql);
            while (rs.next()) {
                MemberInfo minfo = new MemberInfo();
                minfo.setId(rs.getInt("id"));
                minfo.setMembername(rs.getString("membername"));
                minfo.setJoindate(Common.formatDate(rs.getDate("joindate"), "yyyy-MM-dd"));
                minfo.setSurplus(rs.getFloat("surplus"));
                list.add(minfo);
            }
        }finally {
            if (rs != null){
                rs.close();
            }
            if(stmt != null){
                stmt.close();
            }
            if(conn != null){
                conn.close();
            }
        }
    }    
    public List<MemberInfo> getList(){
        return list;
    }


MemberInfo
Java code

/**
 * 
 */
package com.iconfluence.entity;

/**
 * @author Jiessie
 *
 */
public class MemberInfo{
    
    private Integer id;
    private String membername;
    private String joindate;
    private float surplus;        
    
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }    
    public String getMembername() {
        return membername;
    }
    public void setMembername(String membername) {
        this.membername = membername;
    }
    public String getJoindate() {
        return joindate;
    }
    public void setJoindate(String joindate) {
        this.joindate = joindate;
    }
    public float getSurplus() {
        return surplus;
    }
    public void setSurplus(float surplus) {
        this.surplus = surplus;
    }    
}


XML code

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">    
<struts>
    <package name="com.iconfluence.struts"  extends="struts-default">
        <action name="he" class="com.iconfluence.struts.MemberStruts">
            <result>consumeque