小确幸BBS论坛-4-帖子详情及评论

本文介绍了一个简单的论坛系统中帖子详情页的设计与实现,包括帖子详情展示、回复功能及其实现方法。

话说:

各位读者朋友,晚上好啊~这几天真冷啊!怎么敲键盘,手都不热乎,以后发明一种可以暖手的键盘。本篇博客目标:帖子详情及评论

难度系数:★☆☆☆☆
目录
1.帖子详情及评论页面

2.补充栏目对象
2.1 实体类Category
2.2 dao层
2.3 service层

3.帖子详情
3.1 pojo - textAll

3.回复表对象reply
3.1 pojo - reply
3.2 回复展示 - ReplyShowServlet
3.3 回复帖子- AddReplyServlet

1.帖子详情及评论页面

这里写图片描述

这里写图片描述

textDetail.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c" %>
<!--Author:Meice 2018年1月4日下午10:04:50 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>小确幸论坛</title>

<!-- 引入样式 -->
<link href="css/textDetail.css" rel="stylesheet" type="text/css">
<link href="css/textList.css"  rel="stylesheet" type="text/css">
<style type="text/css">

</style>
</head>
<body>
    <jsp:include page="head.jsp"></jsp:include>


    <!--登录后用户信息  -->
        <div id="userInfo" >
            <a href="textShow" class="userInfo">首页</a>
            <span class="userInfo">欢迎【${user.userNick}】!</span><br/>
            <!-- <a href="addText.jsp" class="addText" style="color:orange;">发帖</a> -->
            <a href="login.jsp" class="login.jsp">退出</a>
        </div>


         <%-- ${textAll} --%>
        <div id="title" style="margin:auto;padding:auto;margin-top:60px;width:600px;">

                <span style="font-size:20px;font-weight:bolder;color:black;margin-left:80px;">${textAll.text.title}</span><br/>
                <br/>
                <span style="margin-left:30px;">楼主:${textAll.user.userNick } &nbsp;&nbsp;发帖时间:${textAll.text.textTime }&nbsp;&nbsp; 回复:${textAll.reply.replyCount }</span>


        </div>



        <div id="context" style="background-color:rgb(238,238,238);width:1000px;">
            ${textAll.text.context }


        </div>
        <br/><br/>

        <div>
            <% int i = 0; %>

            <!--循环遍历回复  -->
            <table align="center" width="67%" style="margin-left:250px;">

                    <c:forEach var="reply" items="${listReply}">
                        <%i++; %>
                        <tr style="background-color: rgb(238,238,238);width: 900px;height:50px;">
                            <td> <%=i %>楼: &nbsp;&nbsp;${reply.user.userNick } &nbsp;&nbsp;时间:${reply.replyTime }  </td>
                        </tr>

                        <tr style="background-color: rgb(238,238,238);width: 900px;height:50px;">
                                <td>${reply.replyContext }</td>
                        </tr>

                        <tr style="height:20px;"></tr>

                    </c:forEach>






            </table>
        </div>









        <div id="addText">
                <form action="addReply"  method="get">
                    <input type="hidden" name="textId" value="${textAll.text.textId}">
                    <textarea  name= "replyContext" cols="150" rows="10" style="resize:none;"></textarea>
                    <br/><input type="submit" value="评论" style="width:100px;height:40px;margin-left:920px;">
                </form>
        </div>

    <jsp:include page="foot.jsp"></jsp:include>
</body>
</html>

2.补充栏目对象
栏目对象用的多的是修改,上一篇遗漏,这一篇补上。
2.1 实体类Category

package com.hmc.pojo;
/**
*
*2018年1月1日
*User:Meice
*下午7:54:30
*/
public class Category {
    private int categoryId;
    private String categoryName;
    public Category() {}
    public Category(int categoryId,String categoryName) {
        this.categoryId = categoryId;
        this.categoryName = categoryName;
    }
    public int getCategoryId() {
        return categoryId;
    }
    public void setCategoryId(int categoryId) {
        this.categoryId = categoryId;
    }
    public String getCategoryName() {
        return categoryName;
    }
    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }
    @Override
    public String toString() {
        return "Category [categoryId=" + categoryId + ", categoryName=" + categoryName + "]";
    }




}

2.2 dao层
CategoryDao
CategoryDaoImpl

CategoryDao

package com.hmc.dao;

import java.util.List;

import com.hmc.pojo.Category;

/**
*
*2018年1月3日
*User:Meice
*上午4:43:27
*/
public interface CategoryDao {
    //1.根据栏目名称,返回一个栏目对象
        Category getOneCategory(String categoryName);


