访问Map对象的元素值 ${map.key}
访问Collection对象的元素值${collection[i]}
访问数组的元素值${array[i]}
【index.jsp】
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<%
HashMap<String,String> map1 = new HashMap<String,String>();
map1.put("tom","maomao");
map1.put("jerry","shushu");
map1.put("xiaoming","pangzi");
map1.put("a b","aaaaa");
map1.put("{}","123");
//pageContext.setAttribute("m1",map1);
request.setAttribute("m1",map1); //和上面只是范围不一样
LinkedList<String> list = new LinkedList();
list.add("abc");
list.add("hello");
pageContext.setAttribute("list1",list);
%>
${m1['tom']}<br/>
${m1["jerry"]}<br/>
${m1.xiaoming }<br/>
${m1.tom }<br/> //建议这样写
${m1["a b"]}<br/> //特殊情况只能这样写
${m1["{}"]}<br/>
${list1[1] }<br/> //访问conection集合 数组一样
</body>
</html>
访问JavaBean的属性值${bean.attr}。注意javabean的属性名字是根据get、set的名字第一个字母小写得到的,和private变量的名字无关
【Dog.java】
package com.rupeng.web3;
public class Dog
{
private int hahaId;
private String name;
private Person master;
private String birthDay;
public String getBirthDay()
{
return birthDay;
}
public void setBirthDay(String birthDay)
{
this.birthDay = birthDay;
}
public int getId()
{
return hahaId;
}
public void setId(int id)
{
this.hahaId = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Person getMaster()
{
return master;
}
public void setMaster(Person master)
{
this.master = master;
}
}
【Person.java】
package com.rupeng.web3;
public class Person
{
private int id;
private String name;
private int age;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
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;
}
}
【el1.jsp】
测试见下
【注意】JavaBean的属性是getId 中的id 并不是private字段
el访问N个对象(*)
el内置了N个和jsp有关的map对象
pageScope -- page域对象中保存属性的map, pageContext.getAttribute ("key")
requestScope -- request域对象中保存属性的map,request..getAttribute ("key")
sessionScope -- session域对象中保存属性的map
applicationScope -- ServletContext域对象中保存属性的map
param -- request的所有请求参数的map ,request.getParameter("key")
paramValues -- request的所有请求参数值的map ,request.getParameterValues("key")
header -- request的所有请求头的map
headerValues-- request的所有请求头的map
cookie -- request的所有请求cookie的map
initParam -- 在web.xml中配置的初始化参数,servletContext.getInitparameter(Stringname)
例子:${requestScope.map1.rupeng}
对于pageScope , requestScope,sessionScope,applicationScope 这四个map对象 , 有一个统一简单的访问方式${key},el会依次访问这四个map对象直到找到key对应的value
建议:一般是放到requset里面,然后一次取,很少用到scope
【el1.jsp】
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="com.rupeng.web3.Person"%>
<%@page import="com.rupeng.web3.Dog"%>
<!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>
<%
Person p1 = new Person();
p1.setId(3);
p1.setName("tom");
p1.setAge(3);
Dog d1 = new Dog();
d1.setId(1);
d1.setName("wangcai");
d1.setMaster(p1);
d1.setBirthDay("2016-10-10");
//访问N个对象测试
pageContext.setAttribute("wangcai",d1);
pageContext.setAttribute("test","hell1");
request.setAttribute("test","hello2");
session.setAttribute("test","hello3");
application.setAttribute("test","hello4");
%>
name:${param.name}<br/>
UserAgent:${header["User-Agent"]}<br/>
${test}<br/>
${sessionScope.test}<br/>
${applicationScope.test}<br/>
${requestScope.test}<br/>
${wangcai.name}<br/> //访问javabean属性
${wangcai.id}<br/>
${wangcai.birthDay }<br/>
${wangcai.master.name}<br/>
</body>
</html>
从上面的格式可以看出 , el有两种访问数据的方式 . 和 [ ]
. 支持的 [ ] 都支持 , ${ xx.xxx } 等价于 ${ xx["xxx"]}或${ xx['xxx']}
[ ]支持动态访问map["aa"],map[aa]
[ ]还支持含有特殊字符的属性 map["aa bb"],必须用[""]了
----------------------------------------------------------------------------------------------------
【表达式运算符】
el中的常量 true false 1 1.1 "aa" null
算术表达式 + - * div / mod %
关系表达式 gt > >= ge < lt <= le == eq != ne
逻辑表达式 || && ! and or not
xx表达式 xx.xxx xx["xxx"]
三元表达式 xx?xxx:xxxx
empty 可以判断对象是否为null ,字符串是否为"" , map/ Collection对象的size是否等于0 , 但不能判断数组是否有元素
el可计算各种表达式的值(*)
【作用】不会用的那么复杂,一般是显示颜色、和简单格式。
【el2.jsp】
<%@ 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>
${5 div 3}<br/> //尽量用div gt等
${5 gt 3}<br/>
<%
request.setAttribute("age",3); //一般不会取自己页面的值,而是通过java代码forward重定向
%>
${age eq 9}<br/>
<span style="color:${age>10?'red':'green'}">我的年龄是${age}</span>
</body>
</html>
【什么是JSTL】
1. JavaServer Pages Standard Tag Library
2.Apache Jakarta 小组归纳汇总了jsp页面中常见的Java代码,开发了一套 自定义标签 来代替这些代码,后来,这套标签库被SUN 公司看中,定义为了标准标签库
3.Java Web 开发人员通用的标签库
和EL表达式配合使用,取代jsp页面中Java代码,提高代码的可读性、维护性、易用性
4.<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
5.使用alt+/快速完成uri,不用记
【JSP标准标签库JSTL】
xml和sql一般是不用的
学习该标签有助于学习往后的sprint等框架标签
知道这个标签是用来干什么的
掌握标签的属性用法
1.属性名
2.属性的作用
3.属性值的类型
4.属性值是否支持el表达式
【JSP标准标签库JSTL】
核心标签库
<c:out> 文本输出(默认转义输出)
<c:set><c:remove> 设置、删除 属性值
<c:catch> 异常捕获
<c:if> 和 <c:choose><c:when><c:otherwise> 条件选择
<c:forEach> <c:forToken> 循环迭代
掌握标签的属性用法:
属性名
属性的作用
属性值的类型
属性值是否支持el表达式
【ElTestSerlet.java】
package com.rupeng.web3;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ELTest1Servlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
req.setAttribute("age", 50);
req.setAttribute("name", "hello a>b<c>d"); //escapeXml属性默认为true使特殊字符转义输出
Person p1 = new Person();
p1.setId(5);
p1.setName("tom");
p1.setAge(50);
Person p2 = new Person();
p2.setId(6);
p2.setName("jerry");
p2.setAge(15);
List<Person> list = new LinkedList<Person>();
list.add(p1);
list.add(p2);
req.setAttribute("persons", list);
req.getRequestDispatcher("/ElTest1.jsp").forward(req, resp);
}
}
【然后配置web.xml】
【注意】uri="http://java.sun.com/jsp/jstl/core"
【核心标签库 <c:out>】
l <c:out> 输出一段文本内容到pageContext对象当前保存的“out”对象中
l <c:out>的属性
属性名 | 是否支持EL表达式 | 属性值类型 | 属性说明 |
value | true | Object | 用来指定要输出的内容 |
escapeXml | true | Boolean | true:对特殊字符进行转义 |
default | true | Object | 默认值,当value为null时输出 |
对应的主要Java代码
out.print(obj);
和${name}的区别是<c:out>支持escapeXml,对于可能有特殊字符的要用<c:out>
【核心标签库 <c:forEach>】
循环迭代标签可以迭代的对象:Collection、Map、Iterator、Enmuration、String、ResultSet
属性名 | 是否支持EL | 属性类型 | 属性说明 |
items | true | 集合等 | 将要迭代的集合对象 |
var | false | String | 代表当前迭代到的元素,并存入page中 |
begin | true | int | 迭代开始的索引位置 |
end | true | int | 迭代结束的索引位置 |
step | true | int | 迭代增量,也成为步长 |
varStatus | false | String | 为迭代状态对象命名,并放在page中 |
【ElTest1.jsp】
<%@ 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>
</head>
<body>
<c:out value="${name}" ></c:out>
<p>${name }</p>
<p><c:out value="${nickname}" default="${age}"/></p> //没有值时候默认输出default
<p><c:out value="${requestScope.age}" /></p>
<table>
<tr><td>id</td><td>name</td><td>age</td></tr>
<c:forEach items="${persons}" var="p"> //servlet 和表达式的结合使用
<tr style="color:${p.age gt 18?'red':'black' }"><td>${p.id }</td><td>${p.name }</td><td>${p.age }</td></tr>
</c:forEach>
</table>
</body>
</html>
实际上一般不在jsp中储存数据
【核心标签库 <c:set>(*)】
把数据存放到web域、Map、JavaBean中
属性名 | 是否支持EL表达式 | 属性值类型 | 属性说明 |
value | true | Object | 需要设置的变量的值 |
var | false | String | 需要设置的变量的名称 |
scope | false | String | 变量所放置的范围对象:page、request、session、application,和var配合使用 |
target | true | Object | 指定在JavaBean或Map中的变量,和property配合使用 |
property | true | String | 指定JavaBean的字段名或者Map的key。不能xx.xxx这样多段的 |
1)scope var是一对,用来在scope范围中设定名字为var的值;
2)target property是一对,是用来在通过target的EL表达式获取的对象中设置属性名为property的值。
【核心标签库 <c:remove> (*)】
l <c:remove>标签用于删除各种Web域中的属性。(不会在map list中删除)
其语法格式如下:
<c:remove var="varName"
属性名 | 支持EL表达式 | 属性值类型 | 属性说明 |
var | false | String | 需要删除的变量的名称 |
scope | false | String | 变量所属的范围对象:page、request、session、application,和var配合使用 |
【核心标签库 <c:catch> (*)】知道即可
l 用于捕获异常,仅仅是捕获,并不做处理。如果指定var属性,则会把异常对象存入page对象中
属性列表
属性名 | 是否支持EL表达式 | 属性类型 | 属性说明 |
var | false | String | 指定在page中的变量名 |
注意点
由于JSP有错误页面机制,所以一些关键的错误异常还是要跳转到错误页面,那些次要的异常可以使用此标签捕获
【ELTest2.jsp】
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="com.rupeng.web3.Person"%>
<%@page import="java.util.HashMap"%>
<%@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>
</head>
<body>
<c:set scope="request" var="name" value="rupeng"></c:set>
<c:set scope="session" var="name" value="baidu"></c:set>
<%
Person p1 = new Person();
request.setAttribute("p",p1);
HashMap<String,String> map = new HashMap();
session.setAttribute("map1",map);
%>
${requestScope.name }<br/> 一般不会在jsp中储存数据
${sessionScope.name }<br/>
<c:set target="${requestScope.p}" property="name" value="yzk"/>
${p.name}<br/>
<c:set target="${map1}" property="tom" value="123"/>
<p>
${sessionScope.map1.tom}
</p>
<p>
${name}
</p>
<c:remove var="name" scope="request"/>
<p>
${name}
</p>
<c:catch var="ex"> //存放到page当中
<%
String s = request.getParameter("name");
s.toString();
%>
</c:catch>
异常对象:<c:out value="${pageScope.ex}"></c:out>
异常堆栈:<c:out value="${ex.stackTrace}"></c:out>
</body>
</html>
【核心标签库 <c:if>】
l 用于条件判断
l 没有与if配套的else标签。如果要实现if..else甚至else if的效果,要用后面讲的<c:choose>
属性名 | 是否支持EL表达式 | 属性类型 | 属性说明 |
test | true | Boolean | 指定条件表达式 |
var | false | String | 为test判断的结果起一个名字,并存入scope指定的web域中 |
scope | false | String | page、request、session、application |
默认放到page里面
empty判断是否有元素
empty可以判断数组
【ELTest3Servlet.java】
package com.rupeng.web3;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ELTest3Servlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
req.setAttribute("age", 3);
req.setAttribute("name", "rupeng");
req.setAttribute("nickName", null);
List<String> names = new LinkedList<String>();
//names.add("fafdas");
//names.add("321");
req.setAttribute("names", names);
String[] names2={"afsadfa","342423"};
req.setAttribute("names2", names2);
req.getRequestDispatcher("/ElTest3.jsp").forward(req, resp);
}
}
【核心标签库 <c:choose>】
c:choose、c:when 、c:otherwise
这组标签可以用来代替if-else-if-else语句。和switch不一样,因为switch是判断单个变量的多个值,而when的条件可以各种各样。
属性
只有when标签有test属性,表示需要判断的表达式
注意点
c:choose需要包裹c:when和c:otherwise,
c:otherwise(可选)需要放到c:when标签后面
【ELTest3.jsp】
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="com.rupeng.web3.Person"%>
<%@page import="java.util.HashMap"%>
<%@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>
</head>
<body>
<c:if test="${age ge 18}" var="isCNR">
我们都是成年人啦
</c:if>
<c:out value="${pageScope.isCNR}"></c:out>
<c:if test="${nickName eq null}">
没有昵称
</c:if>
<c:if test="${empty nickName}"> //empty后面集合 变量
没有昵称
</c:if>
<c:if test="${not empty nickName}">
我的昵称是:<c:out value="${nickName}" />
</c:if>
<c:out value="${names[1]}"/>
<c:if test="${empty names}">
names为空
</c:if>
<c:if test="${empty names2}">
names2为空
</c:if>
<c:choose>
<c:when test="${age lt 18}">
未成年人
</c:when>
<c:when test="${age lt 50}">
青年人
</c:when>
<c:otherwise>//必须放到最后 可写可不写
老年人
</c:otherwise>
</c:choose>
<img src="fbb.jpg" />
</body>
</html>
【核心标签库 <c:url>】
1.c:url标签用来构造url地址,提供给其他html标签如a,img使用。
2.可以在构造时加入请求参数:直接在value属性值之后追加,还可以使用c:param添加(*)
3.能够处理虚拟路径到全路径的转换,因为他考虑了ContextPath。
除非明确知道在同级路径下或者确定的自路径下,否则不要用相对路径、更不要用"..",因为很容易乱套。建议其他情况都用从网 站根目录开始的绝对路径"/"。网站部署时候的contextPath可能和开发的时候不一样,还有可能没有contextPath
<img src="<c:url value='/fbb.jpg'/>"/> <img src="/fbb.jpg"/>
form中的action用c:url可以很好的解决全路径的问题
【核心标签库 <c:redirect>】
c:redirect 转发或者重定向
【el5.jsp】
<%@ 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>
</head>
<body>
<img src="/myweb3/fbb.jpg" />
<img src="<c:url value='/fbb.jpg'/>"/>
<p>
<c:url value='/fbb.jpg'/> //动态的获得url 不写死
</p>
<a href="<c:url value='/index.jsp' var='fPath' />">index.jsp</a>
<c:out value="${fPath}" />
<c:redirect url="/index.jsp"></c:redirect> //一般不会在el里重定向
</body>
</html>
【核心标签库 <c:param>】
作用:为c:url、c:redirect、 c:import(后面讲)标签添加请求参数,并对参数进行url编码,追加到url地址后面
用法:<c:param name="name" value="value" />
【el6.jsp】
<%@ 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>
</head>
<body>
<%
request.setAttribute("age",8);
%>
<a href="<c:url value='/index.jsp'><c:param name='name' value='如>鹏<'></c:param><c:param name='age' value='${age}'></c:param></c:url>">rupeng</a>
</body>
</html>
【JSTL函数(*) 】
用来在JSP页面中操作字符串
JSTL函数就是EL表达式的函数,需要在el表达式中使用,而EL函数实质上是Java类的静态方法。很少用。碰到再说
fn:toLowerCase fn:toUpperCase
fn:trim fn:escapeXml fn:length
fn:split fn:join
fn:indexOf fn:replace
fn:contains fn:containsIgnoreCase
fn:startsWith fn:endWith
fn:substring fn:substringAfter fn:substringBefore
碰到再说