Maven 体系
8. 业务功能:显示奏折详情
8.1 流程图
8.2 调整奏折列表页面的超链接
<a th:href="@{/work(method='showMemorialsDetail',memorialsId=${memorials.memorialsId})}">奏折详情</a>
8.3 WorkServlet 方法
protected void showMemorialsDetail(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String memorialsId = request.getParameter("memorialsId");
Memorials memorials = memorialsService.getMemorialsDetailById(memorialsId);
request.setAttribute("memorials", memorials);
String templateName = "memorials_detail";
processTemplate(templateName, request, response);
}
8.4 MemorialsService 方法
@Override
public Memorials getMemorialsDetailById(String memorialsId) {
return memorialsDao.selectMemorialsById(memorialsId);
}
8.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);
}
8.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>
8.7 更新状态
8.7.1 业务逻辑规则
8.7.2 WorkServlet 方法
protected void showMemorialsDetail(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String memorialsId = request.getParameter("memorialsId");
Memorials memorials = memorialsService.getMemorialsDetailById(memorialsId);
Integer memorialsStatus = memorials.getMemorialsStatus();
if (memorialsStatus == 0) {
memorialsService.updateMemorialsStatusToRead(memorialsId);
memorials.setMemorialsStatus(1);
}
request.setAttribute("memorials", memorials);
String templateName = "memorials_detail";
processTemplate(templateName, request, response);
}
8.7.3 MemorialsService 方法
@Override
public void updateMemorialsStatusToRead(String memorialsId) {
memorialsDao.updateMemorialsStatusToRead(memorialsId);
}
8.7.4 MemorialsDao 方法
@Override
public void updateMemorialsStatusToRead(String memorialsId) {
String sql = "update t_memorials set memorials_status=1 where memorials_id=?";
update(sql, memorialsId);
}
9. 业务功能:批复奏折
9.1 本质
提交表单,更新数据。
9.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");
}
9.3 MemorialsService 方法
@Override
public void updateMemorialsFeedBack(String memorialsId, String feedbackContent) {
memorialsDao.updateMemorialsFeedBack(memorialsId, feedbackContent);
}
9.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);
}
10. 业务功能:登录检查
10.1 流程图
10.2 创建 LoginFilter
10.2.1 创建 Java 类
public class LoginFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpSession session = request.getSession();
Object loginEmp = session.getAttribute(ImperialCourtConst.LOGIN_EMP_ATTR_NAME);
if (loginEmp != null) {
filterChain.doFilter(request, servletResponse);
return ;
}
request.setAttribute("systemMessage", ImperialCourtConst.ACCESS_DENIED_MESSAGE);
request.getRequestDispatcher("/").forward(request, servletResponse);
}
@Override
public void destroy() {}
}
10.2.2 注册
- 把 LoginFilter 放在 TransactionFilter 前面声明,原因是:如果登录检查失败不放行,直接跳转到页面,此时将不必执行 TransactionFilter 中的事务操作,可以节约性能。
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>com.alex.imperial.court.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginFilter</filter-name>
<url-pattern>/work</url-pattern>
</filter-mapping>
11. 打包部署
11.1 适配部署环境
- MySQL 连接信息中,IP 地址部分需要改成 localhost。
url=jdbc:mysql://localhost:3306/db_imperial_court
11.2 跳过测试打包
mvn clean package -Dmaven.test.skip=true
<build>
<finalName>demo-me</finalName>
</build>
11.3 部署执行
11.3.1 上传 war 包
11.3.2 启动 Tomcat
/opt/apache-tomcat-8.5.75/bin/startup.sh
11.3.3 访问测试
|