Thymeleaf:列表(四)

该代码段展示了如何使用JavaServlet处理用户登录和登出,以及在登录成功后重定向到显示工作列表的页面。工作列表页面使用Thymeleaf模板引擎渲染,显示奏折摘要信息,并根据用户角色展示不同内容。
摘要由CSDN通过智能技术生成

列表样式

在这里插入图片描述

改登录跳转

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class AuthServlet extends ModelBaseServlet {

    private EmpService empService = new EmpServiceImpl();

    protected void login(
            HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {

        try {
            // 1、获取请求参数
            String loginAccount = request.getParameter("loginAccount");
            String loginPassword = request.getParameter("loginPassword");

            // 2、调用 EmpService 方法执行登录逻辑
            Emp emp = empService.getEmpByLoginAccount(loginAccount, loginPassword);

            // 3、通过 request 获取 HttpSession 对象
            HttpSession session = request.getSession();

            // 4、将查询到的 Emp 对象存入 Session 域
            session.setAttribute(ImperialCourtConst.LOGIN_EMP_ATTR_NAME, emp);

            // 5、前往指定页面视图
            // 前往临时页面
            // String templateName = "temp";
            // processTemplate(templateName, request, response);

            // 前往正式的目标地址 列表
            response.sendRedirect(request.getContextPath() + "/work?method=showMemorialsDigestList");

        } catch (Exception e) {
            e.printStackTrace();

            // 6、判断此处捕获到的异常是否是登录失败异常
            if (e instanceof LoginFailedException) {
                // 7、如果是登录失败异常则跳转回登录页面
                // ①将异常信息存入请求域
                request.setAttribute("message", e.getMessage());

                // ②处理视图:index
                processTemplate("index", request, response);

            }else {
                // 8、如果不是登录异常则封装为运行时异常继续抛出
                throw new RuntimeException(e);

            }

        }

    }

    protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 1、通过 request 对象获取 HttpSession 对象
        HttpSession session = request.getSession();

        // 2、将 HttpSession 对象强制失效
        session.invalidate();

        // 3、回到首页
        String templateName = "index";
        processTemplate(templateName, request, response);
    }
}

WorkServlet

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


public class WorkServlet extends ModelBaseServlet {

    private MemorialsService memorialsService = new MemorialsServiceImpl();

    protected void showMemorialsDigestList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 1、调用 Service 方法查询数据
        List<Memorials> memorialsList = memorialsService.getAllMemorialsDigest();

        // 2、将查询得到的数据存入请求域
        request.setAttribute("memorialsList", memorialsList);

        // 3、渲染视图
        String templateName = "memorials-list";
        processTemplate(templateName, request, response);
    }
}

列表页面

<!DOCTYPE html>
<html lang="en" xml:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        table {
            border-collapse: collapse;
            margin: 0px auto 0px auto;
        }

        table th, td {
            border: 1px solid black;
            text-align: center;
        }

        div {
            text-align: right;
        }
    </style>
</head>
<body>

    <!-- 登录信息部分 -->
    <div>
        <span th:if="${session.loginInfo.empPosition == 'emperor'}">恭请皇上圣安</span>
        <span th:if="${session.loginInfo.empPosition == 'minister'}"><span th:text="${session.loginInfo.empName}">XXX</span>大人请安</span>
        <a th:href="@{/auth?method=logout}">退朝</a>
    </div>

	<!-- 数据显示部分 -->
	<table>
	    <thead>
	        <tr>
	            <th>奏折标题</th>
	            <th>内容摘要</th>
	            <th>上疏大臣</th>
	            <th>上疏时间</th>
	            <th>奏折状态</th>
	            <th>奏折详情</th>
	        </tr>
	    </thead>
	    <tbody th:if="${#lists.isEmpty(memorialsList)}">
	        <tr>
	            <td colspan="6">没有人上过折子</td>
	        </tr>
	    </tbody>
	    <tbody th:if="${not #lists.isEmpty(memorialsList)}">
	        <tr th:each="memorials : ${memorialsList}">
	            <td th:switch="${memorials.memorialsStatus}">
	                <span th:text="${memorials.memorialsTitle}" th:case="0" style="color: red;">奏折标题</span>
	                <span th:text="${memorials.memorialsTitle}" th:case="1" style="color: blue;">奏折标题</span>
	                <span th:text="${memorials.memorialsTitle}" th:case="2">奏折标题</span>
	            </td>
	            <td th:switch="${memorials.memorialsStatus}">
	                <span th:text="${memorials.memorialsContentDigest}" th:case="0" style="color: red;">内容摘要</span>
	                <span th:text="${memorials.memorialsContentDigest}" th:case="1" style="color: blue;">内容摘要</span>
	                <span th:text="${memorials.memorialsContentDigest}" th:case="2">内容摘要</span>
	            </td>
	            <td th:switch="${memorials.memorialsStatus}">
	                <span th:text="${memorials.memorialsEmpName}" th:case="0" style="color: red;">上疏大臣</span>
	                <span th:text="${memorials.memorialsEmpName}" th:case="1" style="color: blue;">上疏大臣</span>
	                <span th:text="${memorials.memorialsEmpName}" th:case="2">上疏大臣</span>
	            </td>
	            <td th:switch="${memorials.memorialsStatus}">
	                <span th:text="${memorials.memorialsCreateTime}" th:case="0" style="color: red;">上疏时间</span>
	                <span th:text="${memorials.memorialsCreateTime}" th:case="1" style="color: blue;">上疏时间</span>
	                <span th:text="${memorials.memorialsCreateTime}" th:case="2">上疏时间</span>
	            </td>
	            <td th:switch="${memorials.memorialsStatus}">
	                <span th:case="0" style="color: red;">未读</span>
	                <span th:case="1" style="color: blue;">已读</span>
	                <span th:case="2">已批示</span>
	            </td>
	
	            <td>
	                <a th:href="@{/work?method=detail}">奏折详情</a>
	            </td>
	        </tr>
	    </tbody>
	</table>

</body>
</html>

web.xml

    <filter>
        <filter-name>loginFilter</filter-name>
        <filter-class>com.atguigu.imperial.court.filter.LoginFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>loginFilter</filter-name>
        <url-pattern>/work</url-pattern>
    </filter-mapping>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员无羡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值