06.JSTL(基本标签库)

1、JSTL

  • 概述:

(JavaServer Page Strandard Tag Linrary):JSP标准标签库
是由Java社区所定制的,提供给JavaWeb开发人的一个通用的自定义标签库
Core:核心标签库
Format:格式化标签
SQL:sql标签库
XML:xml标签库
Function:常用函数标签库

2、使用

1、加入Jar包

jstl-1.2.jar

taglibs-standard-compat-1.2.5.jar

taglibs-standard-impl-1.2.5.jar

taglibs-standard-jstlel-1.2.5.jar

taglibs-standard-spec-1.2.5.jar

2、引入自定义标签描述文件
<%@ taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c” %>

3、核心标签库

表达式操作

a)输出:out

向页面输出内容,如果内容中存在特殊符号,将可以自动进行转义操作value:要输出的内容,可以是EL*default:当value中的内容为空,则输出该默认值^escapeXml:关闭转义操作

b)设置:set

将值存入作用域或者JavaBean对象中var:存入时所使用keyvalue:存入的值scope:存入值得作用域,默认值为pagepagerequestsessionapplication

c)移除:remove

从作用域中移除某个值var:被移除的键值对keyscope:移除的键值对所在的作用域

异常:catch

在页面上做异常处理var:当被该标签圈起来的代码发生异常的时候,会将异常信息存入该属性指定的变量中

d)流程控制:

分支
if:只有if没有else,更没有else-if
test:对应if语句后面括号中的内容,如果该test取值为true则执行if中的内容,否则不执行var:可以在对应作用域中声明变量,用于存放test后面表达式的值scope:var指定的变量所存放的作用域
choose:对标if-else if-else

when:如果后面的test中的表达式为true,则执行when中的内容
test:结果为布尔类型的表达式,用于判断,决定when中的内容是否执行
otherwise:对应ifelse中的else,当所有的when都没有执行的时候,则执行该标签中的内容

迭代操作:forEach(迭代、创建循环)
创建循环var:声明循环变量begin:初始化循环变量end:设置循环条件step:步长迭代var:迭代出来的对象的keyitems:被去除数据的集合varStatus:当前迭代的状态index:当前取出值得索引值count:循环当前进行的次数first:如果当前去除的元素为第一个,则该值为truelast:如果当前取出元素为最后一个,则该值为true      如果迭代对象为Mapvar:指定的内容为map的键值对对象key:该键值对的keyvalue:该键值对的valueforTokens:切割字符串,迭代items:字符串来源(切割之前)delims:切割依据var:切割完成之后的迭代变量

案例:

jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:set value="小明" var="d1" scope="session"></c:set>
<c:set value="小红" var="d2" scope="session"></c:set>
<c:set value="小丽" var="d3" scope="session"></c:set>
<hr/>
<c:remove var="d2" scope="session"/>
<c:out value="${d1 }"></c:out><br/>
<c:out value="${d2 }"></c:out><br/>
<c:out value="${d3 }"></c:out><br/>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:if test="${param.tran<0 }"><c:out value="开暖气"></c:out>
</c:if>
<c:if test="${param.tran>0 and param.tran<=15 }"><c:out value="穿秋裤"></c:out>
</c:if>
<c:if test="${param.tran>15 and param.tran<=25 }"><c:out value="棉袄"></c:out>
</c:if>
<c:if test="${param.tran>25 and param.tran<=35 }"><c:out value="穿T恤"></c:out>
</c:if>
<c:if test="${param.tran>=40  }"><c:out value="去冰箱里冻着"></c:out>
</c:if>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body><c:if test="${param.sex=='男' }"><label><input type="radio" name="sex" value="男" checked="checked"/>男</label><label><input type="radio" name="sex" value="女"/>女</label>
</c:if>
<c:if test="${param.sex=='女' }">  <label><input type="radio" name="sex" value="男" />男</label><label><input type="radio" name="sex" value="女" checked="checked"/>女</label>
</c:if>
<hr/><c:if test=""></c:if></body>
</html>
<%@page import="java.util.Set"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.HashMap"%>
<%@page import="com.zuxia.model.StuInfo"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<%Map<String,StuInfo> map = new HashMap<String,StuInfo>();map.put("A",new StuInfo(1,"小张","男","1999-09-09","重庆市沙坪坝区",1.75f));map.put("B",new StuInfo(2,"小王","男","1999-09-09","重庆市沙坪坝区",1.75f));map.put("C",new StuInfo(3,"小李","男","1999-09-09","重庆市沙坪坝区",1.75f));map.put("D",new StuInfo(4,"小周","男","1999-09-09","重庆市沙坪坝区",1.75f));map.put("E",new StuInfo(5,"小找","男","1999-09-09","重庆市沙坪坝区",1.75f));map.put("F",new StuInfo(6,"小罗","男","1999-09-09","重庆市沙坪坝区",1.75f));pageContext.setAttribute("map", map);/* Set<String> stu = map.keySet();for(String s : stu){StuInfo sinfo = map.get(s);out.print(sinfo.getNo());out.print(sinfo.getName());out.print(sinfo.getAddress());out.print(sinfo.getBirthday());out.print(sinfo.getSex());out.print(sinfo.getHeight()+"<br/>");} */
%>
<body>
<table border="1"><c:forEach items="${map }" var="key"><tr><td>${pageScope.key.value.no }</td><td>${pageScope.key.value.name }</td><td>${pageScope.key.value.sex }</td><td>${pageScope.key.value.birthday }</td><td>${pageScope.key.value.address }</td><td>${pageScope.key.value.height }</td></tr></c:forEach>
</table>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<%String str = "123,1478,2589,3698587,25874867";pageContext.setAttribute("str", str);
%>
<body>
<c:forTokens items="${str }" delims="," var="s">${s }<br/>
</c:forTokens>
<hr/>
<c:forEach var="i" begin="1" end="50" step="5">你好+${i }<br/>
</c:forEach></body>
</html>
<%@page import="java.util.ArrayList"%>
<%@page import="com.zuxia.model.StuInfo"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%List<StuInfo> stus = new ArrayList<StuInfo>();stus.add(new StuInfo(1,"小张","男","1999-09-09","重庆市沙坪坝区",1.75f));stus.add(new StuInfo(2,"小王","男","1999-09-09","重庆市沙坪坝区",1.75f));stus.add(new StuInfo(3,"小李","男","1999-09-09","重庆市沙坪坝区",1.75f));stus.add(new StuInfo(4,"小周","男","1999-09-09","重庆市沙坪坝区",1.75f));stus.add(new StuInfo(5,"小找","男","1999-09-09","重庆市沙坪坝区",1.75f));stus.add(new StuInfo(6,"小罗","男","1999-09-09","重庆市沙坪坝区",1.75f));pageContext.setAttribute("stus", stus);
%>
<table border="1" align="center" width=500 height=250>
<caption><h3>学生信息表</h3></caption><c:forEach var="stu" items="${stus }" varStatus="state"><tr><td>${stu.no }</td><td>${stu.name }</td><td>${stu.sex }</td><td>${stu.birthday }</td><td>${stu.address }</td><td>${stu.height }</td><td>${state.index }</td><td>${state.count }</td><td>${state.first }</td><td>${state.last }</td></tr></c:forEach>
</table>
</body>
</html>

