EL表达式

Posted Mr·Liu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EL表达式相关的知识,希望对你有一定的参考价值。

EL表达式:
  它不是一门语言,只是jsp的表达式
  EL表达式只能获取四大域中的数据(pageContext,request,session,application)
EL表达式:获取 对象 的get方法。
基本语法:${EL表达式}
如果EL表达式获取的对象为NULL,页面不会显示数据,EL表达式永远不会出现空指针。

11个隐式对象:
  pageContext
  pageScope 从page范围内取数据
  requestScope
  sessionScope
  applicationScope
  param 接收传递到本页面的参数
  paramValues 接收传递到本页面的一组参数
  header 一个头信息数据
  headerValues 一组头信息数据
  cookie cookie中的数据
  initParam 取得配置的初始化参数
运算符"."和"[]" : "."能做的"[]"也能做,"[]"能做的"."不一定能做

<jsp:useBean id="实例化对象名称" scope="对象保存的属性范围(4种)" class="包全名.类全名(按住Ctrl鼠标单击可进入对应Java程序)" ></jsp:useBean>
设置对象<jsp:setProperty name="实例化对象名称(id)" property="*" > 自动匹配
设置对象<jsp:setProperty name="实例化对象名称(id)" property="属性名称" /> 指定属性
设置对象<jsp:setProperty name="实例化对象名称(id)" property="属性名称" param="参数"/> 指定参数
设置对象<jsp:setProperty name="实例化对象名称(id)" property="属性名称" value="内容"/> 制定内容
获取对象<jsp:getProperty name="实例化对象名称(id)" property="属性名称" />

 

1.java

1
package cn.gs.ly.mvc.domain; 2 3 import java.util.Date; 4 5 public class Person { 6 private String name; 7 private String gender; 8 private boolean married; 9 private Date birthday; 10 private Address address = new Address(); 11 12 public Person() { 13 14 } 15 16 public Person(String name, String gender, boolean married) { 17 super(); 18 this.name = name; 19 this.gender = gender; 20 this.married = married; 21 } 22 23 24 public Address getAddress() { 25 return address; 26 } 27 28 public void setAddres(Address address) { 29 this.address = address; 30 } 31 32 public String getName() { 33 return name; 34 } 35 public void setName(String name) { 36 this.name = name; 37 } 38 public String getGender() { 39 return gender; 40 } 41 public void setGender(String gender) { 42 this.gender = gender; 43 } 44 public boolean isMarried() { 45 return married; 46 } 47 public void setMarried(boolean married) { 48 this.married = married; 49 } 50 public Date getBirthday() { 51 return birthday; 52 } 53 public void setBirthday(Date birthday) { 54 this.birthday = birthday; 55 } 56 57 }
 1 package cn.gs.ly.mvc.domain;
 2 
 3 public class Address {
 4     private String city="青岛";
 5 
 6     public String getCity() {
 7         return city;
 8     }
 9 
10     public void setCity(String city) {
11         this.city = city;
12     }
13     
14 }

2.jsp

