EL技术

1.EL 表达式概述

EL(Express Lanuage)表达式可以嵌入在jsp页面内部,减少jsp脚本的编写,EL 出现的目的是要替代jsp页面中脚本的编写。

2.EL从域中取出数据(EL最重要的作用)

jsp脚本:<%=request.getAttribute(name)%>
EL表达式替代上面的脚本:${requestScope.name}

EL最主要的作用是获得四大域中的数据,格式EL表达式EL获得pageContext域中的值:{EL表达式} EL获得pageContext域中的值:ELELpageContext{pageScope.key};
EL获得request域中的值:requestScope.key;EL获得session域中的值:{requestScope.key}; EL获得session域中的值:requestScope.key;ELsession{sessionScope.key};
EL获得application域中的值:applicationScope.key;EL从四个域中获得某个值{applicationScope.key}; EL从四个域中获得某个值applicationScope.key;EL{key};
—同样是依次从pageContext域,request域,session域,application域中 获取属性,在某个域中获取后将不在向后寻找
1)获得普通字符串
2)获得User对象的值
3)获得List的值
代码如下:

<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ page import="beyond.domain.*" %>
<%@ page import="java.util.*" %>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body><!-- 模拟域中的数据 --><%//存储一个字符串request.setAttribute("company","beyond谚语" );//存储一个对象User user = new User();user.setId(1);user.setName("siqi");user.setPassword("wsq");session.setAttribute("user", user);//存储一个集合List<User> list = new ArrayList<User>();User user1 = new User();//list集合中的第一个元素user1.setId(2);user1.setName("qibao");user1.setPassword("wsq");list.add(user1);//list集合中的第二个元素User user2 = new User();user2.setId(3);user2.setName("yanyu");user2.setPassword("wsq");list.add(user2);application.setAttribute("list", list);%><!-- 脚本(jsp)的方式取出request域中的值(beyond谚语)  --><%=request.getAttribute("company") %><!-- 脚本(jsp)的方式取出User对象中的Name(siqi)中的值  --><%User sessionUser = (User) session.getAttribute("user");out.write(sessionUser.getName());%><hr/><!-- 使用EL表达式获得request域中的值(beyond谚语)  -->${requestScope.company}<!-- 使用EL表达式获得User对象中的Name中的值(siqi) -->${sessionScope.user.name}<!-- 使用EL表达式获得 application域中的第二个元素的name(yanyu) -->${applicationScope.list[1].name}<hr> <!-- 使用el表达式 全域查找  也就是把域给去掉即可-->${company}${user.name}${list[1].name}</body>
</html>
package beyond.domain;public class User {private int id;private String name;private String password;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}

执行结果:
在这里插入图片描述

3.EL的内置对象11个

${pageContext.request.contextPath}:动态获取web应用

pageScope,requestScope,sessionScope,applicationScope
---- 获取JSP中域中的数据

param,paramValues - 接收参数.
相当于request.getParameter() request.getParameterValues()

header,headerValues - 获取请求头信息
相当于request.getHeader(name)

initParam - 获取全局初始化参数
相当于this.getServletContext().getInitParameter(name)

cookie - WEB开发中cookie
相当于request.getCookies()—cookie.getName()—cookie.getValue()

pageContext - WEB开发中的pageContext.
pageContext获得其他八大对象

${pageContext.request.contextPath}
相当于
<%=pageContext.getRequest().getContextPath%> 这句代码不能实现
获得WEB应用的名称

