今天就告诉大家如何进行EL+JSTL的运用,我们就以分页和修改为例:
index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--
下拉框的模糊查询
<select name="xl">
<option value='gname'>商品名称</option>
<option value='ginfo'>商品描述</option>
</select>
-->
<form action="myload.jsp" method="post">
商品名:<input type="text" name="gname" value="${gname}">
<input type="submit" value="查询">
</form>
<br>
<table border="1px">
<tr>
<td>商品序号</td>
<td>商品名称</td>
<td>商品价格</td>
<td>操作</td>
</tr>
<!-- 如果集合为空 就转发到加载页面去加载数据 -->
<c:if test="${empty myls}">
<jsp:forward page="myload.jsp"></jsp:forward>
</c:if>
<!-- 展示数据 循环遍历集合 -->
<c:forEach items="${myls}" var="g">
<tr>
<td>${g.gid }</td>
<td>${g.gname }</td>
<td>${g.gprice }</td>
<td><a href="preupdate.jsp?gid=${g.gid}">修改</a></td>
</tr>
</c:forEach>
</table>
<div>
【${pageIndex}/${max}】
<a href="myload.jsp?pid=1&gname=${gname}">首页</a>
<a href="myload.jsp?pid=${pageIndex>1?pageIndex-1:1}&gname=${gname}">上一页</a>
<a href="myload.jsp?pid=${pageIndex<max?pageIndex+1:max}&gname=${gname}">下一页</a>
<a href="myload.jsp?pid=${max}&gname=${gname}">末页</a>
</div>
</body>
</html>
myload.jsp:
<%
//调用业务逻辑层 biz
IGoodsBiz igb=new GoodsBiz();
int pageIndex=1;
int pageSize=2;
//接收pid
String pid=request.getParameter("pid");
if(pid!=null){//说明点了x页
pageIndex=Integer.parseInt(pid);
}
//把pageIndex存起来 方便取
request.setAttribute("pageIndex", pageIndex);
//接收表单提交过来的关键字
String str=request.getParameter("gname");
if(str==null){
str="";//相当于查询全部
}
//把gname存起来
request.setAttribute("gname", str);
//获得最大页码
int max=igb.getMax("goods where gname like '%"+str+"%'", pageSize);
//把max存起来 方便取 ${max}
request.setAttribute("max", max);
//调用查询全部的方法
List<Goods> ls=igb.getAllByPage(pageIndex, pageSize, str);
if(ls.size()!=0){//集合中有数据
//把集合存起来=request中
request.setAttribute("myls", ls);
//跳转到展示页面进行展示 只能转发
request.getRequestDispatcher("index.jsp").forward(request, response);
}
else{
System.out.print("集合为空-优先检查数据库中是否有数据 DBHelper以及dao/biz");
}
%>
preupdate.jsp:
<%
//调用业务逻辑层 biz
IGoodsBiz igb=new GoodsBiz();
int pageIndex=1;
int pageSize=2;
//接收pid
String pid=request.getParameter("pid");
if(pid!=null){//说明点了x页
pageIndex=Integer.parseInt(pid);
}
//把pageIndex存起来 方便取
request.setAttribute("pageIndex", pageIndex);
//接收表单提交过来的关键字
String str=request.getParameter("gname");
if(str==null){
str="";//相当于查询全部
}
//把gname存起来
request.setAttribute("gname", str);
//获得最大页码
int max=igb.getMax("goods where gname like '%"+str+"%'", pageSize);
//把max存起来 方便取 ${max}
request.setAttribute("max", max);
//调用查询全部的方法
List<Goods> ls=igb.getAllByPage(pageIndex, pageSize, str);
if(ls.size()!=0){//集合中有数据
//把集合存起来=request中
request.setAttribute("myls", ls);
//跳转到展示页面进行展示 只能转发
request.getRequestDispatcher("index.jsp").forward(request, response);
}
else{
System.out.print("集合为空-优先检查数据库中是否有数据 DBHelper以及dao/biz");
}
%>
update.jsp:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="doupdate.jsp" method="post">
<!-- name值跟实体类的属性名一致 -->
商品序号:<input type="text" name="gid" value="${goods.gid}" readonly="readonly"><br>
商品名称:<input type="text" name="gname" value="${goods.gname}"><br>
商品价格:<input type="text" name="gprice" value="${goods.gprice}"><br>
<input type="submit" value="修改">
<input type="reset" value="清空">
</form>
</body>
</html>