jsp页面java代码如果太多的话会对后期的维护和业务扩展方面有很大限制
文章目录
jsp的表达式语言EL
1.jsp的表达式语言el
2.el(jsp的表达式标签)的主要作用
(1)用于从作用域中获取属性的对象在jsp页面进行呈现
(2)简化从作用域中获取对象的java语句
(3)用于从作用域(pageContext,request,session,application)中获取对象值。
(4)可以对数据进行简单的运算(四则运算和逻辑运算)
3.el表达式语言的语法
(1)从作用域中获取属性值对象【tomcat自动从小到大范围搜素获取,找不到不会报空指针,只是没有返回值】
${作用域属性[.子属性]}---[]意思是可选的
(2)从指定的作用域获取属性对象的值
${pageContext/request/session/applicationScope.对象属性[.属性]}
(3)没有给定作用域时,搜索顺序:pageContext-->request-->session-->application
jstl(jsp的标准标签库)
1.jstl称为jsp的标准标签库
2.jstl的主要作用
(1)使用标签替代jsp页面的java代码,主要是控制语句代码,减少jsp页面java代码量
(2)分离java代码和jsp页面
3.jstl核心标签库的使用【常用用法总结】
(1)导入jstl.jar包
(2)在jsp页面中引入标签库
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
解释:taglib:表示标签库
uri="....":表示核心标签库的位置
prefix="c":表示标签指令的前缀名称,用于对标签指令进行调用
(3)语法
<c:指令 属性="值">
</c:指令>
(4)核心标签库中控制指令的使用
1)判断语句
<c:if test="${条件表达式}">
......
</c:if>
2)循环语句
<c:forEach items="${集合或者数组}" var="集合元素" >
${集合元素对象[.属性]}
</c:forEach>
3)重定向标签
<c:redirect url="重定向目标页面" >
</c:redirect>
例子:使用el和jstl重构购物车案例
商品购物车案例
1.创建Product类,封装product对象
2.商品添加界面add.jsp
3.商品列表界面list.jsp,程序购物车(session对象)中的商品列表
4.商品修改界面update.jsp
5.商品数据处理页面doservice.jsp
List集合保存商品对象,最终设置到session对象中
实施步骤
1.创建实体类Product.java,总计=价格*数量
2.构建add.jsp页面,最后显示商品指向操作数据页面
3.构建商品数据处理页面doservice.jsp,用操作数op来做处理
4.构建list.jsp页面,把删除和修改操作链接到商品信息后面
5.构建update.jsp页面,前面处理要请求和响应的字符,并判断从session中获取的要修改的商品对象是否存在,不存在则做一个链接到添加界面
list.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" pageEncoding="UTF-8" %>
<%@ page import="java.util.*,com.bean.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%--导入jstl核心包--%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>显示商品信息</title>
</head>
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
//从session中获取商品集合对象
//List<Product>lspd = (List<Product>) session.getAttribute("lspd");
//if (lspd==null){
// out.println("没有任何商品存在,请先<a href='add.jsp'>添加</a>!");
// return;
// }
%>
<%--如果lspd数组为空,则重定向到add.jsp页面--%>
<c:if test="${lspd==null}">
<c:redirect url="add.jsp">
</c:redirect>
</c:if>
<%--如果lspd不为空,则执行下面语句--%>
<c:if test="${lspd!=null}">
<body>
<p align="center">商品列表</p>
<hr />
<table width="900" border="1" align="center">
<tr>
<td width="142" align="center" valign="middle">序号</td>
<td width="185" align="center" valign="middle">商品名</td>
<td width="133" align="center" valign="middle">价格</td>
<td width="133" align="center" valign="middle">数量</td>
<td width="133" align="center" valign="middle">小计</td>
<td width="134" align="center" valign="middle">操作</td>
</tr>
<%--
<%
int i = 0;//索引位置
for (Product pd:lspd){
%>
--%>
<c:forEach items="${lspd}" var="pd" varStatus="vs">
<tr>
<td align="center" valign="middle"><%--<%=(i+1)%>--%>${vs.index+1}</td>
<td align="center" valign="middle"><%--<%=pd.getPname()%>--%>${pd.pname}</td>
<td align="center" valign="middle"><%--<%=pd.getPrice()%>--%>${pd.price}</td>
<td align="center" valign="middle"><%--<%=pd.getNum()%>--%>${pd.num}</td>
<td align="center" valign="middle"><%--<%=pd.getAcount()%>--%>${pd.acount}</td>
<td align="center" valign="middle"><a href="doservice.jsp?op=3&pid=<%--<%=i%>--%>${vs.index}">删除</a> <a href="doservice.jsp?op=4&pid=<%--<%=i%>--%>${vs.index}">修改</a></td>
</tr>
<%--
<%
i++;
}
%>--%>
</c:forEach>
<tr>
<td align="center" valign="middle">总计:</td>
<td colspan="5" align="center" valign="middle"><%=session.getAttribute("sum")%>元</td>
</tr>
</table>
<hr />
<p> </p>
<p align="center"><a href="add.jsp">返回添加</a>!</p>
<p> </p>
</body>
</c:if>
</html>
update.jsp
<%@ page import="com.bean.Product" %>
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%--导入jstl核心包--%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>修改商品信息</title>
<script src="file:///C|/Users/顾念思成/AppData/Roaming/Adobe/Dreamweaver CS6/zh_CN/Configuration/Temp/Assets/eam1DF2.tmp/SpryValidationTextField.js" type="text/javascript"></script>
<link href="file:///C|/Users/顾念思成/AppData/Roaming/Adobe/Dreamweaver CS6/zh_CN/Configuration/Temp/Assets/eam1DF2.tmp/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
</head>
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
//从session中获取要修改的商品对象
//Product oldpd = (Product)session.getAttribute("oldpd");
//if(oldpd==null){
//out.println("未找到商品,返回<a href='doservice.jsp?op=5'>列表</a>!");
//return;
//}
%>
<c:if test="${olpd==null}">
没用找到该商品,请返回<a href='doservice.jsp?op=5'>列表</a>!
</c:if>
<c:if test="${olpd!=null}">
<body>
<form id="form1" name="form1" method="post" action="doservice.jsp?op=2">
<hr />
<table width="379" border="1" align="center">
<tr bgcolor="#FFFF99">
<td colspan="2" align="center">修改商品信息</td>
</tr>
<tr>
<td width="44">商品名:</td>
<td width="319"><label for="pname"></label>
<span id="sprytextfield1">
<input type="text" name="pname" id="pname" value="<%--<%=oldpd.getPname()%>--%>${olpd.pname}"/>
<span class="textfieldRequiredMsg">需要提供一个值。</span></span>
<input type="hidden" name="pid" id="pid" value="<%--<%=oldpd.getPid()%>--%>${olpd.pid}"/>
</td>
</tr>
<tr>
<td height="31">价格:</td>
<td><label for="price"></label>
<span id="sprytextfield2">
<input type="text" name="price" id="price" value="<%--<%=oldpd.getPrice()%>--%>${olpd.price}"/>
<span class="textfieldRequiredMsg">需要提供一个值。</span></span></td>
</tr>
<tr>
<td>数量:</td>
<td><label for="num"></label>
<span id="sprytextfield3">
<input name="num" type="text" id="num" value="<%--<%=oldpd.getNum()%>--%>${olpd.num}" />
<span class="textfieldRequiredMsg">需要提供一个值。</span></span></td>
</tr>
<tr>
<td colspan="2" bgcolor="#FFFFCC"><div align="center">
<input type="submit" name="submit" id="submit" value="提交" />
<input type="reset" name="reset" id="reset" value="取消" />
</div></td>
</tr>
</table>
<hr />
</form>
<p align="center"> </p>
<p align="center">显示商品<a href="doservice.jsp?op=5">列表</a>!</p>
<script type="text/javascript">
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1");
var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2", "currency");
var sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3", "integer");
</script>
</body>
</c:if>
</html>
add.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>添加商品信息</title>
<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" name="form1" method="post" action="doservice.jsp?op=1">
<hr />
<table width="379" border="1" align="center">
<tr bgcolor="#FFFF99">
<td colspan="2" align="center">添加商品信息</td>
</tr>
<tr>
<td width="44">商品名:</td>
<td width="319"><label for="pname"></label>
<span id="sprytextfield1">
<input type="text" name="pname" id="pname" />
<span class="textfieldRequiredMsg">需要提供一个值。</span></span></td>
</tr>
<tr>
<td height="31">价格:</td>
<td><label for="price"></label>
<span id="sprytextfield2">
<input type="text" name="price" id="price" />
<span class="textfieldRequiredMsg">需要提供一个值。</span><span class="textfieldInvalidFormatMsg">格式无效。</span></span></td>
</tr>
<tr>
<td>数量:</td>
<td><label for="num"></label>
<span id="sprytextfield3">
<input name="num" type="text" id="num" value="1" />
<span class="textfieldRequiredMsg">需要提供一个值。</span><span class="textfieldInvalidFormatMsg">格式无效。</span></span></td>
</tr>
<tr>
<td colspan="2" bgcolor="#FFFFCC"><div align="center">
<input type="submit" name="submit" id="submit" value="提交" />
<input type="reset" name="reset" id="reset" value="取消" />
</div></td>
</tr>
</table>
<hr />
</form>
<p align="center"> </p>
<p align="center">显示商品<a href="doservice.jsp?op=5">列表</a>!</p>
<script type="text/javascript">
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1");
var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2", "currency");
var sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3", "integer");
</script>
</body>
</html>
doservice.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" pageEncoding="UTF-8" %>
<%@ page import="java.util.List" %>
<%@ page import="com.bean.Product" %>
<%@ page import="javax.print.DocFlavor" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Date" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>商品数据处理</title>
<%
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
//获取操作码
String sop = request.getParameter("op");
//将操作数转为int类型
int op = Integer.parseInt(sop);
//从session中获取商品的集合对象
List<Product>lspd = (List<Product>) session.getAttribute("lspd");
Product pd = null; //用于封装从add.jsp页面传递过来的商品信息
int pid = -1;//商品在集合中的索引位置
switch (op){
case 1://添加
//获取add.jsp页面传递的商品数据
String pname = request.getParameter("pname");
String sprice = request.getParameter("price");
String snum = request.getParameter("num");
//将价格转换成float类型
float price = Float.parseFloat(sprice);
//将数量转换成整型
int num = Integer.parseInt(snum);
//将商品数据封装到Product对象中
pd = new Product(pname,price,num);
//判断从session中获取的商品集合是否为空
if(lspd==null){
lspd = new ArrayList<>();
}
lspd.add(pd); //将封装好的商品信息设置到集合中
session.setAttribute("lspd",lspd); //将商品集合保存到session范围
response.sendRedirect("doservice.jsp?op=5"); //重定向到该路径,二次请求,丢弃request设置的属性
break;
case 2://修改
//获取updata.jsp页面传递的商品数据
pname = request.getParameter("pname");
sprice = request.getParameter("price");
snum = request.getParameter("num");
//将价格转换成float类型
price = Float.parseFloat(sprice);
//将数量转换成整型
num = Integer.parseInt(snum);
//获取要修改的商品编号,商品编号是商品在集合中的索引位置
pid = Integer.parseInt(request.getParameter("pid"));
//将商品数据封装到Product对象中
pd = new Product(pname,price,num);
//判断从session中获取的商品集合是否为空
if(lspd==null){
out.println("没有任何商品存在,请先<a href='add.jsp'>添加</a>!");
return;
}
lspd.set(pid,pd); //将修改好的商品封装好更新到集合中
session.setAttribute("lspd",lspd); //将商品集合保存到session范围
response.sendRedirect("doservice.jsp?op=5"); //重定向到该路径,二次请求,丢弃request设置的属性
break;
case 3://删除
//获取要删除的商品编号
pid = Integer.parseInt(request.getParameter("pid"));
//判断从session中获取的商品集合是否空
if(lspd==null){
out.println("没有任何商品存在,请先<a href='add.jsp'>添加</a>!");
return;
}
lspd.remove(pid);//移除
session.setAttribute("lspd",lspd);
response.sendRedirect("doservice.jsp?op=5");
break;
case 4://查找
//获取要查找的商品pid(编号)
pid = Integer.parseInt(request.getParameter("pid"));
//判断session中获取的商品集合对象是否为空
if(lspd == null){
out.println("没有任何商品存在,请先<a href='add.jsp'>添加</a>!");
return;
}
Product oldpd = lspd.get(pid);//从集合中取出查找的pid编号的商品数据
oldpd.setPid(pid);//设置商品编号为集合的索引位置
session.setAttribute("oldpd",oldpd);
response.sendRedirect("update.jsp");
break;
case 5://显示
//商品总计的变量
float sum = 0.0f;
//判断session中获取的商品集合对象是否为空
if(lspd == null){
out.println("没有任何商品存在,请先<a href='add.jsp'>添加</a>!");
return;
}
for (Product opd:lspd){
sum+=opd.getAcount();
}
session.setAttribute("sum",sum);
response.sendRedirect("list.jsp?t="+new Date().getTime());//增加时间戳,防止页面缓存
break;
}
%>
</head>
<body>
</body>
</html>
Product类
package com.bean;
import java.io.Serializable;
public class Product implements Serializable {
private Integer pid; //商品序号
private String pname; //商品名称
private Float price; //商品价格
private Integer num; //商品数量
private Float acount; //总计
public Product() {
}
public Product(String pname, Float price, Integer num) {
this.pname = pname;
this.price = price;
this.num = num;
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Float getPrice() {
return price;
}
public void setPrice(Float prvice) {
this.price = prvice;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
public Float getAcount() {
acount =price*num;
return acount;
}
public void setAcount(Float acount) {
this.acount = acount;
}
}