小确幸BBS论坛-3-个人中心

话说:

各位读者朋友,晚上好!前面介绍了注册登录、首页,本篇介绍个人中心

难度系数:★★☆☆☆
目录
1.页面展示
2.个人中心pojo
3.个人中心帖子列表、发帖、修改、删除

1.页面展示
userInfo.jsp-个人中心
addText.jsp-发帖
updateText.jsp-修改帖子

userInfo.jsp-个人中心

这里写图片描述

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!--引入JSTL标签库  -->
 <%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c" %>
<!--Author:Meice 2018年1月1日下午6:43:53 -->
<!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/textList.css" rel="stylesheet" type="text/css">
<link href="css/userinfo.css" rel="stylesheet" type="text/css">
<style type="text/css">
    a:hover{
        color:orange;
    }
</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="textShow" class="login.jsp">退出</a>
        </div>




        <!--个人资料  -->
        <div id="myInfo" style="width: 300px;margin-left:100px;">
            <img src="images" style="width:270px;height:170px;"><br/>
            <span style="padding-left:85px;">性别:&nbsp;&nbsp;${user.sex }</span><br/>
            <span>最新登录时间:&nbsp;&nbsp;${user.loginTime }</span><br/>
            <span>注&nbsp;&nbsp;册&nbsp;&nbsp;时&nbsp;&nbsp;间:&nbsp;&nbsp;${user.registerTime}</span><br/>

            <a  href="editUserInfo.jsp" style="padding-left:85px;">修改资料</a>
        </div>





        <!--个人帖子  -->
        <div id="myText">
            <span>Ta滴帖子</span>
            <p style="height: 20px;"></p>
            <!-- <hr style="font-size: 10px;font-color:red;"/> -->
            <table border="1 solid red" width="100%" >
                <thead>
                    <tr style="background-color: rgb(233,248,230);">
                        <th>帖子名</th>
                        <th>栏目</th>
                        <th>发布时间</th>
                        <th>操作</th>
                    </tr>
                </thead>


                <tbody>

                    <c:forEach var="listUserInfo" items="${listUserInfo}">

                        <tr>
                            <td><a href="textDetail?textId=${listUserInfo.text.textId}">${listUserInfo.text.title }</a></td>
                            <td>${listUserInfo.category.categoryName }</td>
                            <td>${listUserInfo.text.textTime}</td>
                            <td align="center">
                                <a href="updateText?textId=${listUserInfo.text.textId}" style="color:green;font-weight:bolder">修改</a> |
                                <a href="delText?textId=${listUserInfo.text.textId}" style="color:green;font-weight:bolder" onclick="return confirm('真的忍心删除我麽?')">删除</a>
                            </td>
                        </tr>

                    </c:forEach>

                </tbody>

            </table>

        </div>



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



</body>
</html>

addText.jsp-发帖
这里写图片描述

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!--Author:Meice 2018年1月3日上午3:48:27 -->
<!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/textList.css" rel="stylesheet" type="text/css">
<link href="css/addText.css" rel="stylesheet" type="text/css">


<style type="text/css">
    textarea{
        resize:none;
    }
</style>
</head>
<body>  
    <jsp:include page="head.jsp"></jsp:include>

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


        <div id="mycookie">
            论坛首页 > 发新帖
        </div>


        <div id="addText">
            <form action="addText" method=-"get">
                <table>

                    <tr>
                        <td>标题:</td>
                        <td>
                            <input type="text" name="title" style="width:600px;height:30px;">
                        </td>
                        <td>
                            <select name="category" style="width:100px;height:30px;">
                                <option>选择栏目</option>
                                <option>Java</option>
                                <option>MySQL</option>
                                <option>大数据</option>
                                <option>人工智能</option>
                                <option>HTML5</option>
                                <option>生活</option>
                                <option>有趣有料</option>
                            </select>

                        </td>
                    </tr>

                    <tr>
                        <td style="text-align: inherit;">内容:</td>
                    </tr>
                    <tr>

                        <td colspan="3">
                            <textarea name="context" cols="150" rows="20"></textarea>
                        </td>


                    </tr>

                    <tr>
                        <td colspan="3" align="right"> 
                            <input type="submit" value="发布" style="width:100px;height:30px;">
                        </td>
                    </tr>
                </table>
            </form>

        </div>








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

</body>
</html>

updateText.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月3日上午3:48:27 -->
<!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/textList.css" rel="stylesheet" type="text/css">
<link href="css/addText.css" rel="stylesheet" type="text/css">


<style type="text/css">
    textarea{
        resize:none;
    }