1
<%@page import="java.util.HashMap"%> 2 <%@page import="java.util.Map"%> 3 <%@page import="java.util.ArrayList"%> 4 <%@page import="java.util.List"%> 5 <%@page import="cn.gs.ly.mvc.domain.Person"%> 6 <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%> 7 <!DOCTYPE html> 8 <html> 9 <head> 10 <title>Insert title here</title> 11 </head> 12 <body> 13 <jsp:useBean id="p" class="cn.gs.ly.mvc.domain.Person"></jsp:useBean> 14 <jsp:setProperty property="name" name="p" value="刘超"></jsp:setProperty> 15 <!--获得属性三种方法 --> 16 方法1:<%=p.getName() %><br/> 17 方法2:<jsp:getProperty property="name" name="p" ></jsp:getProperty> <br/> 18 方法3:${p.name }====${p["name"]}====${p[\'name\']}<hr/> 19 20 获取未赋值的gender1:<%=p.getGender() %><br/> 21 获取未赋值的gender2:<jsp:getProperty property="gender" name="p" ></jsp:getProperty> <br/> 22 获取未赋值的gender3:${p.gender }<hr/> 23 24 获得没有的对象:${p1.gender }<hr/> 25 26 <% 27 //依次从page-request-session-application 取值,都没有为空白 28 pageContext.setAttribute("pp", "pp1"); 29 pageContext.setAttribute("pp", "pp2",pageContext.REQUEST_SCOPE) ; 30 session.setAttribute("pp", "pp3"); 31 application.setAttribute("pp", "pp4"); 32 %> 33 1:${pp} 34 2:${requestScope.pp} 35 3:${pp} 36 4:${pp} 37 5:${pp} 38 <hr/> 39 40 ${p.address.city}<hr/> 41 ${p["class"]}<hr/> 42 43 <jsp:setProperty property="married" name="p" value="true"></jsp:setProperty> 44 ${p.married }<hr/> 45 46 ${pageContext.request.remoteAddr}<hr/> 47 ${pageContext.session.id}<hr/> 48 ${pageContext.request.method}<hr/> 49 ${pageContext.request.localName}<hr/> 50 51 <% 52 String str[] = {"数组元素a","数组元素b","数组元素c"}; 53 pageContext.setAttribute("s", str); 54 55 List list =new ArrayList(); 56 list.add("list集合元素a"); 57 list.add("list集合元素b"); 58 list.add("list集合元素c"); 59 pageContext.setAttribute("list", list); 60 61 Map map = new HashMap(); 62 map.put("a","map集合aa"); 63 map.put("b","map集合bb"); 64 map.put("c","map集合cc"); 65 pageContext.setAttribute("map", map); 66 67 Map map1 = new HashMap(); 68 map.put("1","map集合aa1"); 69 map.put("2","map集合bb2"); 70 map.put("3","map集合cc3"); 71 pageContext.setAttribute("map1", map); 72 73 Map map2 = new HashMap(); 74 map.put("1",new Person("刘超","",false )); 75 map.put("2",new Person("姜静","",false )); 76 map.put("3",new Person("王熙","",true )); 77 pageContext.setAttribute("map2", map); 78 %> 79 ${s[0]}<hr/> 80 ${list[0]}<hr/> 81 ${map.a}<hr/> 82 ${map["1"]}<hr/> 83 ${map2["3"].name}<hr/> 84 85 <!-- 86 浏览器出入下面网址(明文提交) 可获取明文提交的 参数 87 http://localhost:8080/WEB2018/Demo01.jsp?ref=liuyang&inst=inst1&inst=inst2&inst=inst3 88 --> 89 获取明文提交的参数方法1:<%=request.getParameter("ref") %> 90 获取明文提交的参数方法2:${param.ref}<hr/> 91 获取明文提交的多个参数方法:${paramValues.inst[0] } ${paramValues.inst[1] }<hr/> 92

        ${headerValues["accept"][0]}<hr/>
       ${header["accept-encoding"] }<hr/>
 
         Cookie:${cookie}<hr/>
         Cookie_name:${cookie["JSESSIONID"].name }<hr/>
         Cookie_value:${cookie["JSESSIONID"].value }<hr/>

         获得xml初始化参数:${initParam["info"]}<br/>

  93 <input type="text" value="${p.address.city}">

94     
95 </body>
96 </html>

3.web.xml 初始化配置

1 <context-param><!-- 初始化参数 -->
2 <param-name>info</param-name>
3 <param-value>value_liuyang</param-value>
4 </context-param>

 

4.运行结果

 

 

以上是关于EL表达式的主要内容,如果未能解决你的问题,请参考以下文章

jsp--EL表达式

jsp中的EL表达式

Jsp入门EL表达式_学习笔记

第五条知识点

EL表达式

EL表达式的基本知识