StuInfo.java

package com.zuxia.model;public class StuInfo {private int no;private String name;private String sex;private String birthday;private String address;private float height;public StuInfo(){};public StuInfo(int no, String name, String sex, String birthday, String address, float height) {super();this.no = no;this.name = name;this.sex = sex;this.birthday = birthday;this.address = address;this.height = height;}public String getAddress() {return address;}public String getBirthday() {return birthday;}public float getHeight() {return height;}public String getName() {return name;}public int getNo() {return no;}public String getSex() {return sex;}public void setAddress(String address) {this.address = address;}public void setBirthday(String birthday) {this.birthday = birthday;}public void setHeight(float height) {this.height = height;}public void setName(String name) {this.name = name;}public void setNo(int no) {this.no = no;}public void setSex(String sex) {this.sex = sex;}}

jstl使用案例:

案例1:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>表达式操作</h1>
<h2>输出标签</h2>
<%pageContext.setAttribute("msg", "早上好!");pageContext.setAttribute("bookName", "<<小学生行为守则>>");
%>
${msg }
<c:out value="${msg }"></c:out>
<hr/>
${bookName }
<!-- 如果在输出的内容中有特殊符号:><& -->
<c:out value="${bookName }"></c:out>
<hr/>
<c:out value="${age }" default="没有年龄属性存在于作用域中!"></c:out>
<hr>
<c:out value="${age }" default="没有年龄属性存在于作用域中!" escapeXml="false"></c:out>
</body>
</html>

案例2:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- page,request,session,application -->
<c:set var="dt1" value="2020-11-06" scope="session"></c:set>
<c:set var="dt2" value="2020-11-07" scope="session"></c:set>
<c:set var="dt3" value="2020-11-08" scope="session"></c:set><c:remove var="dt2" scope="request"/>${sessionScope.dt1 }<br/>
${sessionScope.dt2 }<br/>
${sessionScope.dt3 }<br/>
</body>
</html>

案例3:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body><c:catch var="e"><%int num = 10/1;%></c:catch>异常信息:${e }
</body>
</html>

案例4:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body><c:if test="${param.age>=18 }"><c:out value="可以进入"/></c:if><c:if test="${param.age<18 }"><c:out value="未成年人禁止入内"></c:out></c:if>
</body>
</html>

案例5:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body><c:if test="${param.age<0 }"><c:out value="开暖气"/></c:if><c:if test="${param.age<15 && param.age>=0 }"><c:out value="羽绒服"></c:out></c:if><c:if test="${param.age<25 && param.age>=15 }"><c:out value="棉袄"></c:out></c:if><c:if test="${param.age<35 && param.age>=25 }"><c:out value="T恤"></c:out></c:if><c:if test="${param.age<40 && param.age>=35 }"><c:out value="吹空调"></c:out></c:if><c:if test="${param.age>=40 }"><c:out value="避暑"></c:out></c:if>
</body>
</html>

案例6:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
${param.sex }
<hr/>
<c:if test="${param.sex eq '男' }"><label><input type="radio" name="sex" value="男" checked="checked">男</label><label><input type="radio" name="sex" value="女">女</label>
</c:if>
<c:if test="${param.sex eq '女' }"><label><input type="radio" name="sex" value="男">男</label><label><input type="radio" name="sex" value="女" checked="checked">女</label>
</c:if>
<hr/>
<label><input type="radio" name="sex1" value="男" ${param.sex eq '男'?'checked':'' }>男
</label>
<label><input type="radio" name="sex1" value="女"  ${param.sex eq '女'?'checked':'' }>女
</label>
</body>
</html>

案例7:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<hr/>
<c:if test="${param.sex eq '男' }" var="abc" scope="page"><c:out value="你好"></c:out>
</c:if><c:if test="${abc }"><c:out value="猛男"/>
</c:if>
<c:if test="${not abc }"><c:out value="你不太好 猛女"></c:out>
</c:if>
</body>
</html>

案例8:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>${param.age }<hr/><c:choose><c:when test="${param.age < 13 }">小学僧</c:when><c:when test="${param.age < 16 }">初中生</c:when><c:when test="${param.age < 19 }">高中生</c:when><c:when test="${param.age < 23 }">打工人</c:when><c:otherwise>空巢老人</c:otherwise></c:choose>
</body>
</html>

