<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # JSP 表達式語言 JSP表達式語言(EL)使得訪問存儲在JavaBean中的數據變得非常簡單。JSP EL既可以用來創建算術表達式也可以用來創建邏輯表達式。在JSP EL表達式內可以使用整型數,浮點數,字符串,常量true、false,還有null。 ## 一個簡單的語法 典型的,當您需要在JSP標簽中指定一個屬性值時,只需要簡單地使用字符串即可: ``` <jsp:setProperty name="box" property="perimeter" value="100"/> ``` JSP EL允許您指定一個表達式來表示屬性值。一個簡單的表達式語法如下: ``` ${expr} ``` 其中,expr指的是表達式。在JSP EL中通用的操作符是"."和"[]"。這兩個操作符允許您通過內嵌的JSP對象訪問各種各樣的JavaBean屬性。 舉例來說,上面的&lt;jsp:setProperty&gt;標簽可以使用表達式語言改寫成如下形式: ``` <jsp:setProperty name="box" property="perimeter" value="${2*box.width+2*box.height}"/> ``` 當JSP編譯器在屬性中見到"${}"格式后,它會產生代碼來計算這個表達式,并且產生一個替代品來代替表達式的值。 您也可以在標簽的模板文本中使用表達式語言。比如&lt;jsp:text&gt;標簽簡單地將其主體中的文本插入到JSP輸出中: ``` <jsp:text> <h1>Hello JSP!</h1> </jsp:text> ``` 現在,在&lt;jsp:text&gt;標簽主體中使用表達式,就像這樣: ``` <jsp:text> Box Perimeter is: ${2*box.width + 2*box.height} </jsp:text> ``` 在EL表達式中可以使用圓括號來組織子表達式。比如${(1 + 2) * 3}等于9,但是${1 + (2 * 3)} 等于7。 想要停用對EL表達式的評估的話,需要使用page指令將isELIgnored屬性值設為true: ``` <%@ page isELIgnored ="true|false" %> ``` 這樣,EL表達式就會被忽略。若設為false,則容器將會計算EL表達式。 ## EL中的基礎操作符 EL表達式支持大部分Java所提供的算術和邏輯操作符: | **操作符** | **描述** | | --- | --- | | . | 訪問一個Bean屬性或者一個映射條目 | | [] | 訪問一個數組或者鏈表的元素 | | ( ) | 組織一個子表達式以改變優先級 | | + | 加 | | - | 減或負 | | * | 乘 | | / or div | 除 | | % or mod | 取模 | | == or eq | 測試是否相等 | | != or ne | 測試是否不等 | | < or lt | 測試是否小于 | | > or gt | 測試是否大于 | | <= or le | 測試是否小于等于 | | >= or gt | 測試是否大于等于 | | && or and | 測試邏輯與 | | &#124;&#124; or or | 測試邏輯或 | | ! or not | 測試取反 | | empty | 測試是否空值 | ## JSP EL中的函數 JSP EL允許您在表達式中使用函數。這些函數必須被定義在自定義標簽庫中。函數的使用語法如下: ``` ${ns:func(param1, param2, ...)} ``` ns指的是命名空間(namespace),func指的是函數的名稱,param1指的是第一個參數,param2指的是第二個參數,以此類推。比如,有函數fn:length,在JSTL庫中定義,可以像下面這樣來獲取一個字符串的長度: ``` ${fn:length("Get my length")} ``` 要使用任何標簽庫中的函數,您需要將這些庫安裝在服務器中,然后使用&lt;taglib&gt;標簽在JSP文件中包含這些庫。 ## JSP EL隱含對象 JSP EL支持下表列出的隱含對象: | **隱含對象** | **描述** | | --- | --- | | pageScope | page 作用域 | | requestScope | request 作用域 | | sessionScope | session 作用域 | | applicationScope | application 作用域 | | param | Request 對象的參數,字符串 | | paramValues | Request對象的參數,字符串集合 | | header | HTTP 信息頭,字符串 | | headerValues | HTTP 信息頭,字符串集合 | | initParam | 上下文初始化參數 | | cookie | Cookie值 | | pageContext | 當前頁面的pageContext | 您可以在表達式中使用這些對象,就像使用變量一樣。接下來會給出幾個例子來更好的理解這個概念。 ## pageContext對象 pageContext對象是JSP中pageContext對象的引用。通過pageContext對象,您可以訪問request對象。比如,訪問request對象傳入的查詢字符串,就像這樣: ``` ${pageContext.request.queryString} ``` ## Scope對象 pageScope,requestScope,sessionScope,applicationScope變量用來訪問存儲在各個作用域層次的變量。 舉例來說,如果您需要顯式訪問在applicationScope層的box變量,可以這樣來訪問:applicationScope.box。 ## param和paramValues對象 param和paramValues對象用來訪問參數值,通過使用request.getParameter方法和request.getParameterValues方法。 舉例來說,訪問一個名為order的參數,可以這樣使用表達式:${param.order},或者${param["order"]}。 接下來的例子表明了如何訪問request中的username參數: ``` <%@ page import="java.io.*,java.util.*" %> <% String title = "Accessing Request Param"; %> <html> <head> <title><% out.print(title); %></title> </head> <body> <center> <h1><% out.print(title); %></h1> </center> <div align="center"> <p>${param["username"]}</p> </div> </body> </html> ``` param對象返回單一的字符串,而paramValues對象則返回一個字符串數組。 ## header和headerValues對象 header和headerValues對象用來訪問信息頭,通過使用 request.getHeader方法和request.getHeaders方法。 舉例來說,要訪問一個名為user-agent的信息頭,可以這樣使用表達式:${header.user-agent},或者${header["user-agent"]}。 接下來的例子表明了如何訪問user-agent信息頭: ``` <%@ page import="java.io.*,java.util.*" %> <% String title = "User Agent Example"; %> <html> <head> <title><% out.print(title); %></title> </head> <body> <center> <h1><% out.print(title); %></h1> </center> <div align="center"> <p>${header["user-agent"]}</p> </div> </body> </html> ``` 運行結果如下: ![jsp-expression-language](https://box.kancloud.cn/2015-12-15_566fe70f242a0.jpg) header對象返回單一值,而headerValues則返回一個字符串數組。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看