</style>
</head>
<body>  
    <jsp:include page="head.jsp"></jsp:include>

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


        <div id="mycookie">
            论坛首页 > 修改帖子
        </div>


        <div id="addText">
            <form action="doUpdateText?textId=${userInfo.text.textId}" method="get">
                <table>

                    <tr>
                        <td>标题:</td>

                        <td>
                            <input type="text" name="title"   value="${userInfo.text.title}"   style="width:600px;height:30px;">
                        </td>
                        <td><input type="hidden" name="textId" value="${userInfo.text.textId}"></td>
                        <td>
                            <select name="category" style="width:100px;height:30px;">

                            <!-- 这里不能写固定,要通过遍历写 -->
                            <!-- <option>选择栏目</option> -->  
                                <c:forEach var="category" items="${listCategory}" >

                                    <c:if test="${category.categoryId==userInfo.category.categoryId}">
                                        <option selected="selected">${category.categoryName}</option>
                                    </c:if>

                                    <c:if test="${category.categoryId!=userInfo.category.categoryId}">
                                        <option >${category.categoryName}</option>
                                    </c:if>




                                </c:forEach>
                            </select>

                        </td>
                    </tr>

                    <tr>
                        <td style="text-align: inherit;">内容:</td>
                    </tr>
                    <tr>

                        <td colspan="3">
                            <textarea name="context"   cols="150" rows="20">${userInfo.text.context }</textarea>
                        </td>


                    </tr>

                    <tr>
                        <td colspan="3" align="right"> 
                            <input type="submit" value="修改" style="width:100px;height:30px;">
                        </td>
                    </tr>
                </table>
            </form>

        </div>








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

</body>
</html>

2.个人中心pojo
个人中心,本质还是显示帖子,只不过多了个栏目,所以Pojo这里改动了下,直接查四个表,里面直接放对象。这样取值非常方便。
useInfo

package com.hmc.pojo;



/**
*
*2018年1月1日
*User:Meice
*下午7:53:46
*/
public class UserInfo {
    //个人中心帖子和首页帖子唯一区别就是个人中心多了栏目,所以,可以定义2个对象就行
    private Text text;
    private Category category;



    public UserInfo() {}
    public UserInfo(Text text,Category category) {

        this.text = text;
        this.category = category;
    }
    public Text getText() {
        return text;
    }
    public void setText(Text text) {
        this.text = text;
    }
    public Category getCategory() {
        return category;
    }
    public void setCategory(Category category) {
        this.category = category;
    }
    @Override
    public String toString() {
        return "UserInfo [text=" + text + ", category=" + category + "]";
    }


}

这里面全是对象,到时候在框架部分,就绑定配置即可。

3. 个人中心帖子列表、发帖、修改、删除
add update del底层封装了Cud方法,因为都是直接修改数据库,不涉及查出来放到一个容器中,所以Cud方法照样适用。

复杂点的地方上就是修改的时候,栏目页面不能写死,要从页面遍历,这样才能保证option选择的是对应的栏目。

这里面除了首页查在TextShowDaoImpl里面写了方法,其他都调用Cud()方法即可。

1)个人中心帖子列表
2)发帖
3)修改
4)删除

1)个人中心帖子列表
UserInfoServlet

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.User;
import com.hmc.pojo.UserInfo;
import com.hmc.service.TextShowService;
import com.hmc.service.TextShowServiceImpl;

/**
*
*2018年1月1日
*User:Meice
*下午7:11:51
*/
public class UserInfoServlet  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.接收参数 userId 用户存在session里面 直接取值
         User user =(User)req.getSession().getAttribute("user");
         int userId = user.getUserId();


        //2.调用方法  显示当前用户的信息
        TextShowService tss = new TextShowServiceImpl();
        String sql = "select t.textId,t.title,t.textTime,t.context,t.categoryId,t.userId,c.categoryName "
                + "from text t "
                + "inner join "
                + "category c on t.categoryId = c.categoryId "
                + "where userId = ?"
                + " order by "
                + "t.textId desc";
        Object[] params = {userId}; 
        List<UserInfo> listUserInfo =   tss.listUserInfo(sql, params);





        //3.跳转页面
        req.setAttribute("listUserInfo", listUserInfo);
        req.getRequestDispatcher("userInfo.jsp").forward(req, resp);

    }
}

2)发帖
AddTextServlet

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.Category;
import com.hmc.pojo.User;
import com.hmc.service.CategoryService;
import com.hmc.service.CategoryServiceImpl;
import com.hmc.service.UserRegisterService;
import com.hmc.service.UserRegisterServiceImpl;
import com.hmc.util.GetFormatTimeUtil;

/**
*
*2018年1月3日
*User:Meice
*上午4:19:22
*/

