EL/JSTL
目的就是去掉JSP中的java代码,都是以标签的形式表示,以标签的形式美工或者前端都可以修改,利于团队的合作。
这套标签和java语言无关。
JSP核心语法:JSP表达式<%=%> JSP脚本<%%>
JSP开发的原则:尽量在JSP页面少写甚至不写java代码,Java代码放在Servlet。
EL
从域对象中取出数据(从setAttribute里)。
语法:${变量或表达式} ${list}
内置对象:PageContxt(当前这个JSP页面)、Request、Session、ServletContext(就是Application)
会从四个域对象中取某个值${}
依次从page域、request域、session域、application域获取值,在某个域中找到不再往后找。
就是现在小的域范围找,找不到再去大的范围找。
例
- 获得普通字符串
- 获得Student对象的值
- 获得List的值
<%@ page import="SERVLET.entity.Student" %>
<%@ page import="java.util.ArrayList" %><%--
Created by IntelliJ IDEA.
User: 15387
Date: 2021/8/20
Time: 14:16
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
request.setAttribute("name","zhangsan");
%>
${name}<br>
<%
Student student = new Student(1,"zhangsan",23,"nan");
session.setAttribute("student",student);
%>
${student}<br>
${student.id}<br>
${student.name}<br>
<%
Student student1 = new Student(1,"lisi",27,"nan");
Student student2 = new Student(2,"wangwu",32,"nv");
Student student3 = new Student(3,"zhaoliu",62,"nv");
ArrayList<Student> list = new ArrayList<>();
list.add(student1);
list.add(student2);
list.add(student3);
application.setAttribute("studentList",list);
%>
${studentList}<br>
${studentList[1]}<br>
${studentList[2].name}<br>
</body>
</html>
- 用EL表达式得到当前项目的路径
<%=request.getContextPath()%>
//变成
${pageContext.request.contextPath}
JSTL
JSTL(JSP Standard Tag Library),JSP标准标签库,可以嵌入在jsp页面中使用标签的形式完成业务逻辑等功能。
例
- if
- 多条件判断
- 3、集合遍历
3.1、遍历List< String >
3.2、遍历List< Student >
3.3、遍历MapMap< String,String >
<%@ page import="java.util.HashMap" %>
<%@ page import="SERVLET.entity.Student" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %><%--
Created by IntelliJ IDEA.
User: 15387
Date: 2021/8/20
Time: 14:56
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>//一定要加这句
<html>
<head>
<title>Title</title>
</head>
<body>
<%
request.setAttribute("age", 18);
request.setAttribute("gender", "女");
%>
<%--1、if--%>
<c:if test="${student == 20}">
已经20岁了
</c:if>
<c:if test="${student != 20}">
不是20岁
</c:if>
<%--2、多条件判断--%>
<c:set var = "score" value="70" scope="request"></c:set>
<c:choose>
<c:when test="${score >= 90 && score <= 100}">
成绩优秀
</c:when>
<c:when test="${score >= 80 && score <= 90}">
成绩良好
</c:when>
<c:when test="${score >= 70 && score <= 80}">
成绩一般
</c:when>
<c:when test="${score >= 60 && score <= 70}">
成绩及格
</c:when>
<c:otherwise>
不及格
</c:otherwise>
</c:choose>
<!--
3、集合遍历
3.1、遍历List<String>
3.2、遍历List<Student>
-->
<br/>
<c:forEach begin="0" end="5" var="i">
${i}<br/>
</c:forEach>
<!-- 遍历List<Student> -->
<%
List<Student> list = new ArrayList();
Student student1 = new Student(1, "张三1", 23, "男");
Student student2 = new Student(2, "张三2", 24, "女");
Student student3 = new Student(3, "张三3", 29, "男");
list.add(student1);
list.add(student2);
list.add(student3);
application.setAttribute("list", list);
%>
<c:forEach items="${list}" var="student">
${student.id}---${student.name}---${student.age}---${student.gender}
</c:forEach>
<!--
3.3、遍历Map<String,String>
-->
<%
Map<String, String> map = new HashMap<String, String>();
map.put("cn", "中国");
map.put("us", "美国");
request.setAttribute("map", map);
%>
<c:forEach items="${map}" var="name">
${name.key}---${name.value}
</c:forEach>
</body>
</html>