当前位置: 首页 > >

Struts2 两层list遍历

发布时间:

这个功能实现了:将两级目录从数据库读取并显示在浏览器上,遍历显示时用到了标签遍历两层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 ?两层列表





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 的配置









/admin/Admin_ManageCategoryTwo.jsp





6.最后view层?Admin_ManageCategoryTwo.jsp ,可以忽略我前端样式的class 选择器


两层list遍历,第一层的status属性不能少。才能在第二层遍历里用到#status.index得到当前行的序号





















二级类目id二级类目名所属一级类目操作








友情链接: