第七节 业务功能:显示奏折列表
1、流程图
2、创建组件
2.1、创建 WorkServlet
2.1.1、创建 Java 类
刚开始是空的,还没有写方法:
public class WorkServlet extends ModelBaseServlet {
private MemorialsService memorialsService = new MemorialsServiceImpl();
}
2.1.2、注册
<servlet>
<servlet-name>workServlet</servlet-name>
<servlet-class>com.atguigu.imperial.court.servlet.module.WorkServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>workServlet</servlet-name>
<url-pattern>/work</url-pattern>
</servlet-mapping>
2.2、创建 MemorialsService
2.2.1、接口
2.2.2、实现类
public class MemorialsServiceImpl implements MemorialsService {
private MemorialsDao memorialsDao = new MemorialsDaoImpl();
}
3、WorkServlet 方法
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);
}
4、MemorialsService 方法
@Override
public List<Memorials> getAllMemorialsDigest() {
return memorialsDao.selectAllMemorialsDigest();
}
5、MemorialsDao 方法
@Override
public List<Memorials> selectAllMemorialsDigest() {
String sql = "select memorials_id memorialsId,\n" +
" memorials_title memorialsTitle,\n" +
" concat(left(memorials_content, 10), \"...\") memorialsContentDigest,\n" +
" emp_name memorialsEmpName,\n" +
" memorials_create_time memorialsCreateTime,\n" +
" memorials_status memorialsStatus\n" +
"from t_memorials m left join t_emp e on m.memorials_emp=e.emp_id;";
return getBeanList(sql, Memorials.class);
}
6、页面显示
6.1、页面上的样式声明
<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>
6.2、用户登录信息部分
<!-- 登录信息部分 -->
<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>
6.3、数据展示信息部分
<!-- 数据显示部分 -->
<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>
7、和登录成功对接
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);
}
}
}
第八节 业务功能:显示奏折详情
1、流程图
2、调整奏折列表页面的超链接
<a th:href="@{/work(method='showMemorialsDetail',memorialsId=${memorials.memorialsId})}">奏折详情</a>
3、WorkServlet 方法
protected void showMemorialsDetail(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// 1、从请求参数读取 memorialsId
String memorialsId = request.getParameter("memorialsId");
// 2、根据 memorialsId 从 Service 中查询 Memorials 对象
Memorials memorials = memorialsService.getMemorialsDetailById(memorialsId);
// 3、将 Memorials 对象存入请求域
request.setAttribute("memorials", memorials);
// 4、解析渲染视图
String templateName = "memorials_detail";
processTemplate(templateName, request, response);
}
4、MemorialsService 方法
@Override
public Memorials getMemorialsDetailById(String memorialsId) {
return memorialsDao.selectMemorialsById(memorialsId);
}
5、MemorialsDao 方法
@Override
public Memorials selectMemorialsById(String memorialsId) {
String sql = "select memorials_id memorialsId,\n" +
" memorials_title memorialsTitle,\n" +
" memorials_content memorialsContent,\n" +
" emp_name memorialsEmpName,\n" +
" memorials_create_time memorialsCreateTime,\n" +
" memorials_status memorialsStatus,\n" +
" feedback_time feedbackTime,\n" +
" feedback_content feedbackContent\n" +
"from t_memorials m left join t_emp e on m.memorials_emp=e.emp_id " +
"where memorials_id=?;";
return getSingleBean(sql, Memorials.class, memorialsId);
}
6、详情页
<!-- 登录信息部分 -->
<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>
<tr>
<td>奏折标题</td>
<td th:text="${memorials.memorialsTitle}"></td>
</tr>
<tr>
<td>上疏大臣</td>
<td th:text="${memorials.memorialsEmpName}"></td>
</tr>
<tr>
<td>上疏时间</td>
<td th:text="${memorials.memorialsCreateTime}"></td>
</tr>
<tr>
<td>奏折内容</td>
<td th:text="${memorials.memorialsContent}"></td>
</tr>
<tr th:if="${memorials.memorialsStatus == 2}">
<td>批复时间</td>
<td th:text="${memorials.feedbackTime}"></td>
</tr>
<tr th:if="${memorials.memorialsStatus == 2}">
<td>批复时间</td>
<td th:text="${memorials.feedbackContent}"></td>
</tr>
</table>
<div th:if="${memorials.memorialsStatus != 2}">
<form th:action="@{/work}" method="post">
<input type="hidden" name="method" value="feedBack" />
<input type="hidden" name="memorialsId" th:value="${memorials.memorialsId}"/>
<textarea name="feedbackContent"></textarea>
<button type="submit">御批</button>
</form>
</div>
<a th:href="@{/work?method=showMemorialsDigestList}">返回列表</a>
7、更新状态
7.1、业务逻辑规则
一份未读奏折,点击查看后,需要从未读变成已读。
7.2、WorkServlet 方法
增加判断:
protected void showMemorialsDetail(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// 1、从请求参数读取 memorialsId
String memorialsId = request.getParameter("memorialsId");
// 2、根据 memorialsId 从 Service 中查询 Memorials 对象
Memorials memorials = memorialsService.getMemorialsDetailById(memorialsId);
// **********************补充功能**********************
// 获取当前奏折对象的状态
Integer memorialsStatus = memorials.getMemorialsStatus();
// 判断奏折状态
if (memorialsStatus == 0) {
// 更新奏折状态:数据库修改
memorialsService.updateMemorialsStatusToRead(memorialsId);
// 更新奏折状态:当前对象修改
memorials.setMemorialsStatus(1);
}
// **********************补充功能**********************
// 3、将 Memorials 对象存入请求域
request.setAttribute("memorials", memorials);
// 4、解析渲染视图
String templateName = "memorials_detail";
processTemplate(templateName, request, response);
}
7.3、MemorialsService 方法
@Override
public void updateMemorialsStatusToRead(String memorialsId) {
memorialsDao.updateMemorialsStatusToRead(memorialsId);
}
7.4、MemorialsDao 方法
@Override
public void updateMemorialsStatusToRead(String memorialsId) {
String sql = "update t_memorials set memorials_status=1 where memorials_id=?";
update(sql, memorialsId);
}
第九节 业务功能:批复奏折
1、本质
提交表单,更新数据。
2、WorkServlet 方法
protected void feedBack(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取表单提交的请求参数
String memorialsId = request.getParameter("memorialsId");
String feedbackContent = request.getParameter("feedbackContent");
// 执行更新
memorialsService.updateMemorialsFeedBack(memorialsId, feedbackContent);
// 重定向回显示奏折列表页面
response.sendRedirect(request.getContextPath() + "/work?method=showMemorialsDigestList");
}
3、MemorialsService 方法
@Override
public void updateMemorialsFeedBack(String memorialsId, String feedbackContent) {
memorialsDao.updateMemorialsFeedBack(memorialsId, feedbackContent);
}
4、MemorialsDao 方法
@Override
public void updateMemorialsFeedBack(String memorialsId, String feedbackContent) {
String feedbackTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String sql = "update t_memorials set memorials_status=2,feedback_content=?,feedback_time=? where memorials_id=?";
update(sql, feedbackContent, feedbackTime, memorialsId);
}
本文章参考B站 尚硅谷2022版Maven教程(maven入门+高深,全网无出其右!),仅供个人学习使用,部分内容为本人自己见解,与尚硅谷无关。