案例9:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"import="java.util.*,com.zuxia.entity.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">table,td,th{border:1px solid #CCCCCC;}table{width:100%;margin:60px auto;}
</style>
</head>
<body>
<%List<StuInfo> listSi = new ArrayList<StuInfo>();listSi.add(new StuInfo(1,"STU0001","小王","女",10,1.1f));listSi.add(new StuInfo(2,"STU0002","老张","女",10,1.1f));listSi.add(new StuInfo(3,"STU0003","老李","女",10,1.1f));listSi.add(new StuInfo(4,"STU0004","老赵","女",10,1.1f));listSi.add(new StuInfo(5,"STU0005","老罗","女",10,1.1f));listSi.add(new StuInfo(6,"STU0006","老龚","女",10,1.1f));pageContext.setAttribute("stus", listSi);/*out.print("<table>");for(StuInfo si : listSi){out.print("<tr>");out.print("<td>"+si.getStuId()+"</td>");out.print("<td>"+si.getStuNo()+"</td>");out.print("<td>"+si.getStuName()+"</td>");out.print("<td>"+si.getStuSex()+"</td>");out.print("<td>"+si.getStuAge()+"</td>");out.print("<td>"+si.getStuHeight()+"</td>");out.print("</tr>");}out.print("</table>");*/
%><table><caption>学生信息表</caption><tr><th>学生编号</th><th>学生学号</th><th>学生姓名</th><th>学生性别</th><th>学生年龄</th><th>学生身高</th></tr><c:forEach var="si" items="${pageScope.stus }" varStatus="sts"><tr><td>${pageScope.si.stuId }</td><td>${pageScope.si.stuNo }</td><td>${pageScope.si.stuName }</td><td>${pageScope.si.stuSex }</td><td>${pageScope.si.stuAge }</td><td>${pageScope.si.stuHeight }</td><td>${sts.index }</td><td>${sts.count }</td><td>${sts.first }</td><td>${sts.last }</td></tr></c:forEach>
</table><hr/>
<c:forEach var="i" begin="0" end="99" step="50">早上好+${i }<br/>
</c:forEach></body>
</html>

案例10:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"import="java.util.*,com.zuxia.entity.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">table,td,th{border:1px solid #CCCCCC;}table{width:100%;margin:60px auto;}
</style>
</head>
<body>
<%Map<String,StuInfo> mapStus = new HashMap<String,StuInfo>();mapStus.put("A",new StuInfo(1,"STU0001","小王","女",10,1.1f));mapStus.put("B",new StuInfo(2,"STU0002","老张","女",10,1.1f));mapStus.put("C",new StuInfo(3,"STU0003","老李","女",10,1.1f));mapStus.put("D",new StuInfo(4,"STU0004","老赵","女",10,1.1f));mapStus.put("E",new StuInfo(5,"STU0005","老罗","女",10,1.1f));mapStus.put("F",new StuInfo(6,"STU0006","老龚","女",10,1.1f));pageContext.setAttribute("stus", mapStus);
%>
<table><caption>学生信息表</caption><tr><th>MapKey</th><th>学生编号</th><th>学生学号</th><th>学生姓名</th><th>学生性别</th><th>学生年龄</th><th>学生身高</th></tr><c:forEach var="ent" items="${stus }"><tr><td>${pageScope.ent.key }</td><td>${pageScope.ent.value.stuId }</td><td>${pageScope.ent.value.stuNo }</td><td>${pageScope.ent.value.stuName }</td><td>${pageScope.ent.value.stuSex }</td><td>${pageScope.ent.value.stuAge }</td><td>${pageScope.ent.value.stuHeight }</td></tr></c:forEach>
</table><%/*Set<String> keys = mapStus.keySet();for(String key : keys){StuInfo si = mapStus.get(key);}*//*out.print("<table>");for(StuInfo si : listSi){out.print("<tr>");out.print("<td>"+si.getStuId()+"</td>");out.print("<td>"+si.getStuNo()+"</td>");out.print("<td>"+si.getStuName()+"</td>");out.print("<td>"+si.getStuSex()+"</td>");out.print("<td>"+si.getStuAge()+"</td>");out.print("<td>"+si.getStuHeight()+"</td>");out.print("</tr>");}out.print("</table>");*/
%></body>
</html>

案例11:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"import="java.util.*,com.zuxia.entity.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">table,td,th{border:1px solid #CCCCCC;}table{width:100%;margin:60px auto;}
</style>
</head>
<body>
<%String phoneNumbers = "110,120,119,123,321,1234567,7654321";String cardNumber = "6228-8510-4455-9358";//朱搏pageContext.setAttribute("pns", cardNumber);//String[] ns = phoneNumbers.split(",");//pageContext.setAttribute("ns", ns);/*for(String str : ns){out.print(str+"<br/>");}*/
%>
<hr/>
<c:forTokens items="${pns }" delims="-" var="pn">${pn }####<br/>
</c:forTokens>
</body>
</html>

StudentInfo.java

package com.zuxia.entity;public class StuInfo {private Integer stuId;private String stuNo;private String stuName;private String stuSex;private Integer stuAge;private Float stuHeight;public Integer getStuId() {return stuId;}public void setStuId(Integer stuId) {this.stuId = stuId;}public String getStuNo() {return stuNo;}public void setStuNo(String stuNo) {this.stuNo = stuNo;}public String getStuName() {return stuName;}public void setStuName(String stuName) {this.stuName = stuName;}public String getStuSex() {return stuSex;}public void setStuSex(String stuSex) {this.stuSex = stuSex;}public Integer getStuAge() {return stuAge;}public void setStuAge(Integer stuAge) {this.stuAge = stuAge;}public Float getStuHeight() {return stuHeight;}public void setStuHeight(Float stuHeight) {this.stuHeight = stuHeight;}public StuInfo() {super();}public StuInfo(Integer stuId, String stuNo, String stuName, String stuSex, Integer stuAge, Float stuHeight) {super();this.stuId = stuId;this.stuNo = stuNo;this.stuName = stuName;this.stuSex = stuSex;this.stuAge = stuAge;this.stuHeight = stuHeight;}@Overridepublic String toString() {return "StuInfo [stuId=" + stuId + ", stuNo=" + stuNo + ", stuName=" + stuName + ", stuSex=" + stuSex+ ", stuAge=" + stuAge + ", stuHeight=" + stuHeight + "]";}}

作业评讲:(实现商品的分页功能,增删改查)

使用JSTL完成简单的菜单信息管理页面1、完成对菜单表的增删改查操作实体类与数据表设计菜系信息系别ID系别名称系别简称菜单信息菜品ID菜品名称菜品单价所属菜系2、完成功能添加A、添加菜系B、添加菜品修改A、修改菜品(只允许修改菜品单价),每次修改一个菜品B、涨价功能,传递一个涨价幅度,每个菜品都做涨价操作删除A、删除菜品(删除单个菜品)查询(分页)A、查询所有菜品信息(查询结果包括:菜品名称,菜品单价,系别名称,系别简称)B、条件查询1)可以根据菜品系别模糊查询2)可以根据菜品名称模糊查询3)可以根据单价区间查询要求,自行设计数据库,页面美观,布局合理

jsp页面

进入首页

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2><a href="FoodServlet?op=query">点我进入首页</a></h2>
</body>
</html>

主页面