//form.jsp
<%@ 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">
<link href="${pageContext.request.contextPath}/yy.css"><!-- 这样的地址叫做  客户端地址 -->
<script type="text/javascript" src="${pageContext.request.contextPath}/yyy.js" ></script><!-- 这样的地址叫做  客户端地址 -->
<title>Insert title here</title>
</head>
<body><form action="${pageContext.request.contextPath}/el/form2.jsp" method="post"><%-- 只要是客户端地址 建议都要把web应用名称给写上:${pageContext.request.contextPath} --%><input type="text" name="username"><br><input type="password" name="password"><br><input type="checkbox" name="hobby" value="zq">足球<input type="checkbox" name="hobby" value="pq">排球<input type="checkbox" name="hobby" value="ppq">乒乓球<br><input type="submit" value="提交"><br></form><img alt="" src="${pageContext.request.contextPath}/1.jpg"><!-- 这样的地址叫做  客户端地址 --><img alt="" src="${pageContext.request.contextPath}/2.jpg"><!-- 这样的地址叫做  客户端地址 --><img alt="" src="${pageContext.request.contextPath}/1.jpg"><!-- 这样的地址叫做  客户端地址 --><!-- <img alt="" src="1.jpg">这样的地址叫 相对地址,跳转的时候偶尔会出现问题 -->
</body>
</html><%-- 一个发出5次请求:
第一次:访问该资源,服务器返回该资源全部代码,客户端开始接受并解析
第二次:当客户端解析到<link href="${pageContext.request.contextPath}/yy.css"><!-- 这样的地址叫做  客户端地址 -->的时候,开始向服务器请求数据
第三次:<script type="text/javascript" src="${pageContext.request.contextPath}/yyy.js" ></script><!-- 这样的地址叫做  客户端地址 -->,向服务器请求数据
第四次:<img alt="" src="${pageContext.request.contextPath}/1.jpg"><!-- 这样的地址叫做  客户端地址 -->向服务器请求数据,并且缓存该图片
第五次:<img alt="" src="${pageContext.request.contextPath}/2.jpg"><!-- 这样的地址叫做  客户端地址 -->向服务器请求数据,并且缓存该图片
当访问<img alt="" src="${pageContext.request.contextPath}/1.jpg"><!-- 这样的地址叫做  客户端地址 -->的的时候,发现本客户端有该图片缓存不用向服务器请求了 --%>
//cookie.jsp
<%@ 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><%Cookie cookie = new Cookie("name","beyond");response.addCookie(cookie);//将cookie写到客户端%>
</body>
</html>
//form2.jsp
<%@ 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><!-- 获得表单的参数 --><%request.getParameter("username");request.getParameter("password");request.getParameter("hobby");%><!-- 使用el获得参数  内置对象.需要获取的数据 -->${param.username}<!-- 获取到username(beyond)并输出到页面上 -->${header.Host}${header["User-Agent"]}<!-- 因为这里的User-Agent有-特殊符号,所以得使用[] -->${header["Host"]}<!-- 能用.操作的都可以用[""]操作 -->${initParam.beyond.value}${cookie.name.value}<!-- 访问cookie的值(beyond) ,在页面中获得value的值--><!-- 通过el表达式获得request对象    其中requestScope代表域-->${pageContext.request}<!-- pageContext可以获得其中八大对象 --></body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"><display-name>WEB18</display-name><!-- 定义全局初始化参数 --><context-param><param-name>beyond</param-name><param-value>wsq</param-value></context-param><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list>
</web-app>

4.EL执行表达式

例如:
${1+1}
${empty user}
${user==null?true:false}

<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ page import="beyond.domain.*" %>
<%@ page import="java.util.*" %>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body><!-- 模拟域中的数据 --><%pageContext.setAttribute("company","ScriptKiddie" );//存储一个字符串request.setAttribute("company","beyond谚语" );//存储一个对象User user = new User();user.setId(1);user.setName("siqi");user.setPassword("wsq");session.setAttribute("user", user);//存储一个集合List<User> list = new ArrayList<User>();User user1 = new User();//list集合中的第一个元素user1.setId(2);user1.setName("qibao");user1.setPassword("wsq");list.add(user1);//list集合中的第二个元素User user2 = new User();user2.setId(3);user2.setName("yanyu");user2.setPassword("wsq");list.add(user2);application.setAttribute("list", list);%><!-- 脚本(jsp)的方式取出request域中的值(beyond谚语)  --><%=request.getAttribute("company") %><!-- 脚本(jsp)的方式取出User对象中的Name(siqi)中的值  --><%User sessionUser = (User) session.getAttribute("user");out.write(sessionUser.getName());%><hr/><!-- 使用EL表达式获得request域中的值(beyond谚语)  -->${requestScope.company}<!-- 使用EL表达式获得User对象中的Name中的值(siqi) -->${sessionScope.user.name}<!-- 使用EL表达式获得 application域中的第二个元素的name(yanyu) -->${applicationScope.list[1].name}<hr> <!-- 使用el表达式 全域查找  也就是把域给去掉即可-->${company}${user.name}${list[1].name}<!-- el可执行表达式的运算 -->${1+1}<!-- 2 -->${1==1?false:true}<!-- false --><!-- empty 判定某个对象是否是null  如果是null返回true;不是返回false -->${empty list}<!-- 不为null,返回false -->${empty user}<!-- 不为null,返回false --></body>
</html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/379611.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

