Jsp的EL作用

來源:互聯網
上載者:User

標籤:邏輯運算   hashmap   getc   技術分享   輸出   運算式   ash   png   val   

EL運算式作用: 向瀏覽器輸出域對象中的變數值或運算式計算的結果!!!

文法: ${變數或運算式}

可以通過page指令來設定EL表示是否啟用,false是不啟用,true是啟用,預設是true

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isELIgnored="false"%>

  

1)輸出基礎資料型別 (Elementary Data Type)變數

1.1 從四個域擷取

     ${name}

 1.2 指定域擷取

      ${pageScope.name}

  域範圍: pageScoep / requestScope / sessionScope / applicationScope

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isELIgnored="false"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">  </head>  <body>    <%     //放入域對象中    pageContext.setAttribute("name", "gqxing");     %>     <%--下面代碼等價於pageContext.findAttribute("name") --%>     ${name}     <br>     <%--也可以從指定的域中擷取資料  等價於 pageContext.getAttribute("name",pageContext.pageScope--%>     ${pageScope.name}   </body></html>

運行結果

 

  2)輸出對象的屬性值

2、1)輸出實體物件

首先建立實體物件:

package com.gqx.entity;public class Student {private String name;private int age;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 Student(String name, int age) {this.name = name;this.age = age;}public Student() {}}

 然後去jsp版面設定域對象

 

<%@ page language="java" import="java.util.*,com.gqx.entity.Student" pageEncoding="UTF-8" isELIgnored="false"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">  </head>  <body>   <%--用el去輸出對象的屬性 --%>   <%//儲存對象   Student student = new Student("gqx",21);   pageContext.setAttribute("student", student);    %>    <%--使用域對象去擷取資料 --%>    ${student.name}——${student.age}    <%-- 等價於((Student)pageContext.findAttribute("student")).getName()--%>    <hr>    <%=((Student)pageContext.findAttribute("student")).getName() %>  </body></html>

 運行結果

這個時候我們要注意到一個細節問題,我們在student對象中設定了私人屬性,我們卻在jsp中訪問到了,如下

private String name;
private int age;

這個時候我們想一想,這肯定是不可能的,所以,在這裡查閱資料發現原來在el運算式中 ${student.name}的.name”就相當於getName()方法,這個時候我可以去驗證一下,我可以在student類中去吧getName()方法改名字

發現結果正如我們想的一樣。但並沒有報出屬性出錯的問題,我們再來做一個實驗,我們作如下更改,把name名字給改了,發現其還能正常的運行,這又完美的驗證了我們的結論

public class Student {private String name1;private int age;public String getName() {return name1;}    ...........................

  

3)輸出集合對象

 List  和 Map

首先來看List的程式碼範例

 <   <% List<Student> list =new ArrayList<Student>();       list.add(new Student("gqx",21));       list.add(new Student("gqxing",22));       list.add(new Student("guoqingxing",22));       pageContext.setAttribute("list", list);     %>     <hr>     <%--使用EL擷取List運算式 --%>     ${list[0].name}-${list[0].age }<br>     ${list[1].name}-${list[1].age }<br>     ${list[2].name}-${list[2].age }<br>     <hr>     <%--等價於 --%>     <%=((Student)((List)pageContext.findAttribute("list")).get(0)).getName() %>          <hr>

  將這段代碼放入到jsp的body中去,運行結果如下

再來看看Map,同樣,加入如下的代碼

<% //MAP    Map<String,Student> map=new HashMap<String,Student>();    map.put("001", new Student("gqx",21));    map.put("002", new Student("gqxing",22));    map.put("003", new Student("guoqingxing",22));//放入到域中去pageContext.setAttribute("map", map);     %>          <%--使用EL擷取List運算式 --%>     ${map[‘001‘].name}-${map[‘001‘].age }<br>     ${map[‘002‘].name}-${map[‘002‘].age }<br>     ${map[‘003‘].name}-${map[‘003‘].age }<br>

  運行結果如下:

 

4)、EL運算式計算

 利用el運算式完成以下幾種運算式的計算

10+5=${10+5} <br>    10*5=${10*5} <br>    10/5=${10/5} <br>    <hr>    <%-- 比較運算 --%>    10>5:${10>5}<br>    10<5:${10<5}<br>    2=2:${2==2}<br>    <hr>    <%-- 邏輯運算子 --%>    true && false :${true && false}<br>    true || false :${true || false}<br>    !true:${!true }<br>    <hr>    <%--判斷是否為空白 --%>    <%    String name= "gqxing";    String value1=null;    String value2="";    //放進域對象中    pageContext.setAttribute("name", name);    pageContext.setAttribute("value1", value1);    pageContext.setAttribute("value2", value2);     %>    ${name==null}<br>    ${value1==null}<br>    ${value2==""}<br>    <%--一下兩種方法去判斷Null 字元串 --%>    ${value1==null || value1==""}<br>    ${empty name}<br>    

  結果如下:

 

 

 

Jsp的EL作用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.