概述
这个功能实现了:将两级目录从数据库读取并显示在浏览器上,遍历显示时用到了<s:iterator>标签遍历两层List
1.首先是数据库要用到的两张表
一级目录表(代码注释中的大类目)t_categoryone:
二级目录表(代码注释中的小类目)t_categorytwo:
2.对应的model层 两个:CategoryOne和CategoryTwo
CategoryOne {
private int CategoryOne_id;
private String CategoryOne_name;
public int getCategoryOne_id() {
return CategoryOne_id;
}
public void setCategoryOne_id(int categoryOne_id) {
CategoryOne_id = categoryOne_id;
}
public String getCategoryOne_name() {
return CategoryOne_name;
}
public void setCategoryOne_name(String categoryOne_name) {
CategoryOne_name = categoryOne_name;
}
}
public class CategoryTwo {
private int categoryTwo_id;
private String categoryTwo_name;
private int categoryOne_id;
public int getCategoryTwo_id() {
return categoryTwo_id;
}
public void setCategoryTwo_id(int categoryTwo_id) {
this.categoryTwo_id = categoryTwo_id;
}
public String getCategoryTwo_name() {
return categoryTwo_name;
}
public void setCategoryTwo_name(String categoryTwo_name) {
this.categoryTwo_name = categoryTwo_name;
}
public int getCategoryOne_id() {
return categoryOne_id;
}
public void setCategoryOne_id(int categoryOne_id) {
this.categoryOne_id = categoryOne_id;
}
}
3.Dao 层操纵数据库,代码如下:
BaseDAO为:
public class BaseDAO {
protected Connection conn = null;
protected ResultSet rs = null;
protected PreparedStatement pstmt = null;
protected int result;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void getConn() {
String url = "jdbc:mysql://localhost:3306/schoolo2o";
String user = "root";
String password = "wmywmy";
try {
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("数据库已连接");
}
public void closeAll() {
try {
if (pstmt != null && !pstmt.isClosed())
pstmt.close();
if (rs != null && !rs.isClosed())
rs.close();
if (conn != null & conn.isClosed())
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("数据库已关闭");
}
public void doQuery(String sql, Object... params) {
try {
pstmt = conn.prepareStatement(sql);
if (params != null) {
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
}
rs = pstmt.executeQuery();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doOperate(String sql, Object... params) {
try {
pstmt = conn.prepareStatement(sql);
if (params != null) {
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
}
result = pstmt.executeUpdate();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int getNum(String sql){
int num = 0;
try{
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()){
++num;
}
}catch (Exception e) {
// TODO: handle exception
}
return num;
}
}
CategoryDAO继承BaseDAO,代码如下:
两表连接查询出需要用到的数据,将它们分拨额添加到列表 list 中作为一组,再将这一组数据整体作为一个对象添加到listc3中
这样 列表即为 List <List> 两层列表
public class CategoryDAO extends BaseDAO{
public ArrayList listCategory(){
//大小类目合并查看
ArrayList listc3 = new ArrayList();
getConn();
String sql = "select categoryTwo_id,categoryTwo_name,categoryOne_name from t_categoryone inner join t_categorytwo on t_categoryone.categoryOne_id = t_categorytwo.categoryOne_id where 1 = ?";
doQuery(sql, 1);
try {
while(rs.next())
{
CategoryTwo c2 = new CategoryTwo();
ArrayList list = new ArrayList();
int CategoryTwo_id = rs.getInt("categoryTwo_id");
String CategoryTwo_name = rs.getString("categoryTwo_name");
String CategoryOne_name = rs.getString("categoryOne_name");
list.add(CategoryTwo_id);
list.add(CategoryTwo_name);
list.add(CategoryOne_name);
listc3.add(list);
}
closeAll();
return listc3;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
}
4.Action 我的这个Action名字为AdminGoodsManageAction 放在包struts.schoolo2o.action.admin 里
public class AdminGoodsManageAction extends ActionSupport{
private ArrayList listc3;//小类目加大类目名称 列表
public ArrayList getListc3() {
return listc3;
}
public void setListc3(ArrayList listc3) {
this.listc3 = listc3;
}
public String listCategory(){
//查看类目
CategoryDAO categorydao = new CategoryDAO();
listc3 = categorydao.listCategory();
return SUCCESS;
}
}
5.sruts.xml 的配置
<constant name="struts.devMode" value="true" />
<package name="admin" namespace="/admin" extends="struts-default">
<!-- 查看小类目 -->
<action name = "Admin_listCategory" class="struts.schoolo2o.action.admin.AdminGoodsManageAction" method="listCategory">
<result>/admin/Admin_ManageCategoryTwo.jsp</result>
</action>
</package>
6.最后view层 Admin_ManageCategoryTwo.jsp ,可以忽略我前端样式的class 选择器
两层list遍历,第一层的status属性不能少。才能在第二层遍历里用到#status.index得到当前行的序号
<table class="table table-bordered table-striped" >
<thead>
<tr >
<th align="center">二级类目id</th>
<th align="center">二级类目名</th>
<th align="center">所属一级类目</th>
<th align="center">操作</th>
</tr>
</thead>
<tbody>
<s:iterator value="#request.listc3" status="st">
<tr >
<s:iterator value="#request.listc3.get(#st.index)" >
<td align="center"><s:property /></td>
</s:iterator>
<td>
<div class="modification">
<form action="" name = "auditform" id = "auditform" method = "post">
<input type = "button" class="btn btn-primary modify " value = "修改" />
<input type = "button" class="btn btn-primary delete" value = "删除" />
</form>
</div>
<div class="clear"></div>
</td>
</tr>
</s:iterator>
</tbody>
</table>
访问http://localhost:8080/schoolo2o(我的项目名称)/admin/Admin_listCategory.action 即可得结果:
最后
以上就是炙热荷花为你收集整理的Struts2 两层list遍历
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复