math.trunc_JavaScript中带有示例的Math.trunc()方法

math.truncJavaScript | Math.trunc()方法 (JavaScript | Math.trunc() Method) Math.trunc() is a function in math library of JavaScript that is used to extract the integer part of the given floating-point number. It removes the decimal and all the digits after…

.NET程序员的书单

zz from sjtu bbs: http://bbs.sjtu.edu.cn/bbscon?boardDotNET&fileM.1126188158.A 发信人: luckySeven(lucky为这位mm默哀), 信区: DotNET 标 题: .NET程序员的书单 发信站: 饮水思源 (2005年09月08日22:02:45 星期四), 转信 发信人: AtomAndBit (原子与比特), 信区: D…

SVN+AnkhSVN端配置

对于ankhSVN我想很多人不陌生&#xff0c;因为经常使用&#xff0c;但是我还是发现很多人并不怎么会配置&#xff0c;或者完全不知道其需要配置&#xff0c;如果不配置的话&#xff0c;当两个人同时需要修改某个文件的时候就容易中弹了。SVN默认是不支持“锁定-编辑-解锁”的&a…

Linux内核设计与实现---模块

模块1 构建模块放在内核源代码树中放在内核代码外2 安装模块3 产生模块依赖性4 载入模块5 管理配置选项6 模块参数7 导出符号表Linux内核是模块化组成的&#xff0c;它允许内核在运行时动态地向其中插入或从中删除代码。 与开发的内核核心子系统不同&#xff0c;模块开发更接近…

JSTL技术

1&#xff0e;JSTL概述 JSTL&#xff08;JSP Standard Tag Library)&#xff0c;JSP标准标签库&#xff0c;可以嵌入在jsp页面中使用标签的形式完成业务逻辑等功能。jstl出现的目的同el一样也是要代替jsp页面中的脚本代码。JSTL标准标准标签库有5个子库&#xff0c;但随着发展…

asinh函数_JavaScript中带有示例的Math.asinh()方法

asinh函数JavaScript | Math.asinh()方法 (JavaScript | Math.asinh() Method) Math.asinh() is a function in math library of JavaScript that is used to find the value of hyperbolic arc-sine of a number. Math.asinh()是JavaScript数学库中的函数&#xff0c;用于查找…

使用PHP创建一个REST API(Create a REST API with PHP)

译者前言&#xff1a; 首先这是一篇国外的英文文章&#xff0c;非常系统、详尽的介绍了如何使用PHP创建REST API&#xff0c;国内这方面的资料非常非常的有限&#xff0c;而且基本没有可操作性。这篇文章写的非常好&#xff0c;只要对PHP稍有了解的程序员&#xff0c;看完本文基…

old-

大数问题:求用一段C或C程序写求 f(x)100! 的完整程序大数问题&#xff0c; 我用数组作的&#xff0c;输出格式应该是是222,222,222 #include "stdafx.h" #include<stdio.h> #include<stdlib.h> int a[1000]{0}; in…

javaEE的开发模式

1&#xff0e;什么是模式 模式在开发过程中总结出的“套路”&#xff0c;总结出的一套约定俗成的设计模式 2&#xff0e;javaEE经历的模式 model1模式&#xff1a; 技术组成&#xff1a;jspjavaBean model1的弊端&#xff1a;随着业务复杂性 导致jsp页面比较混乱 model2模式…

Linux内核设计与实现---kobject sysfs

