zoukankan      html  css  js  c++  java
  • JavaWeb学习总结(十七)EL表达式

    语法格式:

      ${expression}

    1. 表达式支持算术运算符合逻辑运算符

    <%@ 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个内置对象

    <%@ 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 开发函数处理类

      该处理类必须包含静态方法,每一个静态方法都可以定义成一个函数

    srcelFunctions.java

    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 ldsmytaglib.tld

    <?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中使用函数

    Web03WebContentELELDemo3.jsp

    <%@ 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>
    

      访问结果:

     

  • 相关阅读:
    UI设计常用网站 火树银花carol
    SQlite3创建数据库
    代码中的奥卡姆剃刀原理
    接口和上传服务器
    npm升级package.json依赖包到最新版本号
    设计无限滚动下拉加载,实践高性能页面真谛
    a标签带参页面跳转并在跳转页面接收参数
    树状数组—区间修改+单点查询 详解
    求逆元的四种方法
    (非线段树)区间修改_单点查询
  • 原文地址:https://www.cnblogs.com/zydev/p/6987229.html
Copyright © 2011-2022 走看看