废话不多说具体代码如下所示:
package com.rc.controller; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import net.sf.json.JSONObject; import com.rc.dao.R_mailboxDAO; import com.rc.daoimpl.R_mailboxDAOimpl; import com.rc.dbutil.Sqltools; import com.rc.entity.Mailbox; import com.rc.entity.R_user; import com.rc.entity.TreeNodes; import com.rc.util.Page; import com.rc.util.PageUtil; public class MailBoxServlet extends HttpServlet { /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); String action =request.getParameter("action"); HttpSession hs = request.getSession(false); //读取登录人员session R_user cuser =(R_user)hs.getAttribute("user"); PrintWriter out = null; R_mailboxDAO mb = new R_mailboxDAOimpl(); boolean b = true; if("page".equals(action)){//查询所有 int pageNumber = 0; String PageNumberstr = request.getParameter("pageNumber");//从页面获取的当前页 int count =mb.getcount(); if(PageNumberstr == null||"".equals(PageNumberstr)){ pageNumber= 1; }else{ pageNumber = Integer.parseInt(PageNumberstr);//否则强转 } List <Page> pages = new ArrayList<Page>(); Page page = PageUtil.createPage(5, count, pageNumber); pages.add(page); List<Mailbox> mailboxlist = mb.getcostList(page); JSONObject obj = new JSONObject();//定义一个json对象 obj.put("mailbox", mailboxlist); obj.put("Page", pages); out = response.getWriter(); out.write(obj.toString()); }else if("delete".equals(action)){//删除操作 String mid = request.getParameter("id"); JSONObject obj = new JSONObject(); b =mb.delete(Integer.parseInt(mid));//用boolean接收 obj.put("biaozhi",b); out = response.getWriter(); out.write(obj.toString()); }else if("edit".equals(action)){//弹出编辑页面 }else if("tedit".equals(action)){//提交编辑信息 }else if("pldelete".equals(action)){//批量删除 JSONObject obj = new JSONObject(); String deleteidlist = request.getParameter("deleteidlist"); String[] item = deleteidlist.split(","); for (int i = 0; i < item.length; i++) { b =mb.delete(Integer.parseInt(item[i])); } obj.put("biaozhi",b); out = response.getWriter(); out.write(obj.toString()); }else if("tree".equals(action)){ List<TreeNodes> treelist = mb.getnodes(); JSONObject obj = new JSONObject();//定义一个json对象 obj.put("treelist", treelist); out = response.getWriter(); out.write(obj.toString()); }else if("save".equals(action)){ String id = request.getParameter("id"); String zhuti = request.getParameter("zhuti"); String content = request.getParameter("content"); Mailbox mail = new Mailbox(); mail.setS_id(Integer.parseInt(id));//收件人 mail.setS_name(Sqltools.findusername(Integer.parseInt(id)));//收件人姓名 mail.setP_name(cuser.getUsername());//发件人姓名 mail.setP_id(cuser.getId()); mail.setContent(content); mail.setTitle(zhuti); b = mb.addmailbox(mail); JSONObject obj = new JSONObject(); obj.put("biaozhi", b); out = response.getWriter(); out.write(obj.toString()); } } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
dao层
package com.rc.dao; import java.util.List; import com.rc.entity.Mailbox; import com.rc.entity.TreeNodes; import com.rc.util.Page; public interface R_mailboxDAO { public List<Mailbox> getcostList(Page page); //获取全部的邮件 public int getcount();//获取数目 public boolean delete(Integer id); //删除 public boolean add(Mailbox mail);//写信 public boolean update(Integer id);//修改 public List<TreeNodes> getnodes();//树 public boolean addmailbox(Mailbox mail);//添加邮件 }
daoimpl
<pre>ackage com.rc.daoimpl; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.rc.dao.R_mailboxDAO; import com.rc.dbutil.Sqltools; import com.rc.entity.Mailbox; import com.rc.entity.TreeNodes; import com.rc.util.Page; import com.rc.util.StringUtil; public class R_mailboxDAOimpl implements R_mailboxDAO { boolean b=true; Connection cnn = null; PreparedStatement ps = null; ResultSet rs= null; Statement st = null; String sql=""; @Override public List<Mailbox> getcostList(Page page) { List <Mailbox> mailboxlist = new ArrayList<Mailbox>();//定义一个数组 int startsize = page.getCurrentPage()*page.getEverPage(); int endsize = (page.getCurrentPage()-1)*page.getEverPage()+1; sql = "select * from (select a1.*,rownum rn from (select * from mailbox order by m_id desc) a1 WHERE rownum<="+startsize+") where rn>="+endsize+""; try{ rs =Sqltools.excuteQuery(sql, null); while(rs.next()){ Mailbox mailbox = new Mailbox(); mailbox.setMid(rs.getInt("m_id")); mailbox.setP_name(rs.getString("p_name")); mailbox.setS_name(rs.getString("r_name")); mailbox.setStatus(rs.getString("r_status")); mailbox.setContent(rs.getString("r_content")); mailbox.setTitle(rs.getString("r_title")); mailbox.setR_time(StringUtil.TimetoString(rs.getDate("r_time"))); mailboxlist.add(mailbox); } }catch(Exception e){ Sqltools.close(rs, st, cnn); } return mailboxlist.size()==0 ? null:mailboxlist; } @Override public int getcount() { int count =0; sql = "select count(*) from mailbox"; try{ cnn = Sqltools.getConnection(); ps = cnn.prepareStatement(sql); rs = ps.executeQuery(); if(rs.next()){ count = rs.getInt(1); } }catch(Exception e){ e.printStackTrace(); }finally{ Sqltools.close(rs, ps, cnn); } return count; } @Override public boolean delete(Integer id) { sql="delete from mailbox where m_id=?"; try{ cnn=Sqltools.getConnection(); ps=cnn.prepareStatement(sql); ps.setInt(1, id); ps.executeUpdate(); }catch(Exception e){ b=false; e.printStackTrace(); }finally{ Sqltools.aclose(rs, ps, cnn); } return b; } @Override public boolean add(Mailbox mail) { return false; } @Override public boolean update(Integer id) { return false; } @Override public List<TreeNodes> getnodes() {//得到树节点 String sql = "select * from tree_table order by nid "; cnn = Sqltools.getConnection(); ArrayList<TreeNodes> treelist = new ArrayList<TreeNodes>(); try { ps = cnn.prepareStatement(sql); rs =ps.executeQuery(); while (rs.next()){ TreeNodes node = new TreeNodes(); node.setNid(rs.getInt("nid")); node.setParentId(rs.getInt("parentid")); node.setNodeName(rs.getString("nodename")); treelist.add(node); } } catch (SQLException e) { e.printStackTrace(); }finally{ Sqltools.aclose(rs, ps, cnn); } return treelist; } @Override public boolean addmailbox(Mailbox mail) { sql = "insert into mailbox(m_id,p_name,r_name,p_id,r_id,r_content,r_title,r_send,r_status,r_time) values(mailbox_id_seq.nextval,?,?,?,?,?,?,?,?,sysdate)"; try{ cnn =Sqltools.getConnection(); ps = cnn.prepareStatement(sql); ps.setString(1,mail.getP_name()); ps.setString(2,mail.getS_name()); ps.setInt(3,mail.getP_id()); ps.setInt(4,mail.getS_id()); ps.setString(5,mail.getContent()); ps.setString(6,mail.getTitle()); ps.setString(7,"0");//是否发送 ps.setString(8,"3");//是否读取 ps.executeUpdate(); }catch(Exception e){ b = false; e.printStackTrace(); }finally{ Sqltools.aclose(rs, ps, cnn); } return b; } }
jsp页面