在上一期讲了EL表达式和JSTL标签,这期来讲讲EL表达式与JSTL标签的用法,在了解EL表达式与JSTL标签后又可以将网页中的Java代码答题一部分,从而达到Java代码与网页代码进一步分离的效果,大大降低耦合性,下面直接开始上代码吧
IGoodsDao.java:
package com.zking.dao;
import java.util.List;
import com.zking.pojo.Goods;
public interface IGoodsDao {
//查询所有
List<Goods> getAll(int page);
//查询单个
Goods getOne(Integer id);
//查询所有页数
int getRowCount();
}
GoodsDaoImpl.java:
package com.zking.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.zking.dao.IGoodsDao;
import com.zking.pojo.Goods;
import com.zking.pojo.User;
import com.zking.util.DBHelper;
public class GoodsDaolmpl implements IGoodsDao{
private Connection con;
private PreparedStatement ps;
private ResultSet rs;
@Override
public List<Goods> getAll(int page) {
int size=5;//页面数据条数
int begin=(page-1)*size+1;
int end=page*size;
List<Goods> list = new ArrayList<Goods>();
try {
con=DBHelper.getCon();
String sql="select * from (select a.*,rownum myr from shop_goods a) b where myr between ? and ?";
ps=con.prepareStatement(sql);
ps.setInt(1,begin);
ps.setInt(2,end);
rs=ps.executeQuery();
while(rs.next()) {
Goods goods=new Goods();
goods.setId(rs.getInt(1));
goods.setName(rs.getString(2));
goods.setPrice(rs.getInt(3));
goods.setInfo(rs.getString(4));
list.add(goods);
}
return list;
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.close(con, ps, rs);
}
return list;
}
@Override
public Goods getOne(Integer id) {
try {
con = DBHelper.getCon();
ps = con.prepareStatement("select * from shop_goods where id=?");
ps.setInt(1, id);
rs = ps.executeQuery();
if (rs.next()) {
Goods goods = new Goods();
goods.setId(rs.getInt(1));
goods.setName(rs.getString(2));
goods.setPrice(rs.getInt(3));
goods.setInfo(rs.getString(4));
return goods;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.close(con, ps, rs);
}
return null;
}
@Override
public int getRowCount() {
try {
con = DBHelper.getCon();
ps = con.prepareStatement("select count(1) from shop_goods");
rs = ps.executeQuery();
if(rs.next()) {
return rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.close(con, ps, rs);
}
return 0;
}
}
IGoodsBiz.java:
package com.zking.biz;
import java.util.List;
import com.zking.pojo.Goods;
public interface IGoodsBiz {
List<Goods> getAll(int page);
//查询单个
Goods getOne(Integer id);
//查询所有页数
int getRowCount();
}
GoodsBizImpl.java:
package com.zking.biz.impl;
import java.util.List;
import com.zking.biz.IGoodsBiz;
import com.zking.dao.IGoodsDao;
import com.zking.dao.impl.GoodsDaolmpl;
import com.zking.pojo.Goods;
public class GoodsBizlmpl implements IGoodsBiz{
private IGoodsDao goodsDao=new GoodsDaolmpl();
@Override
public List<Goods> getAll(int page) {
return goodsDao.getAll(page);
}
@Override
public Goods getOne(Integer id) {
return goodsDao.getOne(id);
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return goodsDao.getRowCount();
}
}
index.jsp:
<%@page import="org.apache.jasper.tagplugins.jstl.core.ForEach"%>
<%@page import="com.zking.pojo.Goods"%>
<%@page import="com.zking.biz.impl.GoodsBizlmpl"%>
<%@page import="com.zking.biz.IGoodsBiz"%>
<%@page import="com.zking.pojo.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Document</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css">
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
td:nth-child(3)::before{
content: "$";
}
</style>
</head>
<body>
<c:if test="${sessionScope.user == null }">
<c:redirect url="login.jsp"/>
</c:if>
<c:if test="${goods == null}">
<c:redirect url="doIndex.jsp"/>
</c:if>
<div class="jumbotron">
<div class="container">
<h1>欢迎光临嘿嘿嘿SuperMarket</h1>
<p>尊贵的${sessionScope.user.account }</p>
<p><a href="car.jsp" class="btn btn-primary"></a></p>
</div>
</div>
<div class="container">
<table class="table">
<tr>
<th>商品序号</th>
<th>商品名称</th>
<th>商品单价</th>
<th>商品描述</th>
<th>操作</th>
</tr>
<c:forEach items="${goods }" var="g">
<tr>
<td>${g.id}</td>
<td>${g.name}</td>
<td>${g.price}</td>
<td>${g.info }</td>
<td>
<div class="btn-group btn-group-xs">
<a href="doAddCar.jsp?id=${g.id}" class="btn btn-primary">添加购物车</a>
</div>
</td>
</tr>
</c:forEach>
</table>
<!-- style="text-align:center;" 居中 -->
<!-- 分页 -->
<div style="text-align:center;">
<ul class="pagination">
<li>
<a href="doIndex.jsp?ipage=${Math.max(ipage-1,1)}">
<span >«</span>
</a>
</li>
<c:forEach begin="1" end="${maxPage}" var="i">
<li class="${ipage==i?"active":" "}">
<a href="doIndex.jsp?ipage=${i}">${i}</a>
</li>
</c:forEach>
<li>
<a href="doIndex.jsp?ipage=${ipage+1}" >
<span >»</span>
</a>
</li>
</ul>
</div>
</div>
</body>
</html>
doIndex.jsp:
<%@page import="com.zking.biz.impl.GoodsBizlmpl"%>
<%@page import="com.zking.biz.IGoodsBiz"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String parameter = request.getParameter("ipage");
int ipage=1;//默认第一页
if(parameter!=null){
ipage=Integer.parseInt(parameter);
}
//得到商品的Biz
IGoodsBiz goodsBiz=new GoodsBizlmpl();
//将商品查询出来 并且放到请求域当中
request.setAttribute("goods", goodsBiz.getAll(ipage));
//算出也数,带导index.jsp
int row = goodsBiz.getRowCount();
request.setAttribute("maxPage", (int)Math.ceil(row*1.0/4)) ;
request.setAttribute("ipage", ipage);
//把数据携带导index.jsp
request.getRequestDispatcher("index.jsp").forward(request,response);
%>
car.jsp:
<%@page import="com.zking.vo.CarItem"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Document</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css">
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
td:nth-child(4)::before,
small {
content: "$";
}
* {
outline: none !important;
}
td,
th {
text-align: center;
}
input {
text-align: center;
}
</style>
</head>
<body>
<div class="jumbotron">
<div class="container">
<h1>欢迎光临嘿嘿嘿购物车</h1>
<p>尊贵的${user.account}</p>
<p>
<a href="doExit.jsp" class="btn btn-warning">退出登陆</a>
</p>
</div>
</div>
<table class="table">
<tr>
<th>商品序号</th>
<th>商品名称</th>
<th>商品个数</th>
<th>商品总价</th>
<th>操作</th>
</tr>
<c:forEach items="${car }" var="c">
<tr>
<td style="line-height: 30.5px;">${c.goods.id }</td>
<td style="line-height: 30.5px;">${c.goods.name }</td>
<td>
<div class="input-group" style="width: 120px;margin: auto;">
<span class="input-group-btn">
<a href="doUpdCar.jsp?id=${c.goods.id }&type=0" class="btn btn-default" type="button">-</a>
</span>
<input type="number" onblur="location.href='doUpdCar.jsp?id=${c.goods.id}&count='+this.value"
value="${c.count}" type="text" class="form-control">
<span class="input-group-btn">
<a href="doUpdCar.jsp?id=${c.goods.id}&type=1" class="btn btn-default" type="button">+</a>
</span>
</div>
</td>
<td style="line-height: 30.5px;">${c.sum}</td>
<td style="line-height: 30.5px;">
<a href="doDelCar.jsp?id=${c.goods.id}" class="btn btn-primary">删除</a>
</td>
</tr>
</c:forEach>
</table>
<h1 class="alert alert-info">
当前购物车总价
<small>xx</small>
<a href="doClear.jsp" class="btn btn-danger">点我结算</a>
</h1>
</body></html>
本期分享内容中使用了一部分EL表达式和JSTL标签代替了一部分Java代码
好了今天的内容就分享到这了