毕业实习项目-9

需要的资源:

链接:

https://pan.baidu.com/s/12pQBEVRIZ4cBYKtieeRb5Q .
提取码:sxx8

详情模块-查看留言

思路

前台:

  1. 将资源里的comment.jsp文件拷贝至WebContent文件夹下。
  2. 循环遍历显示留言以及分页栏。
  3. top.jsp中修改留言板路径

后台:

  1. 将PageUtil.java工具文件拷贝至com.xxx.util包下

  2. 建一个CommentServlet

  3. 接收参数 currentPage(当前页)

  4. 非空判断
    空,给默认值

  5. 调用Service查询当前页留言集合(当前页、每页数量)

  6. 调用service查询留言总数

    扫描二维码关注公众号,回复: 12648419 查看本文章
  7. 通过PageUtil工具生成pageCode,PageUtil工具参数:(comment?action=findCommentList,留言总数,当前页,每页数量)

  8. 存request作用域(当前留言页集合、PageCode)

  9. 请求转发跳转留言板

实操

  1. 循环遍历显示留言以及分页栏。

comment.jsp文件相关代码

...
<h2>全部留言</h2>
					<ul>
					
					<!--循环遍历显示留言以及分页栏-->
					<c:forEach items="${commentList }" var="comment">
					
						<li>
							<dl>
								<dt>${
    
    comment.content }</dt>
								<dd class="author">
									网友:${
    
    comment.nickName }
									<span class="timer">
									
										<fmt:formatDate value="${comment.createTime }" pattern="yyyy-MM-dd" />
										
									</span>
								</dd>
								
								<!--无官方回复则不显示官方回复-->
								
								<c:if test="${not empty comment.replyContent}">
								<dd>
									官方回复:${
    
    comment.replyContent }&nbsp;&nbsp;
									<span class="timer">
									
									<fmt:formatDate value="${comment.replyTime }" pattern="yyyy-MM-dd" />
										
									</span>
								</dd>
								
								</c:if>
								
							</dl>
						</li><br/>
						
					</c:forEach>
					
					
					</ul>
					<div class="clear"></div>
					<div class="pager">
						<ul class="clearfix">${
    
    pageCode }</ul>
					</div>
...
  1. top.jsp中修改留言板路径

top.jsp文件相关代码

		<!--用户已登陆-->
		<c:if test="${!empty user }">
		
			<a href="" class="shopping">购物车</a>
			<a href="">${
    
    user.userName }</a>
			
			<!--用户注销-->
			<a href="user?action=logout">注销</a>
			
			<a href="register.jsp">注册</a>
			
			<!--修改留言板路径-->
			<a href="comment?action=findCommentList">留言板</a>	
  1. 新建一个CommentServlet

CommentServlet.java文件代码:

package com.xxx.servlet;

import java.io.IOException;
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 com.xxx.po.Comment;
import com.xxx.service.CommentService;
import com.xxx.service.impl.CommentServiceImpl;
import com.xxx.util.PageUtil;
import com.xxx.util.StringUtil;

/**
 * 留言板
 */
@WebServlet("/comment")
public class CommentServlet extends HttpServlet {
    
    
	private static final long serialVersionUID = 1L;

	/**
	 * 
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	
	//CommentService创建一个接口放在Service包下;CommentServiceImpl创建一个类放在Service.Impl包下
	private CommentService commentService = new CommentServiceImpl();
	
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
		// TODO Auto-generated method stub
		
		//判断用户行为
		String action = request.getParameter("action");
		if("findCommentList".equals(action)){
    
    
			
			//查看留言
			findCommentList(request,response);
			
		}else if("addComment".equals(action)){
    
    
			
			//发表留言
			
		}	
	}

	/**
	 * 查看留言
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void findCommentList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
		// TODO Auto-generated method stub
		
		//接收参数 page
		String pageStr = request.getParameter("page");
		
		//非空判断
		if(StringUtil.isEmpty(pageStr)){
    
    
			//空,给默认值
			pageStr = "1";
		}
		
		//
		Integer page = Integer.parseInt(pageStr);
		
		//调用Service查询当前页留言集合(当前页、每页数量)
		//List导一下包,Comment新建一个java类放在po包下,findCommentListByPage生成一下方法
		List<Comment> list = commentService.findCommentListByPage(page,4);
		
		//调用service查询留言总数
		//findCommentTotal生成一下方法
		 int total = commentService.findCommentTotal();
		
		//通过PageUtil工具生成pageCode,PageUtil工具参数:(comment?action=findCommentList,留言总数,当前页,每页数量)
		 String pageCode = PageUtil.getPageCode("comment?action=findCommentList", total, page, 4);
		 
		//存request作用域(当前留言页集合、PageCode)
		 request.setAttribute("commentList", list);
		 request.setAttribute("pageCode", pageCode);
		 
		//请求转发跳转留言板
		request.getRequestDispatcher("comment.jsp").forward(request, response); 
		 
	}

}

  1. 生成的comment.java文件里补充代码
package com.xxx.po;

import java.util.Date;

/**
 * 留言Bean
 * @author yu
 *
 */

public class Comment {
    
    

