EL是Expression Language的缩写,翻译过来及时表达式语言。
格式:${表达式}
为什么会出现EL表达式子,这是为了JSP页面的数据输出,用来简化之前的繁琐写法。看个例子:
<%
pageContext.setAttribute("key1","pageContext域");
%>
<%--看这四个域中是否有值--%>
pageContext:<%=pageContext.getAttribute("key1")%><br>
pageContext:${key1}
这两个输出结果都是pageContext:pageContext域
虽然之前介绍了jsp中的表达式脚本写法,已然是很简洁了,但是el表达式是不是更简洁,越是简洁越是受欢迎。
说到这里是不是有很多人很疑惑,${表达式},这样写我们到底是去找那个域对象呢?,jsp中可是有四个域对象的。首先按照key值去找,如果key值相同的情况下,则是从小到大的顺序来找,如下:
<%
/*pageContext ====>>> request ====>>> session ====>>> application*/
//向着四个域中分别保存数据
pageContext.setAttribute("key","pageContext域");
request.setAttribute("key","request域");
session.setAttribute("key","session域");
application.setAttribute("key","application域");
%>
<%--看这四个域中是否有值--%>
${key}<%--pageContext域--%>
按照pageContext -------> request ---------> session ----------> application这个顺序来找的。
首先准备Student类:
package com.wang.domain;
import java.util.List;
import java.util.Map;
public class Student {
private String name;
private Integer age;
private Integer num;
private List<String> hobby;
private Map<String,Object> map;
private Dog dog;
public Student() {
}
public Student(String name, Integer age, Integer num, List<String> hobby, Map<String, Object> map, Dog dog) {
this.name = name;
this.age = age;
this.num = num;
this.hobby = hobby;
this.map = map;
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
public List<String> getHobby() {
return hobby;
}
public void setHobby(List<String> hobby) {
this.hobby = hobby;
}
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", num=" + num +
", hobby=" + hobby +
", map=" + map +
", dog=" + dog +
'}';
}
}
Dog类:
public class Dog {
private String name;
private Integer age;
public Dog() {
}
public Dog(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
jsp:
<%@ page import="com.wang.domain.Student" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.wang.domain.Dog" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
<%
Student student = new Student();
student.setName("wang");
student.setAge(20);
student.setNum(2017);
List list = new ArrayList<>();
list.add("java");
list.add("python");
list.add("go");
student.setHobby(list);
Dog cai = new Dog("旺财", 5);
student.setDog(cai);
Map map = new HashMap<>();
map.put("key1","你好");
map.put("key2","他好");
student.setMap(map);
application.setAttribute("stu",student);//存在application域中
%>
<%--用el表达式取出里面的值--%>
${stu}
浏览器显示结果:
Student{name='wang', age=20, num=2017, hobby=[java, python, go], map={key1=你好, key2=他好}, dog=Dog{name='旺财', age=5}}
这里可以显示任何属性,比如学生姓名:${stu.name}
,等等。
${1+54}
这个除了上面的还可以进行,关系运算、逻辑运算、算数运算、empty运算、三元运算,由于这个真的太简单了,就不一一举例子。
它的全称是JSP Standard Tag Library,翻译过来就是JSP的标准标签库
导包
taglibs-standard-impl-1.2.1.jar
taglibs-standard-spec-1.2.1.jar
引入标准库:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
常用的标签
if标签
${age}
choose标签
春天
夏天
秋天
冬天
foreach标签
<%
/*普通的for循环*/
for (int i = 0; i < 8; i++) {
System.out.println(i);
}
%>
<c:forEach begin="0" end="8" var="i" step="1">
${i}
</c:forEach>
<%
/*增强for循环*/
String[] a = {"wang","tai","lai"};
request.setAttribute("a",a);
%>
<c:forEach var="arr" items="${a}">
${arr}
</c:forEach>
上面的普通for循环中,begin循环起点,end是终点,var是循环体,step是步长。
增强for中就简单多了,就两个。