一、EL表达式(expression language): 语法 ${...}
jsp中page指令有一个属性叫isELIgnored, 用来标记此页面是否忽略EL表达式, 默认为false
举个例子:
常规写法: <%=session.getAttribute("test")%>
EL表达式写法: ${sessionScope.test}或者${sessionScope["test"]}
实例1:EL变量
<%@ 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>Insert title here</title> </head> <body> <% session.setAttribute("test", "山东威海"); request.setAttribute("test", "山东"); %> ${sessionScope.test } <br>
${test } </body> </html>
知识点:EL变量
当前面不指定哪个作用域下面的值的时候, 会默认按照下面的顺序去找, 如果都找不到, 就输出一个空字符串
pageScope --> requestScope --> sessionScope --> applicationScope
实例2:[ ]和.
package com.hanqi.model; public class User { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User [name=" + name + ", age=" + age + "]"; } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.hanqi.model.User"%> <!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>Insert title here</title> </head> <body> <% User u = new User(); u.setName("小明"); u.setAge(20); session.setAttribute("user", u); %> <h2>EL表达式通过[]取属性</h2> ${user["name"] } ${user["age"] } <h2>EL表达式通过.取属性</h2> ${user.name } ${user.age } </body> </html>
知识点:[]和. 大多数情况下可以通用, 除了两种情况中
1, 表达式中含有特殊字符
错误: ${sessionScope.test_name}
正确: ${sessionScope["test_name"]}
2, 需要动态获取属性
sessionScope["test"]
[] 中可以放一个变量名, 但是点的后面不行
实例3:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.ArrayList"%> <!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>Insert title here</title> </head> <body> <% ArrayList<String> arr = new ArrayList<String>(); arr.add("北京"); arr.add("上海"); arr.add("南京"); arr.add("深圳"); arr.add("广州"); arr.add("杭州"); session.setAttribute("dis", arr); %> ${dis[2] }<!-- 可以通过获取下标(索引)取得集合中的结果集 --> </body> </html>
知识点:在EL表达式中[]也可以用来表示数组或者集合中的索引值
实例4:EL自动类型转换
<%@ 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>Insert title here</title> </head> <body> <form action="test.jsp" method="post"> <input type="text" name="num" /> <input type="submit" value="提交" /> </form> <% String num = request.getParameter("num"); System.out.println(num); %> ${param.num + 20 } <br> </body> </html>
每一次提交,都会自动添加20,然后把值输出
二、EL运算符
算术运算符: + - * /或者div %或者mod
关系运算符: ==或者eq !=或者ne <或者lt >或者gt <=或者le >=或者ge
逻辑运算符: &&或者and ||或者or !或者not
验证运算符: empty(empty 对于 null和"" 的判断都是返回true)
<%@ 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>Insert title here</title> </head> <body> <% session.setAttribute("test", "山东威海"); %> <h1>EL运算符</h1> ${3 + 5 } <br>${5 div 2 } <br>${3 == 5 } <br>${7 lt 8 } <br>${empty test } <br>${empty test123 } <br> ================= <br>empty对null和空字符串的判断 <br> empty对空字符串的判断:${empty ""} <br> empty对空的判断:${empty null} <br> ================= </body> </html>
三、JSTL(JSP Standard Tag Library)标准标签库:
实例1:c:out 标签
输出常量
value---直接赋值
输出变量
default---默认值
escapeXml---控制转义字符(默认为true, 如果需要转义, 设置为false)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ page import="java.util.ArrayList"%> <!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>Insert title here</title> <style type="text/css"> .container { border: 1px red solid; width: 80%; margin: 10px auto; } </style> </head> <body> <% session.setAttribute("test", "你好,我是session的一行字"); request.setAttribute("test", "你好,我是request的一行字"); %> <div class="container"> <h1>c:out 输出标签</h1> <hr> <c:out value="这是一个页面"></c:out> <br> <c:out value="${test }"></c:out> <br> <c:out value="${sessionScope.test }"></c:out> <br> <c:out default="默认值" value="${test1 }"></c:out> <br> <c:out value="你 好"></c:out> <br> <c:out escapeXml="false" value="你 好"></c:out> <br> <c:out escapeXml="true" value="你 好"></c:out> <!-- escapeXml="" ;是否转译特殊字符 --> </div> </body> </html>
实例2:c:set标签
<%@ 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 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>Insert title here</title> <style type="text/css"> .container { border: 1px red solid; width: 80%; margin: 10px auto; } </style> </head> <body> <div class="container"> <h1>c:set标签</h1> <hr> <!-- scope="":作用域,只变量作用在哪个作用域里面 var="":给变量一个名字 value="":赋值 如果scope不写,默认设置在pageContext里面 --> <c:set scope="session" var="tt" value="test的值"></c:set> <!-- 把一个属性为tt,属性值为 "test的值"的添加进session里面--> <c:out value="${tt }"></c:out> </div> </body> </html>
实例3:remove----只能用于页面内的变量移除
移除某个变量
var: 指定需要remove的那个变量名
scope: 两个scope中的变量名有重复的时候, 可以通过这个属性来确定移除的是哪个, 如果不指定, 则全部remove掉
<%@ 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 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>Insert title here</title> <style type="text/css"> .container { border: 1px red solid; width: 80%; margin: 10px auto; } </style> </head> <body> <% session.setAttribute("test", "你好,我是session的一行字"); request.setAttribute("test", "你好,我是request的一行字"); %> <div class="container"> <h1>c:remove标签</h1> <!-- 移除某个变量的值 --> <hr> <c:set var="t1" value="qqqq"></c:set> <br> <c:out value="${t1 }"></c:out> <br> <c:remove var="t1" /> <c:out value="${empty t1 }"></c:out> <br> <br> session: <c:out value="${sessionScope.test }"></c:out> <br> request: <c:out value="${requestScope.test }"></c:out> <br> <br> <c:remove var="test" scope="session" /> session: <c:out value="${sessionScope.test }"></c:out> <br> request: <c:out value="${requestScope.test }"></c:out> <br> <c:out value="${empty test }"></c:out> </div> </body> </html>
实例4:c:catch标签(相当于try...catch...)
可以在这个标签中放其他标签, 只有一个属性var
<%@ 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 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>Insert title here</title> <style type="text/css"> .container { border: 1px red solid; width: 80%; margin: 10px auto; } </style> </head> <body> <%!public void myexception() { throw new RuntimeException("这是瞎写的一个异常!"); }%> <div class="container"> <h1>c:catch标签</h1> <!-- 捕获一个异常! --> <hr> <c:catch var="error"> <% myexception(); %> </c:catch> <c:out value="${error }"></c:out> </div> </body> </html>
实例5:c:if--->流程控制标签
<c:if test="EL表达式" var="变量名(指的是test属性的返回值:true或者false)" scope="作用域范围" ></c:if>
choose---以下三个标签通常一起使用, 类似于switch
when
otherwise--可以不写
<%@ 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 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>Insert title here</title> <style type="text/css"> .container { border: 1px red solid; width: 80%; margin: 10px auto; } </style> </head> <body> <% session.setAttribute("score", 90); %> <div class="container"> <h1>c:if 标签</h1> <!-- 做流程控制 --> <hr> <c:if test="${score>60 }"> <c:out value="及格了"></c:out> </c:if> <hr> <c:choose> <c:when test="${score>60&&score<70 }"> <c:out value="及格了"></c:out> </c:when> <c:when test="${score>70&&score<90 }"> <c:out value="优秀"></c:out> </c:when> <c:when test="${score<60 }"> <c:out value="继续努力"></c:out> </c:when> <c:otherwise> <c:out value="完美"></c:out> </c:otherwise> </c:choose> </div> </body> </html>
实例6:小综合运用(通过输入成绩,可以立即判断在哪个等级范围内)
<%@ 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 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>Insert title here</title> <style type="text/css"> .container { border: 1px red solid; width: 80%; margin: 10px auto; } </style> </head> <body> <form action="index.jsp" method="post"> <input type="text" name="num" value="${param.num}" /> <input type="submit" value="提交" /> </form> <% session.setAttribute("score", 66); %> <div class="container"> <h1>c:if标签</h1> <%-- <hr> <c:if test="${score>60}"> <c:out value="及格了"></c:out> </c:if> --%> <hr> <c:choose> <c:when test="${score>60&&score<70}"> <c:out value="及格了"></c:out> </c:when> <c:when test="${score>70&&score<80}"> <c:out value="优秀"></c:out> </c:when> <c:when test="${score<60}"> <c:out value="继续努力"></c:out> </c:when> <c:otherwise> <c:out value="很棒"></c:out> </c:otherwise> </c:choose> </div> </body> </html>
实例7:c:forEach ---->循环控制标签
forEach
var: 设定一个变量值来存储从数组或者集合中遍历出来的值
items: 指定需要遍历的数组或者集合
begin, end: 指定需要遍历的起始位置
step: 每次遍历跳过的个数, 默认值是1
varStatus: 通过index(在原集合中的索引值), count(当前第几个), first(是否是第一个), last(是否是最后一个)来描述begin和end子集中的状态
注意first和last, 是判断的是否在输出结果中的第一个或者最后一个, 并不是原来的集合中
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ page import="java.util.ArrayList"%> <!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>Insert title here</title> <style type="text/css"> .container { border: 1px red solid; width: 80%; margin: 10px auto; } </style> </head> <body> <% ArrayList<String> arr = new ArrayList<String>(); arr.add("北京"); arr.add("上海"); arr.add("南京"); arr.add("深圳"); arr.add("广州"); arr.add("杭州"); session.setAttribute("dis", arr); %> <div class="container"> <h1>c:forEach 标签</h1> <hr> <h3>全部遍历</h3> <c:forEach var="d" items="${dis }"> <!-- 遍历dis这个集合,遍历出来的每一个元素叫做d --> <c:out value="${d }"></c:out> <br> </c:forEach> <hr> <h3>部分遍历</h3> <c:forEach var="d" items="${dis }" step="2"> <c:out value="${d }"></c:out> <br> </c:forEach> <hr> <h3>部分遍历2</h3> <c:forEach var="d" items="${dis }" begin="2" end="4"> <c:out value="${d }"></c:out> <br> </c:forEach> <hr> <h3>部分遍历3</h3> <c:forEach var="d" items="${dis }" begin="2" end="4" varStatus="dissta"> <c:out value="${d }"></c:out> <br> varStatus--index属性:<c:out value="${dissta.index }"></c:out> <br> <!-- 集合中索引位置,从0开始 --> varStatus--count属性:<c:out value="${dissta.count }"></c:out> <br> <!-- 当前排列,从1开始 --> varStatus--first属性:<c:out value="${dissta.first }"></c:out> <br> <!-- 判断在当前结果集中是不是第一个 --> varStatus--last属性:<c:out value="${dissta.last }"></c:out> <br> <!-- 判断在当前结果集中是不是最后一个 --> <c:out value="=============================="></c:out> <br> </c:forEach> </div> </body> </html>
实例8:forTokens(相当于字符串的split()方法)
<%@ 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 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>Insert title here</title> <style type="text/css"> .container { border: 1px red solid; width: 80%; margin: 10px auto; } </style> </head> <body> <% session.setAttribute("teststring", "0533-1234567890-110"); %> <div class="container"> <h1>c:forToken 标签</h1> <hr> <c:forTokens items="${teststring }" delims="-" var="r"> <c:out value="${r }"></c:out> <br> </c:forTokens> </div> <
<%@ 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 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>Insert title here</title><style type="text/css">.container {border: 1px red solid; 80%;margin: 10px auto;}</style></head><body><form action="index.jsp" method="post"><input type="text" name="num" value="${param.num}" /><input type="submit" value="提交" /></form><%session.setAttribute("score", 66);%><div class="container"><h1>c:if标签</h1><%-- <hr><c:if test="${score>60}"><c:out value="及格了"></c:out></c:if> --%><hr><c:choose><c:when test="${score>60&&score<70}"><c:out value="及格了"></c:out></c:when><c:when test="${score>70&&score<80}"><c:out value="优秀"></c:out></c:when><c:when test="${score<60}"><c:out value="继续努力"></c:out></c:when><c:otherwise><c:out value="很棒"></c:out></c:otherwise></c:choose></div></body></html>