struts2框架从数据库批量取得数据集并在前台页面循环显示
兩天的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,功能已經實現了,代碼也比較詳細,希望能對你有所幫助。
總結
以上是生活随笔為你收集整理的struts2框架从数据库批量取得数据集并在前台页面循环显示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 四种参数传递的形式——URL,超链接,j
- 下一篇: request.getParameter