JavaWeb学习总结(十七)EL表达式
语法格式:#
${expression}
1. 表达式支持算术运算符合逻辑运算符#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <%@ 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> <h2>EL表达式</h2> <table border= "1" bgcolor= "#aaaadd" > <tr> <td><b>表达式语言</b></td> <td><b>计算结果</b></td> <tr> <tr> <td>\${ 1 }</td> <td>${ 1 }</td> </tr> <tr> <td>\${ 1 + 2 }</td> <td>${ 1 + 2 }</td> <tr> <tr> <td>\${( 1 == 2 ) ? 3 : 4 } <td>${( 1 == 2 ) ? 3 : 4 } </tr> <tr> <td>\${ 1 > 2 }</td> <td>${ 1 > 2 }</td> </tr> <tr> <td>\${ 1 lt 2 }</td> <td>${ 1 lt 2 }</td> </tr> </table> </body> </html> |
结果:
2. 表达式语言的内置对象#
表达式语言包含了11个内置对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | <%@ 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> <h2>EL - 内置对象</h2> <form action= "" method= "post" > 名字: <input type= "text" name= "name" value=${param[ 'name' ] }> <input type= "submit" value= "提交" > </form><br/> <% session.setAttribute( "user" , "张三" ) ; Cookie c = new Cookie( "name" , "scott" ); c.setMaxAge( 24 * 3600 ); response.addCookie(c); %> <table border= "1" width= "600" bgcolor= "#aaaadd" > <tr> <td width= "170" ><b>功能</b></td> <td width= "200" ><b>表达式语言</b></td> <td width= "300" ><b>计算结果</b></td> </tr> <!-- 使用两种方式获取请求参数值 --> <tr> <td>获取请求参数值</td> <td>\${param.name }</td> <td>${param.name }</td> </tr> <tr> <td>获取请求参数值</td> <td>\${param[ 'name' ]}</td> <td>${param[ 'name' ] }</td> </tr> <!-- 获取请求头的信息 --> <tr> <td>获取请求头的值</td> <td>\${header[ 'user-agent' ]}</td> <td>${header[ 'user-agent' ] }</td> </tr> <!-- 获取Web应用的初始化参数值 --> <tr> <td>获取初始化参数值</td> <td>\${initParam.author}</td> <td>${initParam.author}</td> </tr> <!-- 获取session的属性值 --> <tr> <td>获取session的属性值</td> <td>\${sessionScope.user}</td> <td>${sessionScope.user}</td> </tr> <!-- 获取Cookie的属相值 --> <tr> <td>获取session的属性值</td> <td>\${cookie.name.value}</td> <td>${cookie.name.value}</td> </tr> </table> </body> </html> |
结果:
3. EL的自定义函数#
自定义函数的开发步骤:
3.1 开发函数处理类
该处理类必须包含静态方法,每一个静态方法都可以定义成一个函数
\src\el\Functions.java
1 2 3 4 5 6 7 8 9 10 11 12 13 | package el; public class Functions { //对字符串进行反转 public static String reverse (String text){ return new StringBuffer(text).reverse().toString(); } //返回字符串的长度 public static int countChar(String text){ return text.length(); } } |
3.2 使用标签库定义函数
\WEB-INF\tlds\mytaglib.tld
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?xml version= "1.0" encoding= "UTF-8" ?> <taglib xmlns= "http://java.sun.com/xml/ns/j2ee" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version= "2.0" > <description>itcast</description> <display-name>itcast-function</display-name> <tlib-version> 1.0 </tlib-version> < short -name>it</ short -name> <uri>http: //localhost/functions</uri> <!-- 定义第一个函数 --> <function> <!-- 定义函数名 --> <name>reverse</name> <!--定义函数处理类 --> <function- class >el.Functions</function- class > <!--定义函数的实现方法 --> <function-signature>java.lang.String reverse(java.lang.String)</function-signature> </function> <!-- 定义第二个函数 --> <function> <!-- 定义函数名 --> <name>countChar</name> <!--定义函数处理类 --> <function- class >el.Functions</function- class > <!--定义函数的实现方法 --> <function-signature> int countChar(java.lang.String)</function-signature> </function> </taglib> |
3.3 在JSP页面EL中使用函数
Web03\WebContent\EL\ELDemo3.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <%@ page language= "java" contentType= "text/html; charset=UTF-8" pageEncoding= "UTF-8" %> <%@ taglib prefix= "it" uri= "http://localhost/functions" %> <!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> ${it:reverse(param.name)} ${it:countChar(param.name)} </body> </html> |
访问结果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了