        //2.遍历得到栏目对象集合
        List<Category> getAllCagegory(String sql,Object... params);
}

CategoryDaoImpl

package com.hmc.dao;

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

import com.hmc.pojo.Category;
import com.hmc.util.DBUtil;
import com.hmc.util.GetPreparedStatementUtil;

/**
*
*2018年1月3日
*User:Meice
*上午4:44:00
*/
public class CategoryDaoImpl implements CategoryDao {

    @Override
    public Category getOneCategory(String categoryName) {
        Connection conn = DBUtil.getConn();
        String sql = "select * from category where categoryName = ?";
        Object[] params = {categoryName};
        PreparedStatement ps = null;
         ps = GetPreparedStatementUtil.getPs(conn, ps, sql, params);
        try {
            ResultSet rs =  ps.executeQuery();
            if(rs != null && rs.next()) {
                Category category = new Category();
                category.setCategoryId(rs.getInt("categoryId"));
                category.setCategoryName(rs.getString("categoryName"));
                return category;
            }   
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {

            DBUtil.closeConn(conn, ps, null);
        }
        return null;
    }

    @Override
    public List<Category> getAllCagegory(String sql,Object... params) {
        List<Category> listCategory = new ArrayList<>();
        Connection conn = DBUtil.getConn();
        PreparedStatement ps2 = null;
         ps2 = GetPreparedStatementUtil.getPs(conn, ps2, sql, params);
        try {
            ResultSet rs2 = ps2.executeQuery();
            while(rs2!= null && rs2.next()) {
                Category c = new Category();
                c.setCategoryId(rs2.getInt("categoryId"));
                c.setCategoryName(rs2.getString("categoryName"));
                listCategory.add(c);

            }
            return listCategory;

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {

            DBUtil.closeConn(conn, ps2, null);
        }

        return null;
    }

}

查单个栏目和查所有栏目,查所有栏目是为了在修改的时候,页面好遍历;查单个是因为要获取单个对象。

2.3 service层
CategoryService
CategoryServiceImpl

CategoryService

package com.hmc.service;
/**
*
*2018年1月3日
*User:Meice
*上午4:41:52
*/

import java.util.List;

import com.hmc.pojo.Category;

public interface CategoryService {

    //1.根据栏目名称,返回一个栏目对象
    Category getOneCategory(String categoryName);

    //2.遍历得到栏目对象集合
    List<Category> getAllCagegory(String sql,Object... params);


}

CategoryServiceImpl

package com.hmc.service;

import java.util.List;

import com.hmc.dao.CategoryDaoImpl;
import com.hmc.pojo.Category;

/**
*
*2018年1月3日
*User:Meice
*上午4:42:59
*/
public class CategoryServiceImpl implements CategoryService {

    //1.新增帖子的时候,用户选择的是栏目名字,得到整个对象
    @Override
    public Category getOneCategory(String categoryName) {
        return new CategoryDaoImpl().getOneCategory(categoryName);
    }

    //2.修改时候,栏目要能自动选择

    @Override
    public List<Category> getAllCagegory(String sql,Object... params) {
        return new CategoryDaoImpl().getAllCagegory(sql, params);
    }

}

3.帖子详情
因为帖子详情和首页还有点不一样,所以索性四表联查,全部放进去。

3.1 pojo - textAll

package com.hmc.pojo;
/**
*
*2018年1月4日
*User:Meice
*下午9:25:05
*/
public class TextAll {
    private Text text;
    private User user;
    private Category category;
    private Reply reply;


    public TextAll() {}


    public TextAll(Text text, User user, Category category, Reply reply) {
        super();
        this.text = text;
        this.user = user;
        this.category = category;
        this.reply = reply;
    }


    public Text getText() {
        return text;
    }


    public void setText(Text text) {
        this.text = text;
    }


    public User getUser() {
        return user;
    }


    public void setUser(User user) {
        this.user = user;
    }


    public Category getCategory() {
        return category;
    }


    public void setCategory(Category category) {
        this.category = category;
    }


    public Reply getReply() {
        return reply;
    }


    public void setReply(Reply reply) {
        this.reply = reply;
    }


    @Override
    public String toString() {
        return "TextAll [text=" + text + ", user=" + user + ", category=" + category + ", reply=" + reply + "]";
    }




}

3.2 textDetailServlet

package com.hmc.controller;

import java.io.IOException;
import java.util.List;

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

import com.hmc.pojo.Reply;
import com.hmc.pojo.TextAll;

import com.hmc.service.TextShowService;
import com.hmc.service.TextShowServiceImpl;
import com.hmc.util.GetStrToInt;

/**
*
*2018年1月4日
*User:Meice
*下午9:54:21
*/
public class TextDetailServlet  extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入帖子详情Servlet");
        //1.接收参数
        String textIdStr  = req.getParameter("textId");
        int textId = GetStrToInt.getInt(textIdStr);
        System.out.println("当前帖子textId  " + textId +"帖子Id字符串:    "+textIdStr);

        //2.调用方法
        TextShowService tss = new TextShowServiceImpl();
        String sql = "select t.textId,t.title,t.context,t.textTime,u.userId ,u.userName,u.userNick,u.loginTime,u.registerTime,c.categoryId,c.categoryName\r\n" + 
                "           ,r.replyId,r.replyContext,r.replyTime,count(replyId) replyCount\r\n" + 
                "           from \r\n" + 
                "           text t \r\n" + 
                "           inner join \r\n" + 
                "           user u \r\n" + 
                "           on t.userId = u.userId\r\n" + 
                "           inner join \r\n" + 
                "           category c \r\n" + 
                "           on t.categoryId = c.categoryId \r\n" + 
                "           left join \r\n" + 
                "           reply r \r\n" + 
                "           on r.textId = t.textId\r\n" + 
                "           where t.textId = ? group by t.textId ";

        Object[] params = {textId};
        List<TextAll> listAll = tss.listAll(sql, params);
        TextAll textAll = listAll.get(0);
        System.out.println("帖子详情:  "+listAll);



        //3.页面跳转
        req.setAttribute("textAll", textAll);
        req.getRequestDispatcher("replyShow").include(req, resp);
        req.getRequestDispatcher("textDetail.jsp").forward(req, resp);




    }
}

