JSP EL表达式

获取数据

EL表达式只能从4大域(pageCntext,request,session,application)中获取数据,从小到大寻找。
可以使用pageScope,requestScope,sessionScope,applicationScope中指定某个域。列如${requestScope.name}
1.获取bean对象

<%
Student stu = new Student("张三",10,new School("大学","湖南"));
pageContext.setAttribute("stu", stu);
%>
name = ${stu.name};
age = ${stu.age};
schoolname = ${stu.school.name};

2.获取list集合
有下标的我们就可以访问,但是set是无序的,el表达式不能访问set

		<%List<String> names= new ArrayList<String>();
		names.add("aaa");
		names.add("bbb");
		names.add("ccc");
		pageContext.setAttribute("names", names);
		%>
		names[2] = ${names[2]};

3.获取map集合

<%
		Map<String,Object> map = new HashMap<>();
		map.put("school", new School("背景","上海"));
		map.put("phone", "123");
		map.put("hah", "zansan");
		pageContext.setAttribute("map", map);
		String username = "";
		String name  = null;
		pageContext.setAttribute("username", username);
		pageContext.setAttribute("name", name);
		
	%>
		school.name=${map.school.sname };
		phone = ${map.phone};

4.empty 运算符
未定义的变量,空字符串,值为null的对象结果都是为true

		<%String username = "";
		String name  = null;
		pageContext.setAttribute("username", username);
		pageContext.setAttribute("name", name);
		%>
		empty name = ${empty name };
		empty username = ${empty username };

EL的内置对象

1.pageContext
等同于jspPageContext。获取项目路径

<form action="${pageContext.request.contextPath}/show.jsp" method="post">
</from>

2.参数内置对象
param获取指定参数
paramValues 获取参数数组
initParam 获取web.xml文件的初始化配置信息

	<form action="${pageContext.request.contextPath}/show.jsp" method="post">
			姓名:<input type="text" name="name"/><br/>
			age:<input type="text" name="age"/><br/>
			爱好:<br/>
			<input type="checkbox" name="hobby" value="work"/>工作
			<input type="checkbox" name="hobby" value="study"/>学习
			<input type="checkbox" name="hobby" value="wolk"/>走路
			<input type="submit" value="提交"><br/>			
		</form>
<% 
	request.setCharacterEncoding("utf-8");
%>
	name = ${param.name }<br/>
	age = ${param.age }<br/>
	hobby[0] = ${paramValues.hobby[0]}<br/>
	hobby[1]= ${paramValues.hobby[1]}<br/>
	hobby[2] = ${paramValues.hobby[2]}<br/>
	name = ${initParam.name}<br/>
	address = ${initParam.address }<br/>
</body>

自定义EL函数

1.创建自定义函数小写转大写

package com.javaweb.function;

public class ELfucntion {

	public static String lowerToUpper(String source){
		return source.toUpperCase();
	}
}

2.配置自定义的xml文件在项目的lib目录中扩展名为tld
参照tomcat\webapps\examples\WEB-INF\jsp2目录下的文件jsp2-example-taglib.tld

myFuntion.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">
   	<!--定义标签库信息-->
	<tlib-version>1.0</tlib-version>
	<short-name>myFunction</short-name>
    <uri>http://myself/jsp/el/function</uri>
    <!--注册函数-->
      <function>
        <name>lowerToUpper</name>
        <function-class>com.javaweb.function.ELfucntion</function-class>
        <function-signature>java.lang.String lowerToUpper( java.lang.String )</function-signature>
    </function>
    
</taglib>

3.使用自定义函数
uri使用注册文件信息中的uri,prefix使用。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://myself/jsp/el/function" prefix="myFuncrion" %>
<!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>
	${myFuncrion:lowerToUpper("abc")}
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值