day10-jsp&ELの代码学习(EL)
EL表达式的学习
EL表达式的常见用法
Student.java(定义实体类)
package com.itheima.domain;
import java.io.Serializable;
public class Student implements Serializable {
private String name;
private int age;
private String sex;
public Student() {
super();
}
public Student(String name, int age, String sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return "我的lg"+sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
01el.jsp(EL简单应用)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
EL: ${1+243432+455+38*438 }<br>
<%
pageContext.setAttribute("home1", "pageContext邓丹丹的住址:软件园1号楼308房间");
request.setAttribute("home", "request邓丹丹的住址:软件园1号楼308房间");
%>
EL实现从域中依次搜索:${home }<br> <!-- page,request,session,application 如何指定一个域? -->
<br>
在指定的域中搜索: ${requestScope.home } <br><!-- 在指定的request域中搜索 -->
在page范围搜索:${pageScope.home1 }<br>
<!--page:pageScope request:requestScope sesison:sessionScope application:applicationScope -->
搜索一个在域中没有存放的值 :${lg }<br> <!-- 如果这个值在域中不存在,则输出空白 -->
</body>
</html>
02elop(EL结合操作符(operator)的使用
<%@page import="com.itheima.domain.Student"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<!-- .运算符与[]运算符 -->
<%
pageContext.setAttribute("p", new Student("lg",18,"女"));
%>
用.取值 : ${pageScope.p.name}<br>
用[]取值 :${p[name] }----------${p['name'] }---------${p["name"] }<br> <!-- []运算符要加引号 -->
<%
int a[]={23,45,67,89};
pageContext.setAttribute("array", a);
%>
用[]取值 :${array[1] }----------${array['1'] }---------${array["1"] }<br> <!-- 取45 作为数组[]运算符的引号可写可不写 -->
用.取值 :<%-- ${array.1} --%> <!-- 取45 不能取到,因为.运算符后面不支持写数字 -->
<h2>取对象的属性</h2>
<!-- 这不是一个坑 -->
取对象lg的sex属性 :${p.sex } <!-- el表达式取对象的属性,其实就是调用 javabean的getter方法 get去掉后面第一个大写字母改小写,其它不变 -->
<h2>取集合</h2>
<%
List list = new ArrayList();
list.add("aj");
list.add("cgx");
list.add("frjj");
request.setAttribute("list",list);
%>
EL取集合List: ${list[2] } ------------${list['2']}--------------- ${list["2"]}
<br>
<%
Map map = new HashMap();
map.put("L1","aj");
map.put("L2","ddd");
map.put("L3","djj");
request.setAttribute("map",map);
%>
EL取Map集合:${map.L3 }------${map["L3"] }-----------${map['L3'] }<br>
<h2>EL 的三元运算符 条件?值1 :值2</h2>
EL 的三元运算符: ${1+2>=3?"你太聪明了":"你太笨了" }<br>
<%
pageContext.setAttribute("loginedUser",new Student("ddd",18,"女"),PageContext.SESSION_SCOPE);
%>
<!-- EL不支持字符串相关的运算 比如字符串加法 "欢迎您"+username-->
${sessionScope.loginedUser==null ?"您还没有登录":"欢迎您" } ${loginedUser.name }<br>
<!-- 代表是否为空 null "" list集合中没有任何元素 都返回true -->
empty运算符:${empty sessionScope.loginedUser ?"您还没有登录":"欢迎您" } ${loginedUser.name }
<%
List list2 = new ArrayList();
list2.add("ddd");
request.setAttribute("list2",list2);
%>
<br>empty测试集合中没有任何元素时,是否为空:${empty list2 }
</body>
</html>
03elinnerobject.jsp(EL结合jsp内置对象的使用)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<!-- pageScope requestScope sessionScope applicationScope -->
应用名: <%=request.getContextPath() %><!-- day10_01_el --><br>
<!-- pageContext可以操作其它 8个JSP内置对象 -->
EL取应用名: ${pageContext.request.contextPath}<br>
<!-- ?username=lg -->
param: ${param.username }<br> <!-- request.getParameter() -->
<!-- ?username=lg&username=lgg -->
paramValues:${paramValues.username[0] }---------${paramValues.username[1] }<br> <!-- request.getParameterValues() -->
header: ${header["Accept-Encoding"] } <br> <!-- request.getHeader("Accept-Encoding") -->
headerValues:${headerValues["Accept-Encoding"][0] } <br> <!-- request.getHeaderValues("Accept-Encoding") -->
cookie中的JSESSIONID :${cookie }------${cookie.JSESSIONID.name }---------------${cookie.JSESSIONID.value }<br><!-- request.getCookies() -->
<%=session.getId() %>
<br>取web.xml中配置的全局初始化参数<param-name>encoding</param-name> :${initParam.encoding }<br>
</body>
</html>
自定义EL方法
MyFunction.java(自定义的方法实现小写字符串转大写)
package com.itheima.el;
public class MyFunctions {
public static String toUpperCase(String str){
return str.toUpperCase();
}
}
myfunctions.tld(在WEB-INF下添加该文件)
<?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>A tag library exercising SimpleTag handlers.</description>
<tlib-version>1.0</tlib-version>
<short-name>myfn</short-name>
<!-- 将来可以通过taglib 标签引入 -->
<uri>http://www.itheima.com/myjstl/myfunctions</uri>
<function>
<!-- 页面中引用的名称 -->
<name>toUpperCase</name>
<!-- 类的全路径 -->
<function-class>com.itheima.el.MyFunctions</function-class>
<!-- 方法的声明 -->
<function-signature>java.lang.String toUpperCase( java.lang.String )</function-signature>
</function>
</taglib>
04ELfunction.jsp(jsp页面调用自定义方法)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.itheima.com/myjstl/myfunctions" prefix="myfn" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<%
request.setAttribute("str", "abcdefgh");
%>
EL自定义函数:${myfn:toUpperCase(requestScope.str) }<br>
SUN的EL函数:${fn:toUpperCase(str) }<br>
</body>
</html>