3.回复表对象reply
3.1 pojo - reply

package com.hmc.pojo;
/**
*
*2018年1月4日
*User:Meice
*下午9:25:54
*/
public class Reply {
    private  int replyId;
    private  String replyContext;
    private String replyTime;
    private int textId;
    private User user;
    private int replyCount;


    public Reply () {}






    public Reply(int replyId, String replyContext, String replyTime, int textId, User user, int replyCount) {
        super();
        this.replyId = replyId;
        this.replyContext = replyContext;
        this.replyTime = replyTime;
        this.textId = textId;
        this.user = user;
        this.replyCount = replyCount;
    }






    public int getReplyId() {
        return replyId;
    }

    public void setReplyId(int replyId) {
        this.replyId = replyId;
    }

    public String getReplyContext() {
        return replyContext;
    }

    public void setReplyContext(String replyContext) {
        this.replyContext = replyContext;
    }

    public String getReplyTime() {
        return replyTime;
    }

    public void setReplyTime(String replyTime) {
        this.replyTime = replyTime;
    }

    public int getTextId() {
        return textId;
    }

    public void setTextId(int textId) {
        this.textId = textId;
    }



    public User getUser() {
        return user;
    }






    public void setUser(User user) {
        this.user = user;
    }






    public int getReplyCount() {
        return replyCount;
    }

    public void setReplyCount(int replyCount) {
        this.replyCount = replyCount;
    }






    @Override
    public String toString() {
        return "Reply [replyId=" + replyId + ", replyContext=" + replyContext + ", replyTime=" + replyTime + ", textId="
                + textId + ", user=" + user + ", replyCount=" + replyCount + "]";
    }





}

这里面可以根据需求,灵活添加属性,比如replyCount

3.2 帖子回复显示列表 - ReplyShowServlet
3.2.1 dao层
3.2.2 service层
3.2.3 Servlet层

3.2.1 dao层
ReplyShowDao

package com.hmc.dao;

import java.util.List;

import com.hmc.pojo.Reply;

/**
*
*2018年1月5日
*User:Meice
*下午9:46:50
*/
public interface ReplyShowDao {
    //1.显示当前帖子所有回复
        List<Reply> listReply(String sql,Object... params);
}

ReplyShowDaoImpl

package com.hmc.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hmc.pojo.Reply;
import com.hmc.pojo.User;
import com.hmc.util.DBUtil;
import com.hmc.util.GetPreparedStatementUtil;

/**
*
*2018年1月5日
*User:Meice
*下午9:47:09
*/
public class ReplyShowDaoImpl  implements ReplyShowDao{