	private Integer id; 			//主键id 
	private String  content; 		//留言内容
	private Date 	createTime; 	//创建时间
	private String 	nickName; 		//昵称
	private String  replyContent; 	//回复内容
	private Date    replyTime;		//回复时间
	
	//敲完以上三行代码可以使用ALT+SHIFT+S组合键;
	//选择Generate Getters and Setters...
	//选择Select all;ok后即可自动生成下面的Get、Set方法代码
	
	public Integer getId() {
    
    
		return id;
	}
	public void setId(Integer id) {
    
    
		this.id = id;
	}
	public String getContent() {
    
    
		return content;
	}
	public void setContent(String content) {
    
    
		this.content = content;
	}
	public Date getCreateTime() {
    
    
		return createTime;
	}
	public void setCreateTime(Date createTime) {
    
    
		this.createTime = createTime;
	}
	public String getNickName() {
    
    
		return nickName;
	}
	public void setNickName(String nickName) {
    
    
		this.nickName = nickName;
	}
	public String getReplyContent() {
    
    
		return replyContent;
	}
	public void setReplyContent(String replyContent) {
    
    
		this.replyContent = replyContent;
	}
	public Date getReplyTime() {
    
    
		return replyTime;
	}
	public void setReplyTime(Date replyTime) {
    
    
		this.replyTime = replyTime;
	}
	
	
}

  1. 生成的CommentServiceImpl.java文件里补充代码:
package com.xxx.service.impl;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.xxx.po.Comment;
import com.xxx.service.CommentService;
import com.xxx.util.DBUtil;

public class CommentServiceImpl implements CommentService {
    
    

	//查询当前页留言集合
	@Override
	public List<Comment> findCommentListByPage(Integer page, int pageSize) {
    
    
		// TODO Auto-generated method stub
		
		
		//new一个list
		List<Comment> list = new ArrayList<>();
		
		Connection conn = null;
		
		//异常
		try{
    
    
			
		//数据库操作
		//建立连接
		conn = DBUtil.getConnection();
		
		//编写sql语句
		String sql = "select * from t_comment order by createTime desc limit ?,?";
			
		//new一个QueryRunner
		QueryRunner qr = new QueryRunner();
		
		//创建一个参数数组
		Object[] params = {
    
    (page-1)*pageSize,pageSize};
		
		//执行查询,得到商品集合,再放入List中
		list = qr.query(conn, sql ,new BeanListHandler<>(Comment.class),params);
		
		} catch (Exception e){
    
    
			e.printStackTrace();
		}finally{
    
    
			
			//关闭连接
			DBUtil.close(null, null, conn);
		}
		return list;
	}

	
	
	//查询留言总数
	@Override
	public int findCommentTotal() {
    
    
		// TODO Auto-generated method stub

		int total = 0;
		
		//new一个list
		List<Comment> list = new ArrayList<>();
		
		Connection conn = null;
		
		//异常
		try{
    
    
			
		//数据库操作
			
		//建立连接
		conn = DBUtil.getConnection();
		
		//编写sql语句
		String sql = "select * from t_comment";
			
		//new一个QueryRunner
		QueryRunner qr = new QueryRunner();
		
		
		//执行查询,得到商品集合,再放入List中
		list = qr.query(conn, sql ,new BeanListHandler<>(Comment.class));
		total = list.size();
		
		} catch (Exception e){
    
    
			e.printStackTrace();
		}finally{
    
    
			
			//关闭连接
			DBUtil.close(null, null, conn);
		}
		return total;
		
	}

}

结果:

经过以上的操作,我们的查看留言功能就完善啦
查看留言完成1
查看留言完成2

详情模块-发表留言

思路

前台:comment.jsp

  1. 给提交按钮绑定点击事件
    提交表单

后台:CommentServlet

  1. 接受参数
  2. 非空判断
    空,跳转并提示用户且回显留言内容
    不空,调用service添加留言,返回受影响行数
  3. 判断row是否大于0
    否,跳转并提示用户且回显留言内容
  4. 调用CommentServlet里的findCommentList方法跳转回留言页面

实操

  1. 给提交按钮绑定点击事件

comment.jsp文件相关代码:

...
<!-- 写留言 -->
					<div id="reply-box">
						<form id="comForm" action="comment" method="post">
						<input type="hidden" name="action" value="addComment" />
							<table>
								<tr>
									<td class="field">昵称:</td>
									<td><input type="text" id="nickName" name="nickName" value="${comment.nickName }"/></td>
								</tr>
								<tr>
									<td class="field">留言内容:</td>
									<td><textarea id="content" name="content">${
    
    comment.content }</textarea></td>
								</tr>
								<tr>
									<td class="field"></td>
									<td><label class="ui-blue">
										<input type="button" id="comBtn" value="提交留言"/></label>
										&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
										<font id="error" color="red"></font>
									</td>
								</tr>
							</table>
						</form>
					</div>
				</div>
			</div>
			<div class="clear"></div>
		</div>
		<div id="footer">
			<jsp:include page="common/footer.jsp" />
		</div>
	</body>
	
