无级栏目C RUD

很久没写无级了 以前写过现在写忘记了。
把代码贴上来怕已经忘记了

数据库标很简单3个字段
id
name
pid
我们先来看dao

package com.hcwy.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hcwy.bean.Menu;

public class MenuDAO {
private DBAccess dao;
private PreparedStatement pstmt;
private ResultSet rs;

public MenuDAO(){
try {
dao=new DBAccess();
} catch (Exception e) {
e.printStackTrace();
}
}

public List showMenu(Integer id) throws Exception{
String sql="select * from testmenu where pid=?";
// System.out.println(sql+"---->"+id);
pstmt=dao.getConn().prepareStatement(sql);
pstmt.setInt(1, id);
rs=pstmt.executeQuery();
ArrayList list=new ArrayList();
while(rs.next()){
Menu menu=new Menu();
menu.setId(rs.getInt(1));
menu.setName(rs.getString(2));
menu.setPid(rs.getInt(3));
list.add(menu);
}
return list;
}

public void add(Menu m){
String sql="insert into testmenu values(null,?,?)";
try {
System.out.println(sql+"--->"+m.getName()+"--->"+m.getPid());
pstmt=dao.getConn().prepareStatement(sql);
pstmt.setString(1, m.getName());
pstmt.setInt(2, m.getPid());
pstmt.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}

public void del(Integer id){
String sql="delete from testmenu where id=?";
try {
pstmt=dao.getConn().prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}

public void del1(Integer id){
String sql="delete from testmenu where pid=?";
try {
pstmt=dao.getConn().prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}




}


在来看service

package com.hcwy.service;

import java.util.ArrayList;
import java.util.List;

import com.hcwy.bean.Menu;
import com.hcwy.dao.MenuDAO;

public class MenuService {
MenuDAO dao=new MenuDAO();


public List getMenu(Integer id){//递归
ArrayList list = null;
ArrayList list1=new ArrayList();
try {
list = (ArrayList) dao.showMenu(id);
list1.addAll(list);//存放自己
if(list!=null){
for(int i=0;i<list.size();i++){
Menu menu=(Menu) list.get(i);
Integer id1=menu.getId();
list1.addAll(getMenu(id1));//存放子类
}
}


} catch (Exception e) {
e.printStackTrace();
}
return list1;
}

public void add(Menu m){
dao.add(m);
}

public void del(Integer id){
dao.del(id);
dao.del1(id);
}

}


action

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.hcwy.struts.action;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.hcwy.bean.Menu;
import com.hcwy.dao.MenuDAO;
import com.hcwy.service.MenuService;
import com.hcwy.struts.form.MenuForm;

/**
* MyEclipse Struts
* Creation date: 04-16-2008
*
* XDoclet definition:
* @struts.action path="/menu" name="menuForm" input="/form/menu.jsp" parameter="method" scope="request" validate="true"
*/
public class MenuAction extends DispatchAction {

public ActionForward show(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
MenuForm menuForm = (MenuForm) form;
String id= request.getParameter("id");
Integer id1;
if(id==null){
id1=0;
}else{
id1=Integer.parseInt(id);
}
MenuService ms=new MenuService();
ArrayList list=(ArrayList) ms.getMenu(id1);
request.setAttribute("list", list);
return mapping.findForward("list");
}









public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
MenuForm menuForm = (MenuForm) form;
Menu m=new Menu();
try {
BeanUtils.copyProperties(m, menuForm);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
MenuService ms=new MenuService();
ms.add(m);
return mapping.findForward("gotoshow");
}



public ActionForward del(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
MenuForm menuForm = (MenuForm) form;
Integer id=menuForm.getId();
MenuService ms=new MenuService();
ms.del(id);
return mapping.findForward("gotoshow");
}



}



<%@ page language="java" pageEncoding="gbk"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<title>show.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<logic:iterate id="menu" name="list">
<bean:write name="menu" property="id"/>
<bean:write name="menu" property="name"/>
<bean:write name="menu" property="pid"/>
<a href="add.jsp?pid=<bean:write name="menu" property="id"/>">添加</a>
<a href="menu.do?method=del&id=<bean:write name="menu" property="id"/>">删除</a>
<br>
</logic:iterate>
</body>
</html:html>


===============================================================
下面是add.jsp
<%@ page language="java" pageEncoding="gbk"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>


<html:html lang="true">
<head>
<html:base />

<title>add.jsp</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<html:form action="/menu.do?method=add">
<br>
名字: <html:text property="name"></html:text>
<html:hidden property="pid" value="<%=request.getParameter("pid") %>"/>
<html:submit></html:submit>
</html:form>
</body>
</html:html>



我不做过多解释了 这东西很简单 放到来做个记录。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值