Servlet 之jsp(中)--EL

來源:互聯網
上載者:User

標籤:

  JSP頁面中很多時候,指令碼都可以用el運算式來代替。在各種引用地址中,由於簡便靈活,所以el被廣泛應用。el使用時一般需要配合標籤jstl,標籤的版本各容器不同也有差異,使用時需要考慮到。

...<link rel="stylesheet"    href="${pageContext.request.contextPath}/static/bower/bootstrap/css/bootstrap.min.css"><link rel="stylesheet"    href="${pageContext.request.contextPath}/static/bower/bootstrap-datepicker/css/bootstrap-datepicker.min.css"><link rel="stylesheet"    href="${pageContext.request.contextPath}/static/css/content.css" />...

基本的格式就是${運算式},要注意的是,如果想用運算式表達自訂的資料,那麼該資料必須存放於域中,el運算式規定了4個範圍,pageScope, requestScope, sessionScope以及applicationScope,另外el規定了pageContext,相當於jsp中的PageContext對象。

  還有幾個el的內建對象為:

  param 一個請求參數 ${param.username} request.getParameter("username");
  paramValues 一組 ${paramValues.loves} request.getParameterValues("loves");
  header 一個頭 ${header.referer} request.getHeader("referer");
  headerValues 一組頭 ${header.cookie} request.getHeaders("cookie");
  cookie 獲得cookie對象
  initParam WEB項目初始化參數, servletContext.getInitParameter("xxx");

共11個el的內建對象,所以頁面取值也比較靈活,常用取值運算式如下:

${pageContext.request.queryString} 取得請求的參數字串${pageContext.request.requestURL} 取得請求的URL,但不包含請求參數字串${pageContext.request.contextPath} 取得服務的web application的名稱${pageContext.request.method} 取得HTTP的方法(GET、POST)${pageContext.request.protocol} 取得使用的協議(HTTP/1.1、HTTP/1.0)${pageContext.request.remoteUser} 取得使用者名稱稱${pageContext.request.remoteAddr} 取得使用者的IP地址${pageContext.session.new} 判斷session是否為新的。所謂新的session,表示剛由server產生而client 尚未使用${pageContext.session.id} 取得session的ID${pageContext.servletContext.serverInfo} 取得主機端的服務資訊

需要注意的是,如果要求標頭中有"-",那麼需要用headerValues["Accept-Encoding"],另外測試cookie時,${cookie.key}指代的是cookie對象,如果要裡面的名字與內容就直接表示為:${cookie.key.name}與${cookie.key.value}.

  [‘‘]與.都是el中的運算子,大多時候二者可以等價使用,也可以混合使用,

    例如:${sessionScope.book.title} 獲得book對象的title值
    等價 ${sessionScope.book["title"]}

    又如:${sessionScope.bookList[0]["title"]},

  如果屬性值中包含了一些特殊字元,則我們也需要用[‘‘]運算子來對它進行取值。注意[]裡要加引號,單雙無所謂,但必須加。

=========================================================>

  算術運算子:+ - * /(或div) %(或mod)

  關係運算子:== != >= <= > < (或:eq ne ge le gt lt) && || !(或and or not) 結果為true或者false

  empty運算子在判斷是否為null "" 以及集合數組沒有內容是很好用,返回true/false

  二元運算式

 =========================================================>

  引用el函數

  首先就是引用規範中提供用於處理輸出內容的函數,需要在頁面中加上:

<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

el輸出顯示到頁面的內容都是字串,fn就是對字串進行操作的函數庫。

...<body>    <hr>    ${fn:contains("abc","a") }    <br> ${empty str }    <br> ${fn:escapeXml("<br/>") }<%--轉義 --%>    <br> ${fn:indexOf("changjiang.chen","i") }...

 =========================================================>

  建立自己的el函數庫:

  1 確定實作類別(自訂,方法必須是static)
  2 編寫設定檔(描述檔案),通知tomcat實作類別位置,tld檔案標籤描述檔案
  3 jsp 使用自己函數庫

首先是定義一個static方法,

public class MyElFunction {    public static String myFunc(String str) {        if (!str.equals("")) {            return str + " success!";        } else {            return "kong";        }    }}

在WEB-INF下任意位置建立一個tld檔案,

<?xml version="1.0" encoding="UTF-8" ?><taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"    version="2.1">    <tlib-version>1.1</tlib-version>    <short-name>myfn</short-name>    <uri>http://www.changhong.com</uri>    <function>        <name>myFunc</name>        <function-class>com.test.el4t.MyElFunction</function-class>        <function-signature>java.lang.String myFunc(java.lang.String)</function-signature>    </function></taglib>

最後是在自己的jsp頁面中進行引用:

<%@taglib uri="http://www.changhong.com" prefix="myfn"%><%@ page isELIgnored="false"%>...    <br> ${myfn:myFunc("This is a test my el function!") }</body>...

即可在頁面中顯示

This is a test my el function! success!

 

Servlet 之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.