ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

云笔记的项目分析(6):云记类型模块呈现

2021-05-31 19:02:20  阅读:231  来源: 互联网

标签:typeId 云记 title request 项目分析 resultInfo params 模块 action


导入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 }』&nbsp;&nbsp;<a href="note?action=detail&noteId=${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;
	}
	

 

标签:typeId,云记,title,request,项目分析,resultInfo,params,模块,action
来源: https://blog.51cto.com/u_13985831/2836987

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有