<%@page import="java.util.List"%>
<%@page import="com.zuxia.model.FoodInfo"%>
<%@page import="com.zuxia.util.PageModel"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>主页面</title>
<style type="text/css">.model-div a{margin-left: 15px;font-size: 18px;}
</style>
<script type="text/javascript" src="js/jquery-2.1.1.js"></script>
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.min.css" ><!-- 可选的 Bootstrap 主题文件(一般不用引入) -->
<link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap-theme.min.css" >
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
</head>
<%String serch = (String)request.getAttribute("foodName");String min = (String)request.getAttribute("minPrice");String max = (String)request.getAttribute("maxPrice");PageModel<FoodInfo> pList = (PageModel<FoodInfo>)request.getAttribute("foodList");List<FoodInfo> foodlist = pList.getList(); pageContext.setAttribute("food", foodlist);
%>
<body><div class="form-group"></div>
<form class="form-inline" method="post" action="FoodServlet?op=query"><!--  <div class="form-group"><label for="exampleInputName2">系别名称</label><input type="text" class="form-control" id="foodtype" name="foodtype" placeholder="模糊查询系别名称"></div> --><div class="form-group"><label for="exampleInputName2">菜品名称</label><input type="text" class="form-control"  id="food" name="food" placeholder="模糊查询菜品名称"></div><div class="form-group"><label for="exampleInputName2">单价查询</label><input type="text" class="form-control" id="Minprice" name="Minprice" placeholder="最低单价区间">~~<input type="text" class="form-control" id="Maxprice" name="Maxprice" placeholder="最高单价区间"></div><button type="submit" class="btn btn-default">查询</button>&nbsp; &nbsp; &nbsp;<a href="add.jsp" class="btn btn-info">添加菜品</a>&nbsp; &nbsp; &nbsp;<a href="Addcuisineinfo.jsp" class="btn btn-info">添加菜系</a>
</form><div class="form-group"><div class="form-group"><div class="col-sm-3">查询出来当前的数据</div></div>
<table class="table table-hover"><tr><th>菜品编号</th><th>菜品名称</th><th>菜品价格</th><th>菜品类型</th><th>操作</th></tr>
<c:forEach var="bumode" items="${food}"><tr><td>${bumode.foodId}</td><td>${bumode.foodName}</td><td>${bumode.foodPrice}</td><td>${bumode.typeId.typeName}</td><td><a href="javascript:if(confirm('你确定要修改吗?'))window.location='FoodServlet?op=up&foodId=${bumode.foodId} '"  class="btn btn-warning">修改</a>&nbsp; &nbsp; &nbsp;<a href="javascript:if(confirm('你确定要删除吗?'))window.location='FoodServlet?op=del&foodId=${bumode.foodId} '"  class="btn btn-warning">删除</a> </td> </tr></c:forEach>
</table><div class="model-div" style="text-align: center;"><a style="cursor:auto">总记录数:<%=pList.getCount() %>,<%=pList.getPageNo() %>/<%=pList.getPageSum() %></a><a href="FoodServlet?op=query&pageNo=1" >第一页</a><a href="FoodServlet?op=query&pageNo=<%=pList.getPageUp() %>" >上一页</a><a href="FoodServlet?op=query&pageNo=<%=pList.getPageDown() %>" >下一页</a><a href="FoodServlet?op=query&pageNo=<%=pList.getPageSum()%>&food=<%=serch==null?"":serch %>&Minprice=<%=max==null?"":max %>&Maxprice=<%=max==null?"":max %>" >最后一页</a></div></div>
</body>
<script type="text/javascript">/*
模糊查询传递文本框的值
*/
function SearchType(){var name=document.getElementById("food").value;//alert(name);location.href="FoodServlet?op=query&typename="+name;
}</script>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>查询所有菜品信息</title>
<style type="text/css">.div{width:680px;height:550px;margin-left: 340px;border:1px solid red;}table{text-align: center;width: 650px;}
</style>
</head>
<body>
<div class="div">
<table border="1"><caption><h3>菜品信息</h3></caption><thead><tr><td colspan="5" ><a href="#" style="float:right;margin: 5px 5px;">添加商品</a></td></tr><tr><th>菜品编号</th><th>菜品名称</th><th>菜品价格</th><th>菜品类型</th><th>操作</th></tr></thead><tbody><c:forEach items="${foodList }" var="food" ><tr><th>${food.foodId }</th><th>${food.foodName }</th><th>${food.foodPrice }</th><th>${food.typeId }</th><th><a href="javascript:if(confirm('你确定要删除吗?'))window.location='FoodServlet?op=del&foodId=${food.foodId} '">删除</a>/<a href="javascript:if(confirm('你确定要修改吗?'))window.location='FoodServlet?op=up&foodId=${food.foodId} '">修改</a></th></tr></c:forEach></tbody>
</table><div ><a style="cursor:auto">总记录数:,/</a><a href="#" >第一页</a><a href="#" >上一页</a><a href="#" >下一页</a><a href="<%-- FoodServlet?op=foodtype&pageNo=<%=pList.getPageSum()%>&typename=<%=serch==null?"":serch %> --%>" >最后一页</a></div>
</div>
</body>
</html>

