码迷,mamicode.com
首页 > Web开发 > 详细

JSP实现 留言板 分页显示,新留言显示在第一个~

时间:2017-05-26 11:57:46      阅读:375      评论:0      收藏:0      [点我收藏+]

标签:servlet   format   setattr   xtend   back   mit   ima   url   cti   

页面效果图技术分享

 


 

留言Servlet-----MessageServlet

package cn.MuJH.newsManager.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.MuJH.newsManager.biz.MessageManager;
import cn.MuJH.newsManager.biz.impl.MessageManagerImpl;
import cn.MuJH.newsManager.entity.Message;
import cn.MuJH.newsManager.entity.PageMsg;

@WebServlet(name = "msg", urlPatterns = { "/msg" })
public class MessageServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
    public MessageServlet() {
        super();
       
    }

 
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  doPost(request, response);
 }

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  MessageManager mm=new MessageManagerImpl();
  String opr = request.getParameter("mth");
  String author =request.getParameter("author");
  String message =request.getParameter("message");
  if(opr!=null){
     if(author!=null || author!=""){
     SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     Message msg=new Message(0,author,message,sdf.format(new Date()));
     mm.addMsg(msg);
     }
  }else{
     request.setAttribute("author", author);
     request.setAttribute("message", message);
  }
  
  PageMsg page=new PageMsg();
  page.setPageSize(5);   //每页显示留言条数
  int totalPage=mm.getTotalCount()%page.getPageSize()==0?mm.getTotalCount()/page.getPageSize():mm.getTotalCount()/page.getPageSize()+1;
  page.setTotalPage(totalPage);
  if(request.getParameter("pageIndex")==null||request.getParameter("pageIndex")==""){
     page.setPageIndex(1);
  }else{
     page.setPageIndex(Integer.parseInt(request.getParameter("pageIndex")));
  }
  List<Message> list=mm.getMessageByPageindex(page.getPageIndex(), page.getPageSize());
  page.setList(list);
  request.setAttribute("page", page);
  
  request.getRequestDispatcher("message.jsp").forward(request, response);
 }

}

见图:技术分享

技术分享

 

 


页面内容---message.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery1.8.0.min.js"></script>
</head>
<body>
 <h1>留言板</h1>
 <c:forEach items="${page.list }" var="msg">
  <p>作者:${msg.author }</p>
  <p>内容:${msg.message }</p>
  <p>发表时间:${msg.pubdate }</p>
 </c:forEach>
 <input type="button" value="上一页" onclick="fenye(${page.pageIndex-1})"
 <c:if test="${page.pageIndex eq 1 }">disabled="disabled"</c:if>
 >
 
 <input type="button" value="下一页" onclick="fenye(${page.pageIndex+1})"
 <c:if test="${page.pageIndex eq page.totalPage }">disabled="disabled"</c:if>
 >
 
 <hr/>
 
 <form action="msg?mth=save" method="post" id="myform" onsubmit="return check()">
  <p>用户名:<input type="text" name="author" id="author" value="${author }"/></p>
  <p>留言:<input type="text" name="message" id="message" value="${message }"/></p>
  <p><input type="submit" value="发表留言"/></p>
  <input type="hidden" name="pageIndex" id="pageIndex">
 </form>
 
 <script>
  function check(){
   if((document).getElementById("author").value==""){
    alert("用户名不能为空");
    document.getElementById("author").focus();
    return false;
   }
   if((document).getElementById("message").value==""){
    alert("用户名不能为空");
    document.getElementById("message").focus();
    return false;
   }
   return true;
  }
  function fenye(pageIndex){
   document.getElementById("pageIndex").value=pageIndex;
   document.getElementById("myform").action="msg";
   document.getElementById("myform").submit();
  }
 </script>
</body>
</html>

见图:技术分享

技术分享

 


 底层代码图片:message 实体类

技术分享

----MessageDao

技术分享

 接口的实现类----MessageDaoImpl

 

技术分享

技术分享

 

 技术分享

----MessageManager

技术分享

 

----MessageManagerImpl

技术分享


数据库图片:

技术分享

 

JSP实现 留言板 分页显示,新留言显示在第一个~

标签:servlet   format   setattr   xtend   back   mit   ima   url   cti   

原文地址:http://www.cnblogs.com/MuJH/p/6907644.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!