两天的Struts2课程实训终于结束了,现在网上Struts2的资料还比较少,一些重要的用法还是Mark一下的好:
从数据库批量取得数据,并在前台页面中用表格循环输出显示
1,一定要定义实体类 比如glyuan类和gylou类代码如下
package com.entity;
public class glyuan {
private String account = null;
private String name = null;
private String gylou = null;
private String descrip = null;
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGylou() {
return gylou;
}
public void setGylou(String gylou) {
this.gylou = gylou;
}
public String getDescrip() {
return descrip;
}
public void setDescrip(String descrip) {
this.descrip = descrip;
}
}
package com.entity;
public class gylou {
private int num;
private String name = null;
private int loucengshu;
private int fangjianshu;
private int chuangweishu;
private String descrip = null;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLoucengshu() {
return loucengshu;
}
public void setLoucengshu(int loucengshu) {
this.loucengshu = loucengshu;
}
public int getFangjianshu() {
return fangjianshu;
}
public void setFangjianshu(int fangjianshu) {
this.fangjianshu = fangjianshu;
}
public int getChuangweishu() {
return chuangweishu;
}
public void setChuangweishu(int chuangweishu) {
this.chuangweishu = chuangweishu;
}
public String getDescrip() {
return descrip;
}
public void setDescrip(String descrip) {
this.descrip = descrip;
}
}
2,在Action中执行逻辑,从数据库取出数据,并注入
package com.Action;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.entity.*;
import com.opensymphony.xwork2.ActionSupport;
public class SelSuperAdminInfo extends ActionSupport {
public List<glyuan> list;//管理员信息表格list
public List<gylou> list1;//公寓楼信息表格list
public List gyloulist;//添加公寓管理员时的授权公寓楼list
public List<glyuan> Infolist = new ArrayList<glyuan>();
public List<gylou> Infolist1 = new ArrayList<gylou>();
public List gylouInfo = new ArrayList();//临时的公寓楼名称list
public List getGyloulist() {
return gyloulist;
}
public void setGyloulist(List gyloulist) {
this.gyloulist = gyloulist;
}
public List<gylou> getList1() {
return list1;
}
public void setList1(List<gylou> list1) {
this.list1 = list1;
}
public List<glyuan> getList() {
return list;
}
public void setList(List<glyuan> list) {
this.list = list;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
Connection con = DatabaseConnection.getConnection();
//查询管理员信息
String sql = "select * from glyuan_tb";
Statement ps = con.createStatement();
ResultSet rs = ps.executeQuery(sql);
while (rs.next()) {
glyuan man = new glyuan();
man.setAccount(rs.getString("account"));
man.setName(rs.getString("name"));
man.setGylou(rs.getString("gylou"));
man.setDescrip(rs.getString("descrip"));
Infolist.add(man);
}
//注入
this.setList(Infolist);
//查询公寓楼名称信息
String sql1 = "select name from gylou_tb";
ResultSet rs1 = ps.executeQuery(sql1);
while (rs1.next()) {
gylouInfo.add(rs1.getString("name"));
}
//注入
this.setGyloulist(gylouInfo);
//查询公寓楼信息
String sql2 = "select * from gylou_tb";
ResultSet rs2= ps.executeQuery(sql2);
while (rs2.next()) {
gylou lou = new gylou();
lou.setNum(rs2.getInt("num"));
lou.setName(rs2.getString("name"));
lou.setLoucengshu(rs2.getInt("loucengshu"));
lou.setFangjianshu(rs2.getInt("fangjianshu"));
lou.setChuangweishu(rs2.getInt("chuangweishu"));
lou.setDescrip(rs2.getString("descrip"));
Infolist1.add(lou);
}
//注入
this.setList1(Infolist1);
return SUCCESS;
}
}
3,注入后即可在前台页面中使用,用迭代器循环输出,代码如下:
<table width="650" height="296" border="1">
<tr>
<td colspan="4"><strong>公寓管理员信息</strong></td>
</tr>
<tr>
<td>账号</td>
<td>姓名</td>
<td>所辖公寓楼</td>
<td>描述</td>
</tr>
<s:iterator value="list" status="st">
<tr>
<td><s:property value="account"/></td>
<td><s:property value="name"/></td>
<td><s:property value="gylou"/></td>
<td><s:property value="descrip"/></td>
</tr>
</s:iterator>
</table>
-----------------------------------------------------------------------------------------------------------------
<table width="650" height="296" border="1">
<tr>
<td colspan="6"><strong>公寓楼信息</strong></td>
</tr>
<tr>
<td>楼号</td>
<td>名称</td>
<td>楼层数</td>
<td>房间总数</td>
<td>床位总数</td>
<td>描述</td>
</tr>
<s:iterator value="list1" status="st">
<tr>
<td><s:property value="num"/></td>
<td><s:property value="name"/></td>
<td><s:property value="loucengshu"/></td>
<td><s:property value="fangjianshu"/></td>
<td><s:property value="chuangweishu"/></td>
<td><s:property value="descrip"/></td>
</tr>
</s:iterator>
</table>
OK,功能已经实现了,代码也比较详细,希望能对你有所帮助。