    @Override
    public List<Reply> listReply(String sql, Object... params) {
        List<Reply> listReply = new ArrayList<>();
        Connection conn = DBUtil.getConn();
        PreparedStatement ps = null;
        ps = GetPreparedStatementUtil.getPs(conn, ps, sql, params);
        ResultSet rs = null;
        try {
             rs =   ps.executeQuery();
            while(rs != null && rs.next()) {
                Reply reply = new Reply();

                reply.setReplyId(rs.getInt("replyId"));
                reply.setReplyContext(rs.getString("replyContext"));
                reply.setReplyTime(rs.getString("replyTime"));
                User user = new User();
                user.setUserId(rs.getInt("userId"));
                user.setUserName(rs.getString("userName"));
                user.setUserNick(rs.getString("userNick"));

                reply.setUser(user);
                listReply.add(reply);
            }

            return listReply;

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {

            DBUtil.closeConn(conn, ps, rs);
        }


        return null;
    }

}

3.2.2 service层

ReplyShowService
ReplyShowServiceImpl

ReplyShowService

package com.hmc.service;
/**
*
*2018年1月5日
*User:Meice
*下午9:44:59
*/

import com.hmc.pojo.Reply;

import java.util.List;

public interface ReplyShowService {
    //1.显示当前帖子所有回复
    List<Reply> listReply(String sql,Object... params);
}

ReplyShowServiceImpl

package com.hmc.service;

import java.util.List;

import com.hmc.dao.ReplyShowDaoImpl;
import com.hmc.pojo.Reply;

/**
*
*2018年1月5日
*User:Meice
*下午9:46:01
*/
public class ReplyShowServiceImpl implements ReplyShowService {


    // 显示帖子回复
    @Override
    public List<Reply> listReply(String sql, Object... params) {
        return new ReplyShowDaoImpl().listReply(sql, params);
    }

}

3.2.3 Servlet层

ReplyShowServlet

package com.hmc.controller;

import java.io.IOException;
import java.util.List;

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

import com.hmc.pojo.Reply;
import com.hmc.service.ReplyShowService;
import com.hmc.service.ReplyShowServiceImpl;
import com.hmc.util.GetStrToInt;

/**
*
*2018年1月5日
*User:Meice
*下午9:41:15
*/
public class ReplyShowServlet  extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收参数textId
        String textIdStr = req.getParameter("textId");
        int textId = GetStrToInt.getInt(textIdStr);


        //2.调用方法
        ReplyShowService rss = new ReplyShowServiceImpl();
        String sql = "select r.replyId,r.replyContext,r.replyTime,u.userId,u.userName,u.userNick\r\n" + 
                "               from reply r\r\n" + 
                "               inner join user u\r\n" + 
                "               on \r\n" + 
                "               r.userId = u.userId\r\n" + 
                "               where textId = ?";
        Object[] params = {textId};

        List<Reply> listReply = rss.listReply(sql, params);



        //3.页面跳转
        req.setAttribute("listReply", listReply);




    }
}   

3.3 回复帖子- AddReplyServlet

AddReplyServlet

package com.hmc.controller;

import java.io.IOException;

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

import com.hmc.pojo.User;
import com.hmc.service.UserRegisterService;
import com.hmc.service.UserRegisterServiceImpl;
import com.hmc.util.GetFormatTimeUtil;
import com.hmc.util.GetStrToInt;

/**
*
*2018年1月5日
*User:Meice
*下午10:42:00
*/
public class AddReplyServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收页面参数 textId userId
        String textIdStr = req.getParameter("textId");
        int textId = GetStrToInt.getInt(textIdStr);


        User user = (User)req.getSession().getAttribute("user");
        int userId = user.getUserId();

        System.out.println("发布评论  帖子Id:"+textId+"   发布评论用户Id"+userId);
        //接收帖子内容
        String replyContext = req.getParameter("replyContext");
        String replyTime = GetFormatTimeUtil.getFormatTime(24);

        //2.调用方法  CUD 

        UserRegisterService urs = new UserRegisterServiceImpl();
        String sql = "insert into reply (replyContext,replyTime,textId,userId) values (?,?,?,?)";
        //如果数据库replyTime字段是date类型,那么Sql语句直接now()即可,或者直接new Date()
        Object[] params = {replyContext,replyTime,textId,userId};
        int result =    urs.Cud(sql, params);



        //3.跳转页面
        if(result>0) {
            //给提示,,恭喜评论成功!
                req.getRequestDispatcher("textDetail").forward(req, resp);
        }else {
            //遗憾,评论失败!
            System.out.println("遗憾,评论失败!");
        }


    }
}

分层比较细,所以看起来多一点。好了,这篇到此结束,下篇预告:个人中心的【修改资料】 这次是真的晚安了!再会!

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值