	<!--给提交按钮绑定点击事件-->
	<script type="text/javascript">
	$("#comBtn").click(function(){
    
    
		//提交表单
		$("#comForm").submit();
	})
	</script>
	
</html>
  1. CommentServlet.java里修改代码:

CommentServlet.java更新代码:

package com.xxx.servlet;

import java.io.IOException;
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 com.xxx.po.Comment;
import com.xxx.service.CommentService;
import com.xxx.service.impl.CommentServiceImpl;
import com.xxx.util.PageUtil;
import com.xxx.util.StringUtil;

/**
 * 留言板
 */
@WebServlet("/comment")
public class CommentServlet extends HttpServlet {
    
    
	private static final long serialVersionUID = 1L;

	/**
	 * 
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	
	//CommentService创建一个接口放在Service包下;CommentServiceImpl创建一个类放在Service.Impl包下
	private CommentService commentService = new CommentServiceImpl();
	
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
		// TODO Auto-generated method stub
		
		//处理添加留言字符乱码 
		request.setCharacterEncoding("UTF-8");
		
		//判断用户行为
		String action = request.getParameter("action");
		if("findCommentList".equals(action)){
    
    
			
			//查看留言
			findCommentList(request,response);
			
		}else if("addComment".equals(action)){
    
    
			
			//发表留言
			addComment(request,response); //ctrl+1生成一下方法
		}	
	}

	
	/**
	 * 发表留言
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void addComment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
		// TODO Auto-generated method stub
		
		//接受参数
		String nickName = request.getParameter("nickName");
		String content = request.getParameter("content");
		Comment comment = new Comment(nickName,content); //报红,ctrl+1创建一下构造参数
		
		//非空判断
		if(StringUtil.isEmpty(nickName)){
    
    
			//空,跳转并提示用户且回显留言内容
			request.setAttribute("comment", comment);
			request.setAttribute("msg", "昵称不能为空");
			findCommentList(request,response); //ctrl+1抛一下异常
			return;
		}
		
		if(StringUtil.isEmpty(content)){
    
    
			//空,跳转并提示用户且回显留言内容
			request.setAttribute("comment", comment);						
			request.setAttribute("msg", "留言不能为空");
			findCommentList(request,response); //ctrl+1抛一下异常
			return;
		}
		
		//不空,调用service添加留言,返回受影响行数
		int row = commentService.addComment(comment); //创建一下addComment方法
		
		//判断row是否大于0
		if(row <= 0){
    
    
			//否,跳转并提示用户且回显留言内容
			request.setAttribute("comment", comment);						
			request.setAttribute("msg", "发表留言失败");
		}
		
		//调用**CommentServlet**里的**findCommentList**方法跳转回留言页面
		findCommentList(request,response); 
	}

	/**
	 * 查看留言
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void findCommentList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
		// TODO Auto-generated method stub
		
		//接收参数 page
		String pageStr = request.getParameter("page");
		
		//非空判断
		if(StringUtil.isEmpty(pageStr)){
    
    
			//空,给默认值
			pageStr = "1";
		}
		
		//
		Integer page = Integer.parseInt(pageStr);
		
		//调用Service查询当前页留言集合(当前页、每页数量)
		//List导一下包,Comment新建一个java类放在po包下,findCommentListByPage生成一下方法
		List<Comment> list = commentService.findCommentListByPage(page,4);
		
		//调用service查询留言总数
		//findCommentTotal生成一下方法
		 int total = commentService.findCommentTotal();
		
		//通过PageUtil工具生成pageCode,PageUtil工具参数:(comment?action=findCommentList,留言总数,当前页,每页数量)
		 String pageCode = PageUtil.getPageCode("comment?action=findCommentList", total, page, 4);
		 
		//存request作用域(当前留言页集合、PageCode)
		 request.setAttribute("commentList", list);
		 request.setAttribute("pageCode", pageCode);
		 
		//请求转发跳转留言板
		request.getRequestDispatcher("comment.jsp").forward(request, response); 
		 
	}

}

  1. CommentServiceImpl.java文件修改代码:
...
//添加留言,返回受影响行数
	@Override
	public int addComment(Comment comment) {
    
    
		// TODO Auto-generated method stub
		
		int row = 0;
		
		Connection conn = null;
		
		//异常
		try{
    
    
			
		//数据库操作
			
		//建立连接
		conn = DBUtil.getConnection();
		
		//编写sql语句
		String sql = "insert into t_comment (content,createTime,nickName) values (?,now(),?)";
			
		//new一个QueryRunner
		QueryRunner qr = new QueryRunner();
		
		//创建一个参数集合
		Object[] params = {
    
    comment.getContent(),comment.getNickName()};
		
		//执行查询,得到商品集合,再放入List中
		row = qr.update(conn, sql ,params);
		
		} catch (Exception e){
    
    
			e.printStackTrace();
		}finally{
    
    
			
			//关闭连接
			DBUtil.close(null, null, conn);
		}
		return row;
	}
...

结果

经过以上步骤,详情模块就结束啦,我们可以发表留言,查看留言:
发表留言1
发表留言2

猜你喜欢

转载自blog.csdn.net/weixin_42347543/article/details/113618622