由jsp中的<%=request.getAttribute(“ varName”)%>转变为EL实现: ${ varName }
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>el表达式</title>
</head>
<body>
<% /*设置参数*/
pageContext.setAttribute("name1","pageContext参数名:张飞");
request.setAttribute("name2","request参数名:黄忠");
session.setAttribute("name3","session参数名:刘备");
application.setAttribute("name4","application参数名:赵云");
pageContext.setAttribute("name","pageContext参数名:关羽");
request.setAttribute("name","request参数名:关羽");
session.setAttribute("name","session参数名:关羽");
application.setAttribute("name","application参数名:关羽");
%>
<%--获取参数--%>
<h1> <%="pageContext"%>+${name1}</h1><br>
<h1> <%="request"%>+${name2}</h1><br>
<h1> <%="session"%>+${name3}</h1><br>
<h1> <%="application"%>+${name4}</h1><br>
<%-- pageContext==>request==>session==>application--%>
<h1> <%="谁先出来呢"%>+${name}</h1><br>
</body>
</html>
由request中的request.getParameter(name);request.getParameterValues(name); 转变为EL中的param:接收的参数只有一个值;paramValues:接受的参数有多个值。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>el表达式</title>
</head>
<body>
<form action="elresponse.jsp" method="post">
姓名:<input type="text" name="name"><br>
爱好:<input type="checkbox" name="hobby" value="唱歌"> 唱歌<br>
<input type="checkbox" name="hobby" value="跳舞">跳舞<br>
<input type="checkbox" name="hobby" value="乐器">乐器<br>
<input type="submit" value="提交">
</form>
</body>
</html>
<%@ page import="java.lang.reflect.Array" %> <%@ page import="java.util.Arrays" %><%-- Created by IntelliJ IDEA. User: Administrator Date: 2021/1/29 Time: 9:42 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>el表达式</title> </head> <body> <%--设置编码格式--%> <% request.setCharacterEncoding("utf-8");%> <%--jsp中requese获取参数--%> <%="姓名是"+request.getParameter("name")%><br> <%="爱好是"+ Arrays.toString(request.getParameterValues("hobby"))%> <%-- el 获取参数 --%> <br> <h1>姓名是${param.name}</h1> <h1>爱好是${paramValues.hobby}</h1> <h1>第一个爱好是${paramValues.hobby[0]}</h1> </body> </html>
访问方式:$ { 对象名 . 属性名 } 或$ { 对象名 [“属性名”] } ,当要存取的属性名中包含一些特殊字符,或动态取值时用[]方式
<%@ page import="java.lang.reflect.Array" %> <%@ page import="java.util.Arrays" %> <%@ page import="com.entity.Person" %><%-- Created by IntelliJ IDEA. User: Administrator Date: 2021/1/29 Time: 9:42 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>elbean</title> </head> <body> <%--创建bean对象--%> <% Person person=new Person("zhangfei",30); pageContext.setAttribute("var","name"); pageContext.setAttribute("va2","age"); pageContext.setAttribute("person",person); %> <%--jsp读取--%> 姓名:<%= person.getName()%> 年龄:<%=person.getAge()%> <hr> <%--el获取一--%> 姓名:${person.name} 年龄:${person.age} <hr> <%--el获取二--%> 姓名:${person["name"]} 年龄:${person["age"]} <hr> 姓名:${person[var]} 年龄:${person[va2]} <hr> </body> </html>
访问方式与bean类似,${集合名[下标].属性}
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>elbean</title> </head> <body> <%--创建bean对象--%> <% List<String> list=new LinkedList<>(); list.add("zhangfei"); list.add("liubei"); list.add("guanyu"); Map<String,Integer> map=new HashMap<>(); map.put("zhanfei",20); map.put("liubei",30); map.put("guanyu",40); pageContext.setAttribute("list",list); pageContext.setAttribute("map",map); %> <%--el获取list一--%> 第一个list元素:${list[0]} 第二个list元素:${list[1]} 第三个list元素:${list[2]} <hr> <%--el获取map--%> 第一个map元素:${map["zhanfei"]} 第二个map元素:${map["guanyu"]} 第三个map元素:${map["liubei"]} <hr> </body> </html>
| 类别 | 标识符 | 描述 |
| JSP | pageContext | PageContext 处理当前页面 |
| 作用域 | pageScope | 同页面作用域属性名称和值有关的Map类 |
| requestScope | 同请求作用域属性的名称和值有关的Map类 | |
| sessionScope | 同会话作用域属性的名称和值有关的Map类 | |
| applicationScope | 同应用程序作用域属性的名称和值有关的Map类 | |
| 请求参数 | param | 根据名称存储请求参数的值的Map类 |
| paramValues | 把请求参数的所有值作为一个String数组来存储的Map类 | |
| 请求头 | header | 根据名称存储请求头主要值的Map类 |
| headerValues | 把请求头的所有值作为一个String数组来存储的Map类 | |
| Cookie | cookie | 根据名称存储请求附带的cookie的Map类 |
| 初始化参数 | initParam | 根据名称存储Web应用程序上下文初始化参数的Map类 |
6. 简单运算
| 算术运算符 | 说 明 | 范 例 | 运算结果 |
| + | 加 | ${1+2} | 3 |
| - | 减 | ${2-1} | 1 |
| * | 乘 | ${2*3} | 6 |
| / 或 div | 除 | ${16/5}或${16div5} | 3.2 |
| % 或 mod | 取余 | ${16%5}或${16mod5} | 1 |
| 关系运算符 | 说 明 | 范 例 | 运算结果 |
| ==或eq | 等于 | ${1==2}或${1 eq 2} | false |
| != 或ne | 不等于 | ${2!=1}或${1 ne 2} | true |
| < 或lt | 小于 | ${2<3}或${2 lt 3 } | true |
| > 或 gt | 大于 | ${16>5}或${16 gt 5} | true |
| <= 或 le | 小于等于 | ${16<=5}或${16 le 5} | false |
| >= 或 ge | 大于等于 | ${16>=5}或${16 ge 5} | true |
| 逻辑运算符 | 说 明 | 范 例 | 运算结果 |
| && 或 and | 与运算 | ${true&&true}或${true and true} | true |
| || 或or | 或运算 | ${true||false}或${true or false} | true |
| ! 或not | 非运算 | ${!true}或${not true } | false |

