S2结业项目完成了,用到的知识点很多,下面我来总结下我写的功能块
我主要负责整个项项目的留言方面和新闻管理方面
前台的留言如下:
要求是 用户购买商品后对自己购买的商品作出评价和意见,功能主要用一个查询,新增和分页
关键代码如下:
写方法
public interface Meliuyan { //获取所有消息 public List<me> getme(int pageIndex,int pageSize); //添加消息方法 public boolean addme(me me); //返回消息个数 public int getone(); public boolean Update(me me); public List<me> byIdgetAllInfo(int id); //回复留言的方法 public boolean byIdUpdatemeInfo(me m); //删除留言的方法 public boolean byIdrem(int id); }
实现类
public class Meliuyanimpl extends BaseDao implements Meliuyan{ public List<me> getme(int pageIndex, int pageSize) { String sql="select top "+pageSize+" * from EASYBUY_COMMENT where EC_ID not in(select top "+(pageIndex-1)*pageSize+" EC_ID from EASYBUY_COMMENT)"; ResultSet rs = executeSelect(sql); List<me> list=new ArrayList<me>(); if (rs!=null) { try { while (rs.next()) { me me=new me(); me.setEC_ID(rs.getInt("EC_ID")); me.setEC_CONTENT(rs.getString("EC_CONTENT")); me.setEC_CREATE_TIME(rs.getString("EC_CREATE_TIME")); me.setEC_REPLY(rs.getString("EC_REPLY")) ; me.setEC_REPLY_TIME(rs.getString("EC_REPLY_TIME")); me.setEC_NICK_NAME(rs.getString("EC_NICK_NAME")); list.add(me); } } catch (Exception e) { e.printStackTrace(); } } return list; } public boolean addme(me me) { String sql="insert into EASYBUY_COMMENT values(?,?,?,?,?)"; System.out.println(sql); Object [] obj={me.getEC_CONTENT(),me.getEC_CREATE_TIME(),me.getEC_REPLY(),me.getEC_REPLY_TIME(),me.getEC_NICK_NAME()}; if (executeUpdate(sql,obj)) { return true; } return false; } public int getone() { String sql="select count(1) as count from EASYBUY_COMMENT"; ResultSet rs=executeSelect(sql); int id=0; if (rs!=null) { try { while (rs.next()) { id=rs.getInt("count"); } } catch (Exception e) { e.printStackTrace(); } } return id; } public int getone1() { String sql="select count(1) as count from EASYBUY_COMMENT"; ResultSet rs=executeSelect(sql); int id=0; if (rs!=null) { try { while (rs.next()) { id=rs.getInt("count"); } } catch (Exception e) { e.printStackTrace(); } } return id; } //查询 public List<me> byIdgetAllInfo(int id) { String sql="select * from EASYBUY_COMMENT where EC_ID=?"; Object[] para={id}; ResultSet rs = executeSelect(sql,para); List<me> list=new ArrayList<me>(); if (rs!=null) { try { while (rs.next()) { me me=new me(); me.setEC_ID(rs.getInt("EC_ID")); me.setEC_CONTENT(rs.getString("EC_CONTENT")); me.setEC_CREATE_TIME(rs.getString("EC_CREATE_TIME")); me.setEC_REPLY(rs.getString("EC_REPLY")) ; me.setEC_REPLY_TIME(rs.getString("EC_REPLY_TIME")); me.setEC_NICK_NAME(rs.getString("EC_NICK_NAME")); list.add(me); } } catch (Exception e) { e.printStackTrace(); } } return list; }
Servlet调用
public class MeServlet extends HttpServlet { ProoductCategoryDaoImpl_hyj pcd = new ProoductCategoryDaoImpl_hyj(); /** * */ private static final long serialVersionUID = 1L; Meliuyanimpl mdi=new Meliuyanimpl(); UserDaoimpl impl=new UserDaoimpl(); /** 出品人:豪哥 */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } /** 出品人:豪哥 */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //处理乱码 request.setCharacterEncoding("utf-8"); try { page(request,response); } catch (Exception e1) { e1.printStackTrace(); } //获取opr的值 String opr=request.getParameter("opr"); if(opr!=null){ if (opr.equals("list")) { try { // 1获取一级菜单的集合放到作用域中 List<ProductCategory> listOne = pcd.getAllOneLeveInfo(0); // 2.获取二级菜单的集合放到作用域中 List<ProductCategory> listTwo = pcd.getAllTowLeveInfo(0); request.setAttribute("listOne", listOne); request.setAttribute("listTwo", listTwo); } catch (Exception e) { e.getMessage(); } Page page=new Page(); page.setPageSize(3); try { page.setPageSum(mdi.getone()); int pageIndex=1; String uindex=request.getParameter("pageIndex"); if(uindex!=null&&!uindex.equals("")){ pageIndex=Integer.parseInt(uindex); } page.setPageCount((int)Math.ceil(page.getPageSum()*1.0/page.getPageSize())); //判断是否符合显示条件 if (pageIndex<1) { pageIndex=1; }else if (pageIndex>page.getPageCount()) { pageIndex=page.getPageCount(); } page.setPageIndex(pageIndex); page.setList(mdi.getme(page.getPageIndex(), page.getPageSize())); request.setAttribute("page",page); request.getRequestDispatcher("/guestbook.jsp").forward(request, response); } catch (Exception e) { e.getStackTrace(); } } if(opr.equals("add")){ //获取提交的值 String name=request.getParameter("guestName"); if (name.equals("当前用户名")) { String uid=(String)request.getSession().getAttribute("userId"); try { name=impl.byUserIdtoName(uid); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } String nrorong=request.getParameter("guestContent"); //String date =request.getParameter() //获取系统时间 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 String date=df.format(new Date()); //进行添加操作 //实例化Message实体类 me message=new me(); message.setEC_NICK_NAME(name); message.setEC_CONTENT(nrorong); message.setEC_CREATE_TIME(date); //实例化MessageDaoimpl类调用添加的方法 System.out.println(message.getEC_CREATE_TIME()); System.out.println(message.getEC_REPLY_TIME()); if(mdi.addme(message)){ request.getRequestDispatcher("/servlet/MeServlet?opr=list").forward(request, response); } } //返回留言信息 if(opr.equals("Update")){ String id=request.getParameter("id"); List<me> melist=mdi.byIdgetAllInfo(Integer.parseInt(id)); request.setAttribute("meList", melist); request.getRequestDispatcher("/manage/guestbook-modify.jsp").forward(request, response); }
最后在index.jsp页面显示就完成了
<h2>全部留言</h2> <c:forEach var="item" items="${page.list}"> <ul> <li> <dl> <dt>${item.EC_NICK_NAME}</dt> <dd class="author">网友:${item.EC_CONTENT} <span class="timer">${item.EC_CREATE_TIME }</span></dd> <dd>${item.EC_REPLY_TIME}</dd> <dd>回复:${item.EC_REPLY}</dd> </dl> </li> </ul> </c:forEach>
接下来是后台的留言管理
后台留言是根据前台用户的留言作出回复,回复后 状态会显示已回复,没有回复状态会显示未回复,根据ID来获取前台留言的信息
关键代码如下:
//实现类
//回复留言 public boolean byIdUpdatemeInfo(me m) { String sql="Update EASYBUY_COMMENT set EC_REPLY=?,EC_REPLY_TIME=? where EC_ID=?"; Object[] pa={m.getEC_REPLY(),m.getEC_CREATE_TIME(),m.getEC_ID()}; return executeUpdate(sql, pa); } //删除留言 public boolean byIdrem(int id) { String sql = "delete from EASYBUY_COMMENT where EC_ID=?"; Object[] objs={id}; boolean count =executeUpdate(sql, objs); return count; }
Servlet类调用
//回复留言 if(opr.equals("UpdateOK")){ String nrorong=request.getParameter("replyContent"); String id=request.getParameter("id"); //String date =request.getParameter() //获取系统时间 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 String date=df.format(new Date()); //进行添加操作 //实例化Message实体类 me message=new me(); message.setEC_ID(Integer.parseInt(id)); message.setEC_REPLY(nrorong); message.setEC_CREATE_TIME(date); if(mdi.byIdUpdatemeInfo(message)){ request.getRequestDispatcher("/manage/index.jsp").forward(request, response); } } } } //后台留言 public void page(HttpServletRequest request, HttpServletResponse response) throws Exception, IOException { Meliuyanimpl mdi=new Meliuyanimpl(); //留言总记录数 int pageSum; String opr=request.getParameter("opr"); if(opr!=null){ if (opr.equals("listafter")) { Page page=new Page(); page.setPageSize(3); try { page.setPageSum(mdi.getone()); int pageIndex=1; String uindex=request.getParameter("pageIndex"); if(uindex!=null&&!uindex.equals("")){ pageIndex=Integer.parseInt(uindex); } page.setPageCount((int)Math.ceil(page.getPageSum()*1.0/page.getPageSize())); //判断是否符合显示条件 if (pageIndex<1) { pageIndex=1; }else if (pageIndex>page.getPageCount()) { pageIndex=page.getPageCount(); } page.setPageIndex(pageIndex); page.setList(mdi.getme(page.getPageIndex(), page.getPageSize())); request.setAttribute("page",page); request.getRequestDispatcher("/manage/guestbook.jsp").forward(request, response); } catch (Exception e) { e.getStackTrace(); } //保存在作用域中 request.getSession().setAttribute("mdi",page); //跳转到JSP页面 request.getRequestDispatcher("/manage/guestbook.jsp").forward(request, response); } //后台修改 if(opr.equals("add1")){ //获取提交的值 String nrorong=request.getParameter("replyContent"); //String date =request.getParameter() //获取系统时间 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 String date=df.format(new Date()); //进行添加操作 //实例化Message实体类 me message=new me(); message.setEC_REPLY(nrorong); message.setEC_CREATE_TIME(date); //实例化MessageDaoimpl类调用添加的方法 System.out.println(message.getEC_CREATE_TIME()); System.out.println(message.getEC_REPLY()); if(mdi.Update(message)){ request.getRequestDispatcher("/manage/index.jsp").forward(request, response); } } //删除 if (opr.equals("del")) { System.out.println("11"); String flag= request.getParameter("id"); if (flag != null) { int id =Integer.parseInt(flag); if (mdi.byIdrem(id)) { request.getRequestDispatcher("/servlet/MeServlet?opr=listafter").forward(request,response); }else { request.getRequestDispatcher("/servlet/MeServlet?opr=listafter").forward(request, response); } } }
后台的jsp页面实现
<h2>留言管理</h2> <div class="manage"> <table class="list"> <tr> <th>ID</th> <th>姓名</th> <th>留言内容</th> <th>状态</th> <th>操作</th> </tr> <c:forEach var="item" items="${page.list}"> <tr> <td class="first w4 c">${item.EC_ID}</td> <td class="w1 c">${item.EC_NICK_NAME}</td> <td>${item.EC_CONTENT }</td> <c:choose> <c:when test="${empty item.EC_REPLY}"> <td class="w1 c">未回复</td> <td class="w1 c"><a href="<%=path %>/servlet/MeServlet?opr=Update&id=${item.EC_ID}">回复</a> <a class="manageDel" href="http://localhost:8080/<%=path %>/servlet/MeServlet?opr=del&id=${item.EC_ID}">删除</a></td> </c:when> <c:otherwise> <td class="w1 c">已回复</td> <td class="w1 c"><a href="<%=path %>/servlet/MeServlet?opr=Update&id=${item.EC_ID}">修改</a> <a class="manageDel" href="http://localhost:8080/<%=path %>/servlet/MeServlet?opr=del&id=${item.EC_ID}">删除</a></td> </c:otherwise> </c:choose> </tr> </c:forEach> </table> <div class="pager"> <ul class="clearfix"> <li><a href="<%=path %>/servlet/MeServlet?opr=listafter&pageIndex=1">尾页</a></li> <li><a href="<%=path%>/servlet/MeServlet?pageIndex=${page.pageIndex-1}&opr=listafter">上一页</a></li> <li>${page.pageIndex}/${page.pageCount}</li> <li><a id="buttom" href="<%=path %>/servlet/MeServlet?pageIndex=${page.pageIndex+1}&opr=listafter">下一页</a></li> <li><a href="<%=path %>/servlet/MeServlet?opr=listafter&pageIndex=${page.pageCount}">尾页</a></li> </ul> </div>
还有一个权限管理,就是用来判断用户是普通用户还是管理员,登录的账号是管理员就可以进入后台进行操作,普通用户则不能
这个功能相对简单,不做过多解释
新闻管理这个也比较简单就是进行增删改查
代码我就不发了。以上就是我总结的功能块,感谢大家支持,想看更多精彩文章,请关照我,后面会更新更多的知识