码迷,mamicode.com
首页 > 其他好文 > 详细

EL表达式

时间:2018-10-03 00:27:44      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:linu   分享   image   util   enter   center   com   imp   ssi   

EL表达式

①EL表达式(Expression Language,EL)简介:

El表达式主要是简化jsp代码,EL(Expression Language) 是为了使JSP写起来更加简单,提供了在 JSP 中简化表达式的方法,让Jsp的代码更加简化。

我们使用EL表达式直接输出内置对象的值:

代码:

技术分享图片
 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <%
11     pageContext.setAttribute("info1", "pageContext的值");
12     session.setAttribute("info2", "session的值");
13     request.setAttribute("info3", "reqest的值");
14     application.setAttribute("info4","applicaiton值");
15 %>
16 <h1>${info1 }</h1>
17 <h1>${info2 }</h1>
18 <h1>${info3 }</h1>
19 <h1>${info4 }</h1>
20 </body>
21 </html>
el01.jsp

显示结果:技术分享图片

②EL表达式内置对象

 

表达式内置对象

说明

1

pageContext

表示javax.servlet.jsp.PageContext对象

2

pageScope

表示从page属性范围查找输出属性

3

requestScope

表示从request属性范围查找输出属性

4

sessionScope

表示从session属性范围查找输出属性

5

applicationScope

表示从applicaiton属性范围查找输出属性

6

param

接收传递到本页面的参数

7

paramValues

接收传递到本页面的一组参数

8

header

取得一个头信息数据

9

headerValues

取出一组头信息数据

10

cookie

取出cookie中的数据

11

initParam

取处配置的初始化参数。

③EL表达式范围4中范围属性

寻找值的顺序:page->request->session->application

 如果在page中找到想要的值就不会再往下面找,其他的也是。

例如代码:

技术分享图片
 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <%
11     pageContext.setAttribute("info1", "pageContext的值");
12     session.setAttribute("info1", "session的值");
13     request.setAttribute("info1", "reqest的值");
14     application.setAttribute("info1","applicaiton值");
15 %>
16 <h1>${info1 }</h1>
17 <h1>${info2 }</h1>
18 <h1>${info3 }</h1>
19 <h1>${info4 }</h1>
20 </body>
21 </html>
el02.jsp

El表达式取到请求参数值

1、param.参数名称

例如:在info.jsp中我们写页面提交的数据

技术分享图片
 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <form action="el2.jsp" method="post">
11 <input type="text" id="name" name="name" value="张三"/><br/>
12 <input type="submit"  value="提交到el2.jsp" >
13 </form>
14 <a href="el2.jsp?age=12">提交到el2.jsp</a>
15 </form>
16 </body>
17 </html>
info.jsp

在el2.jsp中获取得到的值

方法:param.name获取姓名 param.age获取年龄

技术分享图片
 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title> el2.jsp 页 面 </title>
 8 </head>
 9 <body>
10     <%    request.setCharacterEncoding("utf-8"); %>
11     <h3>姓名:${param.name }</h3>
12     <h2>年龄:${param.age}</h2>
13 </body>
14 </html>
el2.jsp

request.setCharacterEncoding("utf-8");

设置字体编码

2、param.一组参数

技术分享图片
 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <form action="el2.jsp" method="post">
11 <input type="checkbox" name="hobby" value="java"/>java
12 <input type="checkbox" name="hobby" value="C">C
13 <input type="checkbox" name="hobby" value="linux">linux<br/>
14  <input type="submit" value="提交啊">
15 </form>
16 </body>
17 </html>
info.jsp
技术分享图片
 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title> el2.jsp 页 面 </title>
 8 </head>
 9 <body>
10     <%    request.setCharacterEncoding("utf-8"); %>
11     <h1>爱好:${paramValues.hobby[0] }</h1>
12     <h1>爱好:${paramValues.hobby[1] }</h1>
13     <h1>爱好:${paramValues.hobby[2] }</h1>
14     
15 </body>
16 </html>
el2.jsp

 ⑤EL表达式操作对象

新建一个people对象,属性有id ,name ,age;

然后我们使用get set方法。

代码如下:

技术分享图片
 1 package com.java1234.model;
 2 
 3 public class People {
 4     private int id;
 5     private String name;
 6     private int age;
 7     public int getId() {
 8         return id;
 9     }
10     public void setId(int id) {
11         this.id = id;
12     }
13     public String getName() {
14         return name;
15     }
16     public void setName(String name) {
17         this.name = name;
18     }
19     public int getAge() {
20         return age;
21     }
22     public void setAge(int age) {
23         this.age = age;
24     }
25     
26 
27 }
People.java

对对象的操作可以直接对对象的名称操作

技术分享图片
 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <%@ page import="com.java1234.model.People"  %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 8 <title> El 操 作people对象 </title>
 9 </head>
10 <body>
11     <% People zhangsan =new People();
12         zhangsan.setId(1);
13         zhangsan.setName("张三");
14         zhangsan.setAge(20);
15         request.setAttribute("zhangsan",zhangsan);
16     %>
17     <h1>编号:${zhangsan.id }</h1>
18     <h1>姓名:${zhangsan.name }</h1>
19     <h1>年龄:${zhangsan.age }</h1>
20     
21 </body>
22 </html>
el3.jsp

EL表达式集合操作

 我们对集合进行操作:

List all =new LinkedList();

技术分享图片
 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <%@ page import="java.util.*"  %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 8 <title> El 操 作people对象 </title>
 9 </head>
10 <body>
11     <% List all=new LinkedList();
12     all.add(0,"元素一");
13     all.add(1, "元素二");
14     all.add(2,"元素三");
15     request.setAttribute("all", all);
16     %>
17     <h1>${all[0] }</h1>
18     <h1>${all[1] }</h1>
19     <h1>${all[2] }</h1>
20 </body>
21 </html>
el4.jsp

EL表达式运算符操作

 

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <%@ page import="java.util.*"  %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 8 <title> EL运算符操作 </title>
 9 </head>
10 <body>
11     <%
12     request.setAttribute("num1",10);
13     request.setAttribute("num2",20);
14     request.setAttribute("flag1",true);
15     request.setAttribute("flag2",false);
16     %>
17     <h1>num1=${num1 }   num2=${num2 }</h1>
18     <h3>num1 + num2 =${num1+num2}</h3>
19     <h3>num1 - num2 =${num1-num2}</h3>
20     <h3>num1*num2=${num1*num2 }</h3>
21     <h3>num1%num2=${num1%num2 }</h3>
22     
23     <h3>逻辑运算</h3>
24     <h1>flag1=${flag1 }   flag2=${flag2 }</h1>
25     <h3>flag1 && flag2 :${flag1 && flag2 }</h3>
26     <h3>flag1 || flag2 :${flag1 || flag2 }</h3>
27     <h3>!flag1 : ${!flag1 }</h3>
28     
29     <h3>empty运算</h3>
30     <h3>${empty a }</h3>
31 </body>
32 </html>

 

EL表达式

标签:linu   分享   image   util   enter   center   com   imp   ssi   

原文地址:https://www.cnblogs.com/zyxsblogs/p/9712758.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!