kobject sysfs1 kobject2 ktype3 kset4 subsystem5 别混淆了这些结构体6 管理和操作kobject7 引用计数kref8 sysfssysfs中添加和删除kobject向sysfs添加文件9 内核事件层2.6内核增加了一个引人注目的新特性—同一设备模型。设备模型提供了独立的机制专门表示设备&#xff0c;并…

开发Windows Mobile今日插件 -- 内存电量,桌面便笺,桌面记单词

本篇文章讲解的是开发 Windows Mobile 上的今日插件。关于是今日插件&#xff0c;在 PPC 或者 SP SDK 的帮助文档中有相关的章节介绍&#xff0c;在网络上也有一些帖子和资源讲解。在这里简要回顾一下。今日插件就是在windows mobile的桌面上显示的条目&#xff0c;例如系统提供…

c语言中将函数指针作为形参_在C中将有效指针作为NULL指针

c语言中将函数指针作为形参Prerequisite: An Example of Null pointer in C 先决条件&#xff1a; C中的空指针示例 Any pointer that contains a valid memory address can be made as a NULL pointer by assigning 0. 通过分配0&#xff0c;可以将包含有效内存地址的任何指…

[转]一个清华计算机博士生的退学申请

偶然间在网上看到这篇帖子&#xff0c;回想起自己的求学经历&#xff0c;思索良久。。。 本想找到原帖及作者&#xff0c;但是几经搜索&#xff0c;发现原帖出自科学网&#xff0c;已被删除。对此&#xff0c;我还能说啥&#xff1f;&#xff01; http://www.sciencenet.cn/m/u…

算法---递归

递归结题三部曲 何为递归&#xff1f;程序反复调用自身即是递归。 我自己在刚开始解决递归问题的时候&#xff0c;总是会去纠结这一层函数做了什么&#xff0c;它调用自身后的下一层函数又做了什么…然后就会觉得实现一个递归解法十分复杂&#xff0c;根本就无从下手。 相信…

给定条件找最小值c语言程序_根据给定条件最小化n的最小步骤

给定条件找最小值c语言程序Problem statement: 问题陈述&#xff1a; Given a number n, count minimum steps to minimize it to 1 performing the following operations: 给定数字n &#xff0c;执行以下操作&#xff0c;计算最少的步骤以将其最小化为1&#xff1a; Operat…

提高C#编程水平不可不读的50个要诀

提高C#编程水平的50个要点 1.总是用属性 (Property) 来代替可访问的数据成员 2.在 readonly 和 const 之间&#xff0c;优先使用 readonly 3.在 as 和 强制类型转换之间&#xff0c;优先使用 as 操作符 4.使用条件属性 (Conditional Attributes) 来代替条件编译语句 #if 5.总是…

那个年代的苏联歌曲

小时候&#xff0c;不时听父亲提起电影《这里的黎明静悄悄》&#xff0c;怎么也想不到如此美丽的名字为什么要和战争联系起来。后来在大学看了这部电影之后&#xff0c;开始认为这名字是合适的&#xff0c;因为电影讲的是女性——战场中的女性&#xff0c;各自都怀揣着爱情去保…

linux系统编程---进程总结

进程控制总结1 进程创建的三种方式forkvfrokclone2 进程终止进程正常退出returnexit_exit进程异常退出进程收到某个信号&#xff0c;而该信号使进程终止abort3 进程等待进程等待的方法waitwaitpid4 进程替换替换原理替换函数制作一个简单的shell1 进程创建的三种方式 参考文章…

银行账务转账系统(事务处理)

流程如下&#xff1a; 创建项目工程如下&#xff1a; transfer包下的代码如下&#xff1a; package beyond.transfer.dao;import java.sql.Connection; import java.sql.SQLException;import org.apache.commons.dbutils.QueryRunner;import beyond.utils.DataSourceUtils;pu…

【msdn wpf forum翻译】TextBox中文本 中对齐 的方法

原文链接&#xff1a;http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/49864e35-1dbf-4292-a361-93f1a8400558问题&#xff1a;TextBox中文本中对齐&#xff0c;使用 TextBox.HorizontalContentAlignment"Center"行不通&#xff08;TextBox.VerticalConte…