public class AddTextServlet 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.接收发帖内容
        String title =  req.getParameter("title");
        String categoryName = req.getParameter("category");
        String context = req.getParameter("context");
        System.out.println("发帖内容:"+title + "   "+categoryName+"  "+context);

        CategoryService cs = new CategoryServiceImpl();
        Category category = cs.getOneCategory(categoryName);


        String textTime =GetFormatTimeUtil.getFormatTime(24);
        User user =(User)req.getSession().getAttribute("user");
        int userId = user.getUserId();


        //2.调用发帖方法
        UserRegisterService urs = new UserRegisterServiceImpl();
        String sql = "insert into text (title,context,textTime,categoryId,userId) values (?,?,?,?,?)";
        Object[] params = {title,context,textTime,category.getCategoryId(),userId};
        int result =    urs.Cud(sql, params);




        //3.跳转个人中心
        if(result>0) {

            req.getRequestDispatcher("userInfo").forward(req, resp);
        }else {

            System.out.println("遗憾,发帖失败");
        }


    }

}

3)修改
UpdateTextServlet

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.Category;
import com.hmc.pojo.Text;
import com.hmc.pojo.UserInfo;
import com.hmc.service.CategoryService;
import com.hmc.service.CategoryServiceImpl;
import com.hmc.service.TextShowService;
import com.hmc.service.TextShowServiceImpl;
import com.hmc.util.GetStrToInt;

/**
*
*2018年1月3日
*User:Meice
*上午5:12:55
*/
public class UpdateTextServlet 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.接收参数
        String textIdStr =req.getParameter("textId");   
        int textId = GetStrToInt.getInt(textIdStr);

        //2.调用方法
        String sql = "select t.textId,t.title,t.context,t.categoryId,t.textTime, c.categoryId,c.categoryName ,t.userId from text t inner join category c on t.categoryId = c.categoryId where textId = ?";
        Object[] params = {textId};
        TextShowService tss = new TextShowServiceImpl();
        List<UserInfo> listUserInfo =   tss.listUserInfo(sql, params);
        UserInfo userInfo = listUserInfo.get(0);

        System.out.println("得到单个帖子信息:"+userInfo);

        //把整个栏目对象传过去,便于页面取值
        CategoryService gs = new CategoryServiceImpl();
        String sql2 = "select * from category";
        List<Category> listCategory =   gs.getAllCagegory(sql2, null);

        System.out.println("得到所有栏目集合:"+listCategory);

        //3.页面跳转
        req.setAttribute("userInfo", userInfo);
        req.setAttribute("listCategory", listCategory);
        req.getRequestDispatcher("updateText.jsp").forward(req, resp);

    }
}

上面是把要修改的帖子放到页面,下面才真正执行修改:
DoUpdateTextServlet

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.Category;
import com.hmc.service.CategoryService;
import com.hmc.service.CategoryServiceImpl;
import com.hmc.service.UserRegisterService;
import com.hmc.service.UserRegisterServiceImpl;
import com.hmc.util.GetStrToInt;

/**
*
*2018年1月3日
*User:Meice
*上午6:23:35
*/
public class DoUpdateTextServlet  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.接收参数
        String textIdStr = req.getParameter("textId");
        int textId = GetStrToInt.getInt(textIdStr);
        String title =  req.getParameter("title");
        String categoryName =   req.getParameter("category");
        String context  = req.getParameter("context");
        System.out.println(textId);

        CategoryService cs = new CategoryServiceImpl();
        Category category = cs.getOneCategory(categoryName);
        int categoryId = category.getCategoryId();
        //2.调用方法

        UserRegisterService urs = new UserRegisterServiceImpl();
        String sql = "update text set title=?,categoryId = ?,context=? where textId = ?";
        Object[] params = {title,categoryId,context,textId};
        int result =    urs.Cud(sql, params);
        System.out.println(result);


        //3.跳转页面
        if(result>0) {
            req.getRequestDispatcher("userInfo").forward(req, resp);
        }else {
            System.out.println("遗憾,修改失败!");
        }



    }
}

4)删除
DelTextServlet

package com.hmc.controller;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hmc.service.UserRegisterService;
import com.hmc.service.UserRegisterServiceImpl;
import com.hmc.util.GetStrToInt;

/**
*
*2018年1月3日
*User:Meice
*上午6:36:42
*/
public class DelTextServlet 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.接收参数

            String textIdStr = req.getParameter("textId");
            int textId = GetStrToInt.getInt(textIdStr);


            //2.调用方法
            UserRegisterService urs = new UserRegisterServiceImpl();
            String sql = "delete from text where textId = ?";
            Object[] params = {textId};
            int result =    urs.Cud(sql, params);

            //3.页面跳转
            if(result>0) {
                req.getRequestDispatcher("userInfo").forward(req, resp);
            }else {
                System.out.println("删除失败。。");
            }


    }
}

好了,到此为止,今天内容结束了,暂不总结吧,总结放在最后面。

其实,这里可以看到,每写一个功能,调用一个Servlet,这样Servlet很多,后面框架就改善了这种情况,一个Controller里面,写多个方法,根据路径,直接调用到方法。由此可知,Servlet 本质根据路径直接访问Servlet(类),框架是直接根据路径访问方法。

好了,晚安!下期预告:帖子详情

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值