添加页面

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加</title>
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.min.css" >
<script type="text/javascript" src="js/jquery-2.1.1.js"></script>
<!-- 可选的 Bootstrap 主题文件(一般不用引入) -->
<link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap-theme.min.css" >
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
</head>
<body><div class="form-group"></div>
<form class="form-horizontal" method="post" action="FoodServlet?op=add"><div class="form-group"><label for="" class="col-sm-2 control-label"></label><div class="col-sm-10">添加菜品</div></div><div class="form-group"><label for="inputPassword3" class="col-sm-2 control-label">菜品名称</label><div class="col-sm-6"><input type="text" class="form-control" name="food" id="inputPassword1" placeholder="菜品名称"></div></div><div class="form-group"><label for="inputPassword3" class="col-sm-2 control-label">菜品单价</label><div class="col-sm-6"><input type="text" class="form-control" name="price" id="inputPassword2" placeholder="菜品单价"></div></div><div class="form-group"><label for="inputPassword3" class="col-sm-2 control-label">所属菜系</label><div class="col-sm-6" placeholder="所属菜系"><select class="form-control" name="foodtype"><option value="1">鲁菜</option><option value="2">川菜</option><option value="3">粤菜</option><option value="4">苏菜</option><option value="5">闽菜 </option><option value="6">浙菜</option><option value="7">湘菜 </option><option value="8">徽菜</option></select></div></div><div class="form-group"><div class="col-sm-offset-2 col-sm-10"><input type="submit" class="btn btn-default" id="insert" value="添加菜品" />&nbsp; &nbsp; &nbsp;<a href="index.jsp"  class="btn btn-link">返回上一层</a> </div></div>
</form>
</body>
<script type="text/javascript">
$("#insert").click(function(){var name=$("#inputEmail3").val();//姓名var zh=$("#inputPassword1").val();//账号var pwd=$("#inputPassword2").val();//密码var phone=$("#inputPassword3").val();//电话$.ajax({url:"stuInfo.cn?op=insert",//请求路径type:"post",//请求方式dataType:"json",//返回类型data:{name:name,zh:zh,pwd:pwd,phone:phone},success:function(data){if(data.result=="成功"){window.location.href="index.jsp";alert("添加成功");}else{alert("添加失败");}console.log(data);},error:function(e){console.log(e);}})});</script>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.min.css" >
<script type="text/javascript" src="js/jquery-2.1.1.js"></script>
<!-- 可选的 Bootstrap 主题文件(一般不用引入) -->
<link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap-theme.min.css" >
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
</head>
<body><div class="form-group"></div>
<form class="form-horizontal" action="FoodServlet?op=addfoodtype" method="post"><div class="form-group"><label for="" class="col-sm-2 control-label"></label><div class="col-sm-10">添加菜系</div></div><div class="form-group"><label for="inputPassword3" class="col-sm-2 control-label">系别名称</label><div class="col-sm-6"><input type="text" class="form-control" name="foodtype" placeholder="系别名称"></div></div><div class="form-group"><label for="inputPassword3" class="col-sm-2 control-label">系别简称 </label><div class="col-sm-6"><input type="text" class="form-control" id="inputPassword2" name="text" placeholder="系别简称 "></div></div><div class="form-group"> <div class="col-sm-offset-2 col-sm-10"><input type="submit" class="btn btn-default" id="insert" value="添加菜系" />&nbsp; &nbsp; &nbsp;<a href="index.jsp"  class="btn btn-link">返回上一层</a> </div></div>
</form>
</body>
<script type="text/javascript"></script>
</html>

修改页面

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>修改</title>
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.min.css" ><!-- 可选的 Bootstrap 主题文件(一般不用引入) -->
<link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap-theme.min.css" >
<script type="text/javascript" src="js/jquery-2.1.1.js"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
</head>
<body><div class="form-group"></div>
<form class="form-horizontal"><div class="form-group"><label for="" class="col-sm-2 control-label"></label><div class="col-sm-10">修改当前用户的数据</div></div><div class="form-group"><label for="inputEmail3" class="col-sm-2 control-label">姓名</label><div class="col-sm-6"><input type="text" class="form-control" id="inputEmail3" placeholder="姓名"></div></div><div class="form-group"><label for="inputPassword3" class="col-sm-2 control-label">账号</label><div class="col-sm-6"><input type="text" class="form-control" id="zhang" placeholder="账号"></div></div><div class="form-group"><label for="inputPassword3" class="col-sm-2 control-label">密码</label><div class="col-sm-6"><input type="text" class="form-control" id="inputPassword2" placeholder="密码"></div></div><div class="form-group"><label for="inputPassword3" class="col-sm-2 control-label">手机</label><div class="col-sm-6"><input type="text" class="form-control" id="inputPassword3" placeholder="手机"></div></div><div class="form-group"><div class="col-sm-offset-2 col-sm-10"><button type="button" class="btn btn-default" id="update">修改</button>&nbsp; &nbsp; &nbsp;<a href="index.jsp"  class="btn btn-link">返回上一层</a> </div></div>
</form>
</body>
<script type="text/javascript">
$(function(){//获取idvar id=location.search.substring(location.search.indexOf('=')+1);$.ajax({url:"stuInfo.cn?op=getonly",//请求路径type:"post",//请求方式dataType:"json",//返回类型data:{id:id},success:function(d){    $("#inputEmail3").val(d.ss.name);$("#zhang").val(d.ss.account);$("#inputPassword2").val(d.ss.password);$("#inputPassword3").val(d.ss.phone);console.log(d);},error:function(e){console.log(e);   }});//确定修改$("#update").click(function(){var name=$("#inputEmail3").val();//姓名var zh=$("#zhang").val();//账号var pwd=$("#inputPassword2").val();//密码var phone=$("#inputPassword3").val();//电话$.ajax({url:"stuInfo.cn?op=update",//请求路径type:"post",//请求方式dataType:"json",//返回类型data:{id:id,name:name,zh:zh,pwd:pwd,phone:phone},success:function(d){    if(d.result=="成功"){alert("修改成功");window.location.href="index.jsp";}console.log(d);},error:function(e){console.log(e); }});});})
</script>
</html>

三层搭建

model(实体类)

package com.zuxia.model;public class FoodInfo {private int foodId;// int primary key auto_increment, -- 菜品编号private String foodName;// varchar(100) not null, -- 菜品名称private float foodPrice;// float not null, -- 菜品价格private FoodTypeInfo typeId;// int not null,  -- 所属菜系public int getFoodId() {return foodId;}public void setFoodId(int foodId) {this.foodId = foodId;}public String getFoodName() {return foodName;}public void setFoodName(String foodName) {this.foodName = foodName;}public float getFoodPrice() {return foodPrice;}public void setFoodPrice(float foodPrice) {this.foodPrice = foodPrice;}public FoodTypeInfo getTypeId() {return typeId;}public void setTypeId(FoodTypeInfo typeId) {this.typeId = typeId;}}
package com.zuxia.model;public class FoodTypeInfo {private int typeId;// int  primary key auto_increment, -- 菜系编号private String typeName;// varchar(20) not null unique,  -- 菜系名称private String feature;// text not null -- 菜系特色public int getTypeId() {return typeId;}public void setTypeId(int typeId) {this.typeId = typeId;}public String getTypeName() {return typeName;}public void setTypeName(String typeName) {this.typeName = typeName;}public String getFeature() {return feature;}public void setFeature(String feature) {this.feature = feature;}}

dao类(数据访问层)

package com.zuxia.dao;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;import com.zuxia.model.FoodInfo;
import com.zuxia.util.DBUtil;
import com.zuxia.util.PageModel;public class FoodDao {//定义连接对象Connection con = null;//定义预编译对象PreparedStatement pre = null;//定义数据集对象ResultSet rs = null;//创建一个预编译对象来得到prepublic PreparedStatement getPre(String sql) {//创建连接对象con = DBUtil.getCon();try {pre = con.prepareStatement(sql);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return pre;}//查询所有菜品信息public ArrayList<FoodInfo> queryFoodInfo(){ArrayList<FoodInfo> foodList = new ArrayList<FoodInfo>();pre = getPre("select * from food;");try {rs = pre.executeQuery();while(rs.next()) {FoodInfo food = new FoodInfo();food.setFoodId(rs.getInt("foodId"));food.setFoodName(rs.getString("foodName"));food.setFoodPrice(rs.getFloat("foodPrice"));food.setTypeId(new FoodTypeDao().queryFoodTypeId(rs.getInt("typeId")));foodList.add(food);}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {DBUtil.getClose(rs, pre, con);}return foodList;}//添加菜品public int addFoodInfo(String foodName,float price,int typeId) {int row = 0;pre = getPre("insert into food values(null,'"+foodName+"',"+price+","+typeId+");");try {row = pre.executeUpdate();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {DBUtil.getClose(rs, pre, con);}return row;}//删出指定菜系public int DelFoodInfo(int foodId) {int row = 0;pre = getPre("delete from food where foodid="+foodId+";");try {row = pre.executeUpdate();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {DBUtil.getClose(rs, pre, con);}return row;}//修改菜品public int UpFoodInfo(float price,int foodId) {int row = 0;pre = getPre("update food set foodprice="+price+" where foodid="+foodId+";");try {row = pre.executeUpdate();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {DBUtil.getClose(rs, pre, con);}return row;}//涨价幅度的修改public int UpFoodInfo(double num) {int row = 0;pre = getPre("update food set foodprice=foodprice*"+num+";");try {row = pre.executeUpdate();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {DBUtil.getClose(rs, pre, con);}return row;}//通过菜品类型名称查询指定的菜品类型信息public PageModel<FoodInfo> SearchFood(String foodName,int pageNo,int pageSize,float minPrice,float maxPrice){//实例化空的模型盒子PageModel<FoodInfo> model=new PageModel<FoodInfo>();ArrayList<FoodInfo> foodList=new ArrayList<FoodInfo>();//计算页码和总页数int pageNs=(pageNo-1)*pageSize;if(foodName!=null || minPrice!=0 || maxPrice!=0){pre=getPre("select * from food  where foodName like '%"+(foodName==null?"":foodName)+"%' and foodPrice>"+(minPrice!=0?minPrice:0)+" and foodPrice<="+(maxPrice!=0?maxPrice:"select MAX(foodPrice) from food")+" limit "+pageNs+","+pageSize+";");}else{pre=getPre("select * from food  limit "+pageNs+","+pageSize+";");}try {rs=pre.executeQuery();while(rs.next()){FoodInfo f=new FoodInfo();f.setFoodId(rs.getInt("foodId"));f.setFoodName(rs.getString("foodName"));f.setFoodPrice(rs.getFloat("foodPrice"));f.setTypeId(new FoodTypeDao().queryFoodTypeId(rs.getInt("typeId")));foodList.add(f);}//填充盒子//填充盒子模型model.setList(foodList);//查询出来的商品信息model.setPageNo(pageNo);//当前页码model.setPageSize(pageSize);//每页显示的数量model.setCount(getCount(foodName));//总条数} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{DBUtil.getClose(rs, pre, con);}return model;}//模糊查询的菜品数量总条数public int getCount(String like){int count=0;//创建连接对象con=DBUtil.getCon();try {pre=con.prepareStatement("select count(*) from food where foodname like '%"+(like==null?"":like)+"%';");//执行sql语句rs=pre.executeQuery();if(rs.next()){count=rs.getInt(1);}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{DBUtil.getClose(rs, pre, con);}return count;}}
package com.zuxia.dao;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;import com.zuxia.model.FoodTypeInfo;
import com.zuxia.util.DBUtil;public class FoodTypeDao {//创建数据集对象ResultSet rs = null;//定义连接对象Connection con = null; //定义预编译对象、PreparedStatement pre = null;//创建一个预编译对象来得到prepublic PreparedStatement getPre(String sql) {//创建连接对象con = DBUtil.getCon();try {pre = con.prepareStatement(sql);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return pre;}//通过菜系编号查询指定信息public FoodTypeInfo queryFoodTypeId(int typeId) {FoodTypeInfo foodtype = null;pre = getPre("select * from foodtype where typeid="+typeId+";");try {rs = pre.executeQuery();while(rs.next()) {foodtype =   new FoodTypeInfo();foodtype.setTypeId(rs.getInt("typeId"));foodtype.setTypeName(rs.getString("typeName"));foodtype.setFeature(rs.getString("feature"));}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {DBUtil.getClose(rs, pre, con);}return foodtype;}//查询所有菜系信息public ArrayList<FoodTypeInfo> queryRegionInfo(){ArrayList<FoodTypeInfo> list=new ArrayList<FoodTypeInfo>();pre=getPre("select * from foodtype;");try {rs=pre.executeQuery();while(rs.next()){FoodTypeInfo ft=new FoodTypeInfo();ft.setTypeId(rs.getInt("typeId"));ft.setTypeName(rs.getString("typeName"));ft.setFeature(rs.getString("feature"));list.add(ft);}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{DBUtil.getClose(rs, pre, con);}return list;}//添加菜系public int addFoodType(FoodTypeInfo foodtype) {int row = 0;pre = getPre("insert into foodtype values(null,'"+foodtype.getTypeName()+"','"+foodtype.getFeature()+"');");try {row = pre.executeUpdate();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {DBUtil.getClose(rs, pre, con);}return row;}
}

servlet(业务逻辑层)

package com.zuxia.servlet;import java.io.IOException;
import java.util.ArrayList;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.zuxia.dao.FoodDao;
import com.zuxia.dao.FoodTypeDao;
import com.zuxia.model.FoodInfo;
import com.zuxia.model.FoodTypeInfo;
import com.zuxia.util.PageModel;
@SuppressWarnings("serial")
@WebServlet("/FoodServlet")
public class FoodServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}FoodDao fDao = new FoodDao();@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//设置输出编码request.setCharacterEncoding("UTF-8");//接收op值String op = request.getParameter("op");//查询所有商品信息if(op.equals("query")) {doQueryFood(request, response);}//删除商品信息if(op.equals("del")) {doDelFood(request, response);}//添加菜品信息if(op.equals("add")) {doAddFood(request, response);}//添加菜系信息if(op.equals("addfoodtype")) {doAddFoodType(request, response);}}//添加菜系protected void doAddFoodType(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String foodtype = request.getParameter("foodtype");String text = request.getParameter("text");//实例化对象FoodTypeInfo food = new FoodTypeInfo();food.setTypeName(foodtype);food.setFeature(text);//调用方法int row = new FoodTypeDao().addFoodType(food);if(row>0) {request.getRequestDispatcher("index.jsp").forward(request, response);}}//添加菜品protected void doAddFood(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//接收菜品信息String food = request.getParameter("food");String price = request.getParameter("price");String foodtype = request.getParameter("foodtype");int typeId=0;float pe = 0f;if(price!=null) {pe=Float.parseFloat(price);}if(foodtype!=null) {typeId=Integer.parseInt((foodtype));}//调用添加菜品的方法int row = fDao.addFoodInfo(food,pe,typeId);if(row>0) {doQueryFood(request, response);}}//删除菜品信息protected void doDelFood(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//接收菜品foodIdString foodId = request.getParameter("foodId");int id = 0;if(foodId!=null) {id=Integer.parseInt(foodId);}//调用删除方法int row = fDao.DelFoodInfo(id);if(row>0) {doQueryFood(request, response);}}//查询所有菜品信息protected void doQueryFood(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//String foodtype=request.getParameter("foodtype");String food=request.getParameter("food");String minPrice=request.getParameter("Minprice");String maxPrice=request.getParameter("Maxprice");System.out.println(food);Float max=0f;Float min=0f;//定义一个空的盒子模型PageModel<FoodInfo> model=null;//设置当前页数和每页显示的条数int pageSize=5;int pno=1;//接收当前页数String pageNo=request.getParameter("pageNo");if(pageNo!=null){pno=Integer.parseInt(pageNo);}if(minPrice!=null&&minPrice!=""){min=Float.parseFloat((minPrice));}if(maxPrice!=null&&maxPrice!=""){max=Float.parseFloat((maxPrice));}//接收查询指定菜品的value值if(food!=null){//设置get请求编码food=new String(food.getBytes("ISO-8859-1"),"UTF-8");}model=fDao.SearchFood(food, pno, pageSize, min, max);//设置传入的值request.setAttribute("foodList", model);//设置传入的值request.setAttribute("minPrice", minPrice);request.setAttribute("maxPrice", maxPrice);request.setAttribute("foodName", food);//内部跳转request.getRequestDispatcher("index.jsp").forward(request, response);}}

util(工具类)

连接数据库工具类
package com.zuxia.util;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;public class DBUtil {/** 创建连接*/public static Connection getCon() {//定义连接对象Connection con=null;//定义连接数据库变量final String DB_URL="jdbc:mysql:///eateryDB?characterEncoding=utf-8";final String USER="root";final String PASS="123456";//加载驱动try {Class.forName("com.mysql.jdbc.Driver");//得到连接对象con=DriverManager.getConnection(DB_URL,USER,PASS);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return con;}//关闭连接public static void getClose(ResultSet rs,PreparedStatement pre,Connection con) {if(rs!=null) {try {rs.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(pre!=null) {try {pre.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(con!=null) {try {con.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
}
分页盒子模型
package com.zuxia.util;import java.util.List;public class PageModel<T> {//得到分页存放数据的集合private List<T> list;//得到总条数private int count;//每一页显示的条数private int pageSize;//当前页数private int pageNo;public List<T> getList() {return list;}public void setList(List<T> list) {this.list = list;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize = pageSize;}public int getPageNo() {return pageNo;}public void setPageNo(int pageNo) {this.pageNo = pageNo;}//上一页public int getPageUp(){int pageUp=this.pageNo-1;if(pageUp<=1){//当点击上一页等于1的时候 ,返回当前页为第一页return 1;}return pageUp;}//下一页public int getPageDown(){int pageDown=this.pageNo+1;//每当点击下一页的时候,在当前的页数上加一到下一页if(pageDown>=getPageSum()){return getPageSum();}return pageDown;}//总页数   数据的总条数%每一页需要的条数(两种情况)public int getPageSum(){//除尽的情况if(this.count%this.pageSize==0){return this.count/this.pageSize;}else{//除不尽  return this.count/this.pageSize+1;}}
}

贵族时代脚本

-- 创建数据库 并判断数据库是否存在
drop database if exists eateryDB;
create database eateryDB character set utf8;
-- 使用数据库
use eateryDB;-- 创建菜系类型表
drop table if exists foodtype;
create table foodtype
(typeid int  primary key auto_increment, -- 菜系编号typename varchar(20) not null unique,  -- 菜系名称feature text not null -- 菜系特色
);-- 创建菜品类型表
drop table if exists food;
create table food
(foodid int primary key auto_increment, -- 菜品编号foodName varchar(100) not null, -- 菜品名称foodPrice float not null, -- 菜品价格typeid int not null,  -- 所属菜系foreign key(typeid) references foodtype(typeid)
);
insert into foodtype values(null,'川菜','它以麻辣、鱼香、家常、怪味、酸辣、椒麻、醋椒为主要特点。');
insert into foodtype values(null,'粤菜','它以选料广泛,讲究鲜、嫩、爽、滑、浓为主要特点。它主要由广东菜、潮州菜和东江菜组成。');
insert into foodtype values(null,'鲁菜','选料精细、刀法细腻,注重实惠,花色多样,善用葱姜');
insert into foodtype values(null,'苏菜','制作精细,因材施艺、四季有别,浓而不腻,味感清鲜,讲究造型。  ');
insert into foodtype values(null,'浙菜','讲究刀工、制作精细、变化较多、富有乡土气息。');
insert into foodtype values(null,'闽菜','闽菜以烹制山珍海味而著称,在色香味形俱佳的基础上,尤以“香”、“味”见长,其清鲜、和醇、荤香、不腻的风格特色,以及汤路广泛的特点,在烹坛园地中独具一席。');
insert into foodtype values(null,'徽菜','它以烹制山珍野味著称,擅长烧、炖、蒸,而少爆炒。其烹饪芡大、油重、色浓、朴素实惠');
insert into foodtype values(null,'湘菜','以熏、蒸、干炒为主,口味重于酸、辣, 辣味菜和烟熏腊肉是湘菜的独特风味。 ');
insert into foodtype values(null,'冀菜','色香味型,讲究营养结构,咸鲜、浓香和酱香,花样品种繁多。');insert into food values(null,'干煸四季豆',25.5,1);
insert into food values(null,'口水鸡',15.5,1);
insert into food values(null,'辣子鸡丁',25.9,2);
insert into food values(null,'毛血旺',30,4);
insert into food values(null,'麻婆豆腐',13.5,5);
insert into food values(null,'蒜泥白肉',25.5,6);insert into food values(null,'鱼香肉丝',30.9,7);
insert into food values(null,'红烧乳鸽',30,8);
insert into food values(null,'麒麟鲈鱼',33.5,8);
insert into food values(null,'蜜汁叉烧',45.5,9);select * from foodtype;
select * from food;

06.JSTL(基本标签库)相关推荐

  1. EL表达式和JSTL标准标签库

    一.EL表达式 什么是EL表达式 EL(Express Lanuage)表达式可以嵌入在jsp页面内部 减少jsp脚本的编写 EL出现的目的是要替代jsp页面中脚本的编写. EL表达式的作用 EL最主 ...

  2. JSP→JSTL标准标签库简介与环境搭建、JSTL助手EL表达式EL隐式对象、标签→out、set、if、多选择配合、foreach、redirect、格式化、JSTL函数标签、自定义标签、标签类架构

    JSTL标准标签库简介与环境搭建 EL表达式与EL隐式对象 out标签 set标签 remove标签 catch标签 if标签 choose.when.otherwise配合标签 foreach标签 ...

  3. JSTL 核心标签库标签共有13个

    JSTL 核心标签库标签共有13个,功能上分为4类: 1.表达式控制标签:out.set.remove.catch 2.流程控制标签:if.choose.when.otherwise 3.循环标签:f ...

  4. JSP~~JSTL~~XML标签库

    XML 提供了非常灵活的方式来表示结构化的数据,可以创建自己的标记来识别数据,不仅仅人可以看懂,而且计算机也可以读懂.并且 XML 可以在不兼容的系统之间进行数据的交换,所以 XML 必定将称为最普遍 ...

  5. 深入分析JavaWeb Item21 -- JSTL核心标签库

    一.JSTL标签库介绍 JSTL标签库的使用是为弥补html标签的不足,规范自定义标签的使用而诞生的.使用JSLT标签的目的就是不希望在jsp页面中出现java逻辑代码. 1.1 使用方法 外包项目, ...

  6. java的jstl的标签库_java_jstl 标签库

    jstl标签库的使用以及介绍 jstl:jsp标准标签库,是jsp的标签集合,它里面封装了jsp通用的核心功能,比如:建构化的任务,迭代,条件判断,xml 文档的操作,国际化标签,sql标签,还提供框 ...

  7. Eclipse配置jstl标准标签库详解

    安装JSTL1.2 日期:2017-06-27 下载jstl1.2版本,下载地址:http://repo2.maven.org/maven2/javax/servlet/jstl/ 用压缩包打开jst ...

  8. java中jstl核心包丢失,小博老师解析Java核心技术 ——JSTL核心标签库

    [引言] JSTL(JSP Standard Tag Library,JSP标准标签库)是一个不断完善的开放源代码的JSP标签库,由四个定制标记库(core.format.xml 和 sql)和一对通 ...

  9. 早起的鸟儿有虫吃-JSTL核心标签库[收藏]

    介绍 JSTL 全名为Java Server Pages Standard Tag Library(JSP Standard Tag Library),它的中文名称为JSP 标准标签函数库. Web  ...

最新文章

  1. 牛客题霸 [ 数字在升序数组中出现的次数] C++题解/答案
  2. 【WebRTC---源码篇】(三)Windows/Linux音视频采集封装模块
  3. 领域应用 | 常识性概念图谱建设以及在美团场景中的应用
  4. java操作阿里云的对象存储OSS
  5. SpringCloud集成Seata精简入门教程
  6. 反转字符串中的元音字符
  7. web前端入门:CSS 样式书写规范
  8. 计算机网络课设之TCP通讯录
  9. python黑帽子怎么样_PYTHON 黑帽子第二章总结
  10. 苹果4s变php服务器,苹果4S改装无线充电
  11. 好好说话之hijack retaddr
  12. Win键失效解决方案+键盘检测器
  13. pyká Unlimited for Mac(项目管理软件)
  14. 揭晓计算机的神秘面纱——计算机是如何工作的(一)
  15. 缓冲区(1):为什么会有缓冲区(缓存)?
  16. Homekit智能家居DIY设备一智能灯泡
  17. 11大Java开源中文分词器的使用方法和分词效果对比(转)
  18. MDK V5.28来了,STM32G4也来了
  19. DIV布局——化妆品商城-功能齐全(43页) HTML+CSS+JavaScript web大作业 静态网页
  20. 计算机文化基础概括内容简介,计算机文化基础

热门文章

  1. IRremoteESP8266控制格力空调无反应问题记录
  2. python语言程序设计 陈东_《Python语言程序设计实践教程/全国高等院校重点规划教材·程序设计系列》陈东【摘要 书评 试读】- 京东图书...
  3. FTPC Purge概述
  4. 靠谱的软件开发公司怎样选择比较靠谱?(一)
  5. (kuangbin带你飞--最短路径)MPI Maelstrom(dijstra模板题)
  6. 12.2 票据背书需求分析
  7. 天津理工大学与北京信息科技大学的计算机,急!!北京信息科技大学 天津理工大学 天津工业大学 哪个好...
  8. 分享python爬虫视频教程
  9. CF607B - Zuma题解
  10. 今天你花里胡哨了吗 --- 定制属于自己的linux ssh迎宾信息