导入JSTL的标签库
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
若查询到的云记列表为空,显示提示信息,不呈现页面
<c:if test="${resultInfo.code == 0 }">
<h2>${resultInfo.msg }</h2>
</c:if>
呈现首页显示列表
<c:if test="${resultInfo.code == 1 }">
<div class="note_datas">
<ul>
<c:forEach items="${resultInfo.result.datas }" var="item">
<fmt:formatDate value="${item.pubTime}" pattern="yyyy-MM-dd" var="pubTime"/>
<li>『 ${pubTime }』 <a href="note?action=detail¬eId=${item.noteId }">${item.title }</a> </li>
</c:forEach>
</ul>
</div>
分页的操作JSP (这一串代码,整个人处于懵逼状态)
<nav style="text-align: center">
<ul class="pagination center">
<li><a href="index?pageNum=1<c:if test="${!empty action }">&action=${action }</c:if><c:if test="${!empty title }">&title=${title }</c:if><c:if test="${!empty date }">&pubTime=${date }</c:if><c:if test="${!empty typeId }">&typeId=${typeId }</c:if>">首页</a></li>
<li><a href="index?pageNum=${resultInfo.result.prePage }
<c:if test="${!empty action }">&action=${action }</c:if>
<c:if test="${!empty title }">&title=${title }</c:if><c:if test="${!empty date }">&pubTime=${date }</c:if>
<c:if test="${!empty typeId }">&typeId=${typeId }</c:if>">上一页</a></li>
<c:forEach begin="${resultInfo.result.startNavPage }" end="${resultInfo.result.endNavPage }" var="p">
<li <c:if test="${p == resultInfo.result.pageNum }">class="active"</c:if> >
<a href="index?pageNum=${p}<c:if test="${!empty action }">&action=${action }</c:if>
<c:if test="${!empty title }">&title=${title }</c:if>
<c:if test="${!empty date }">&pubTime=${date }</c:if>
<c:if test="${!empty typeId }">&typeId=${typeId }</c:if>">${p }</a></li>
</c:forEach>
</ul>
</nav>
</c:if>
</div>
TypeServlet:
首先得到用户行为
String action = request.getParameter("action");
if ("list".equals(action)) {
//查询类型列表
typeList(request,response);
}
private void typeList(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//1.从session作用域中得到用户对象,得到userId
User user = (User) request.getSession().getAttribute("user");
Integer userId = user.getUserId();
//2.调用Service层,返回result对象
ResultInfo<List<NoteType>> resultInfo = typeService.findTypeList(userId);
//3.将resultInfo对象存到request作用域
request.setAttribute("resultInfo", resultInfo);
//4.设置动态包含的页面
request.setAttribute("changePage", "type/list.jsp");
//5.请求转发到index.jsp
request.getRequestDispatcher("index.jsp").forward(request, response);
}
Service层:
public ResultInfo<List<NoteType>> findTypeList(Integer userId) {
ResultInfo<List<NoteType>> resultInfo = new ResultInfo<List<NoteType>>();
//调用Dao层,通过用户ID查询类型的集合
List<NoteType> typeList = typeDao.findTypeList(userId);
//判断集合是否为空
if (typeList !=null && typeList.size()>0) {
resultInfo.setCode(1);
resultInfo.setResult(typeList);
}else{
resultInfo.setCode(0);
resultInfo.setMsg("未查询到类型数据!");
}
return resultInfo;
}
Dao层:
public int editNote(String typeId, String title, String content,
String noteId) {
String sql = "";
List<Object> params = new ArrayList<Object>();
params.add(typeId);
params.add(title);
params.add(content);
if (StringUtil.isNotEmpty(noteId)) { //修改操作
sql = "update tb_note set typeId = ?,title = ?,content=? where noteId = ?";
params.add(noteId);
}else{//添加操作
sql = "insert into tb_note (typeId,title,content,pubTime) values (?,?,?,now())";
}
int row = baseDao.executeUpdate(sql, params);
return row;
}
注:params中注入的类型要和问号对应
public int executeUpdate(String sql,List<Object> params) {
int row = 0; //受影响的行数
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
//得到数据库连接
connection = DBUtil.getConnection();
//预编译
preparedStatement = connection.prepareStatement(sql);
//设置参数
//判断参数集合是否为空
if (params !=null && params.size()>0) {
//循环设置参数,下标从1开始
for (int i = 0; i < params.size(); i++) {
preparedStatement.setObject(i+1, params.get(i));
}
}
//执行更新,返回受影响的行数
row = preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
finally{
//关闭资源
DBUtil.close(null, preparedStatement, connection);
}
return row;
}