微信开放平台 网站应用开发,网络营销策划书模板,网站专业建设,做单页网站怎么选产品一#xff0c;EL 表达式概述#xff08;EL主要从域中取数据#xff09; EL#xff08;Express Lanuage#xff09;表达式可以嵌入在jsp页面内部#xff0c;减少jsp脚本的编写#xff0c;EL出现的目的是要替代jsp页面中脚本的编写。 二#xff0c;EL从域中取出数据(EL… 一EL 表达式概述EL主要从域中取数据 ELExpress Lanuage表达式可以嵌入在jsp页面内部减少jsp脚本的编写EL出现的目的是要替代jsp页面中脚本的编写。 二EL从域中取出数据(EL最重要的作用) jsp脚本 %request.getAttribute(name)% EL表达式替代上面的脚本 ${requestScope.name} EL最主要的作用是获得四大域中的数据 格式${ EL表达式 } EL获得pageContext域中的值 ${pageScope.key}; EL获得request域中的值 ${requestScope.key}; EL获得session域中的值 ${sessionScope.key}; EL获得application域中的值 ${applicationScope.key} EL从四个域中获得某个值${key}; EL表达式语句在执行时会调用pageContext.findAttribute方法用标识符为关键字分别从page、request、session、application四个域中查找相应的对象找到则返回相应对象找不到则返回 注意不是null而是空字符串。 例如 User实体类 public class User {private int id;private String name;private String pwd;public User(int id, String name, String pwd) {this.id id;this.name name;this.pwd pwd;}public User() {}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 getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd pwd;}
} Index.jsp % page languagejava contentTypetext/html; charsetUTF-8pageEncodingUTF-8%
% page importcom.zender.*,java.util.*%
%String path request.getContextPath();String basePath request.getScheme()://request.getServerName():request.getServerPort()path/;
%
!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd
html
head
base href%basePath%
meta http-equivContent-Type contenttext/html; charsetUTF-8
titleInsert title here/title
/head
body!-- 模拟域中的数据 --%//存储字符串request.setAttribute(name,Zender);//存储一个对象User user new User(1, Zender, 123);session.setAttribute(user, user);//存储一个集合User user1 new User(1, Zender, 123);User user2 new User(2, Zender2, 1234);User user3 new User(3, Zender3, 1235);ListUser list new ArrayListUser();list.add(user1);list.add(user2);list.add(user3);application.setAttribute(list, list);%通过脚本获取域中的数据:br/!-- 通过脚本获取域中的数据 --%request.getAttribute(name) %%User sessionUser (User)session.getAttribute(user);out.write(sessionUser.getName());%hr/通过EL表达式获取域中的数据:br/!-- 通过EL表达式获取域中的数据 --${requestScope.name}${sessionScope.user.name}${applicationScope.list[1].name}hr/通过EL表达式全域查找获取域中的数据 :br/!-- 通过EL表达式全域查找获取域中的数据 --${name}${user.name}${list[2].name}hr/
/body
/html 访问index.jsp结果如下 三EL表达式执行运算 语法${运算表达式}EL表达式支持如下运算符 1、关系运算符 2、逻辑运算符 3、empty运算符检查对象是否为null(空) 4、二元表达式${user!null?user.name :} 5、[ ] 和 . 号运算符 例如 % page languagejava contentTypetext/html; charsetUTF-8pageEncodingUTF-8%
%taglib urihttp://java.sun.com/jsp/jstl/core prefixc %
% page importcom.zender.*,java.util.*%
%String path request.getContextPath();String basePath request.getScheme()://request.getServerName():request.getServerPort()path/;
%
!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd
html
head
base href%basePath%
meta http-equivContent-Type contenttext/html; charsetUTF-8
titleInsert title here/title
/head
bodyel表达式进行四则运算br/加法运算${100100}br/减法运算${100-100}br/乘法运算${100*100}br/除法运算${100/100}hr/el表达式进行关系运算br/%--${user null}和 ${user eq null}两种写法等价--%${user null}br/${user eq null}hr/el表达式使用empty运算符检查对象是否为null(空):br/%--使用empty运算符检查对象是否为null(空) --%% ListString list new ArrayListString();list.add(Zender);list.add(Zender2);request.setAttribute(list,list);%c:if test${!empty(list)}c:forEach varstr items${list}${str}br//c:forEach/c:ifhr/EL表达式中使用二元表达式:br/% session.setAttribute(user1,new User(1,Zender,123));%${user1 null ? 对不起您没有登陆 : user1.name}
/body
/html 运行结果如下 四EL的内置11个对象 pageScope,requestScope,sessionScope,applicationScope 用于获取JSP中域中的数据 param,paramValues 用于接收参数相当于request.getParameter()rquest.getParameterValues() header,headerValues 用于获取请求头信息相当于request.getHeader(name)request.getHeaders() initParam 用于获取全局初始化参数相当于this.getServletContext().getInitParameter(name) cookie 用于WEB开发中的cookie相当于request.getCookies()---cookie.getName()---cookie.getValue() pageContext 用于WEB开发中的pageContext 注意: 测试header和headerValues时如果头里面有- 例Char-Encoding则需要header[Char-Encoding]、headerValues[Char-Encoding] 测试cookie时例${cookie.key}取的是cookie对象如访问cookie的名称和值必须${cookie.key.name}或者${cookie.key.value} 五EL表达式保留关键字 所谓保留字的意思是指变量在命名时应该避开上述的名字以免程序编译时发生错误。 转载于:https://www.cnblogs.com/Zender/p/7801330.html