<!-- prefix属性用于指定库前缀 --> <!-- uri属性用于指定库的标识 --> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:if test =“EL条件表达式”> 满足条件执行 </c:if >
<c:choose > <c:when test =“EL表达式”> 满足条件执行 </c:when> … < c:otherwise> 不满足上述when条件时执行 </c:otherwise> </c:choose >
<c:forEach var=“循环变量” items=“集合”> … </c:forEach>
<%@ page import="java.util.List" %> <%@ page import="java.util.ArrayList" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core_1_1" %> <%-- Created by IntelliJ IDEA. User: Administrator Date: 2021/1/29 Time: 15:42 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>jstl的常用核心标签</title> </head> <body> <%--初始值--%> <% pageContext.setAttribute("score",59); String[] list={"11","22","33","44","55"}; pageContext.setAttribute("list",list); %> <%--设置标签--%> <%--默认作用域为page--%> <jsp:useBean id="person" class="com.entity.Person" scope="page"></jsp:useBean> <c:set target="${person}" property="name" value="zhangfei"></c:set> <c:set target="${person}" property="age" value="20"></c:set> <c:set var="name" value="guanyu"></c:set> <%--输出标签--%> <c:out value="${person.name}"></c:out> <%--zhangfei --%> <c:out value="${person.age}"></c:out> <%--20--%> <c:out value="${name}"></c:out> <%--guanyu--%> <hr/> <%--删除标签--%> <c:remove var="name"></c:remove> <c:out value="${name}" default="hello"></c:out> <%--hello--%> <%--单条件标签--%> <c:if test="${score<60}"> <c:out value="不及格"></c:out> </c:if> <hr> <%--多条件标签--%> <c:choose> <c:when test="${score<60}"> <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> <hr> <%--循环标签--%> <%--对list集合遍历,begin初始值,end结束值,循环包含begin和end,step表示间隔,items表示作用对象--%> <c:forEach var="ts" items="${list}" begin="1" end="4" step="2" > <c:out value="${ts}"></c:out> <%--22,44--%> </c:forEach> </body> </html>
<%@ page import="java.util.Date" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%-- Created by IntelliJ IDEA. User: Administrator Date: 2021/1/29 Time: 15:59 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>函数与格式</title> </head> <body> <%--加入变量--%> <c:set var="string" value="Hello World!"></c:set> <c:out value="${fn:contains(string,‘o‘)}"></c:out> <%--true--%> <c:out value="${fn:toUpperCase(string)}"></c:out> <%--HELLOWORLD--%> <c:out value="${fn:toLowerCase(string)}"></c:out><%--helloworld--%> <% Date date = new Date(); pageContext.setAttribute("date",date); %> <fmt:formatDate value="${date}" pattern="yyyy-MM-dd HH:mm:ss"></fmt:formatDate> </body> </html>
package com.entity;
import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.SimpleTagSupport;import java.io.IOException; public class HelloTag extends SimpleTagSupport { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public void doTag() throws JspException, IOException { JspWriter out = this.getJspContext().getOut(); out.println("自定义标签"+name); } }
<?xml version="1.0" encoding="ISO-8859-1"?> <taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" version="2.1"> <tlib-version>1.0</tlib-version> <short-name>my</short-name> <uri>http://rf.com</uri> <!-- Invoke ‘Generate‘ action to add tags or functions --> <tag> <name>helloTag</name> <tag-class>com.entity.HelloTag</tag-class> <body-content>empty</body-content> <attribute> <name>name</name> <required>true</required> </attribute> </tag> </taglib>
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2021/1/29 Time: 16:19 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="my" uri="http://rf.com" %> <html> <head> <title>自定义标签</title> </head> <body> <my:helloTag name="zhangfei"/> </body> </html>
原文:https://www.cnblogs.com/forever-fate/p/14345661.html