获取用户在JSP中选择的值,并将值传递给java类(Struts2)

时间:2015-09-16 08:47:18

标签: java database jsp struts2

我使用Struts2来创建函数。获取公司的名称和 来自数据库的业务代码并使其成为下拉列表。用户  必须选择公司的名称,业务代码之后,我想将用户选择的值发送给java  类。

selectCondition.jsp

<s:form action="ResultAction" method="post">
    <!-- Company Name -->
    <s:select name="company" list="companyList" listKey="companyId" 
        listValue="companyName" headerKey="0" headerValue="--- Company ---" label="Conpany"/>

    <!-- Business Code -->
    <s:select name="busiCode" list="busiCodeList" listKey="busiCodeId" 
        listValue="busiCodeName" headerKey="0" headerValue="--- Business Code ---" label="Business Code" />
</s:form>

SelectConditionAction.java

public class SelectConditionAction extends ActionSupport {  
    public String populate() {
    //Company
    companyList = new ArrayList<Company>();

    Statement statement = null;
    Connection conn = null;

    try {
        conn = DBUtils.getInstance().getConnection();
        statement = conn.createStatement();
        ResultSet rs = statement.executeQuery("SELECT comp_id, comp_name FROM COMPANY ORDER BY comp_id ASC");

        while(rs.next()) {
            int index = 0;
            String companyName = "";
            Company company = new Company(index, companyName);
            index = rs.getInt("comp_id");
            companyName = rs.getString("comp_name");
            company.setCompanyId(index);
            company.setCompanyName(companyName);
            companyList.add(company);
            //System.out.println(index + " " + companyName);
        }
        rs.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();    

        try {
            conn.rollback();
        } catch(SQLException e1) {
            e1.printStackTrace();
        }
    }

    //Business Code
    busiCodeList = new ArrayList<BusiCode>();

    Statement statement1 = null;
    Connection conn1 = null;

    try {
        conn1 = DBUtils.getInstance().getConnection();
        statement1 = conn1.createStatement();
        ResultSet rs = statement1.executeQuery("SELECT busi_id, busi_name FROM Business ORDER BY busi_id ASC");

        while(rs.next()) {
            int index = 0;
            busiCode = "";
            BusiCode busicode = new BusiCode(index, busiCode);
            index = rs.getInt("busi_id");
            busiCode = rs.getString("busi_name");
            busicode.setBusiCodeId(index);
            busicode.setBusiCodeName(busiCode);
            busiCodeList.add(busicode);
            //System.out.println(index + " " + busiCode );
        }
        rs.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();    

        try {
            conn.rollback();
        } catch(SQLException e1) {
            e1.printStackTrace();
        }
    }
    return "populate";
}

public String execute() {
    return SUCCESS;
}

//Company
public String getCompany() {
    return company;
}

public void setCompany(String company) {
    this.company = company;
}

public ArrayList<Company> getCompanyList() {
    return companyList;
}

public void setCompanyList(ArrayList<Company> companyList) {
    this.companyList = companyList;
}

//Business Code
public String getBusiCode() {
    return busiCode;
}

public void setBusiCode(String busiCode) {
    this.busiCode = busiCode;
}

public ArrayList<BusiCode> getBusiCodeList() {
    return busiCodeList;
}

public void setBusiCodeList(ArrayList<BusiCode> busiCodeList) {
    this.busiCodeList = busiCodeList;
}
}

公司信息示例

public class Company {

private int companyId;

private String companyName;

public Company(int companyId, String companyName) {
    this.companyId = companyId;
    this.companyName = companyName;
}

public int getCompanyId() {
    return companyId;
}
public void setCompanyId(int companyId) {
    this.companyId = companyId;
}
public String getCompanyName() {
    return companyName;
}
public void setCompanyName(String companyName) {
    this.companyName = companyName;}

如何获取用户在JSP中选择的值并将这些值传递给java类?

0 个答案:

没有答案