EL表達式、JSTL表達式和OGNL表達式

EL表達式

表達式語言(Expression Language),或稱EL表達式,簡稱EL,是Java中的一種特殊的通用編程語言,借鑑於JavaScriptXPath。主要作用是在Java Web應用程序嵌入到網頁(如JSP)中,用以訪問頁面的上下文以及不同作用域中的對象 ,取得對象屬性的值,或執行簡單的運算或判斷操作。EL在得到某個數據時,會自動進行數據類型的轉換。

表達式語法

${變量名}

  • 使用時需要導入外部包,在tomcat服務器上找到此包,並導入。
    apache-tomcat-8.5.34/lib/servlet-api.jar
  • 案例從1.jsp 頁面提交,通過servlet跳轉到showEL.jsp頁面完成賦值的輸出
  • 1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>表單提交</title>
</head>
<body>
    <form action="<%=request.getContextPath() %>/ELServlet" method="post">
        用戶名:<input type="text" name="username"><br>
        年齡: <input type="text" name="age"><br>
        <input type="submit" value="提交"> 
    </form>
    
</body>
</html>

 

  • ELServlet 
package com.alan.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/ELServlet")
public class ELServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、首先獲取username和age的屬性值
        String username = request.getParameter("username");
        String age = request.getParameter("age");
        //2、將其保存到request域中
        request.setAttribute("username", username);
        request.setAttribute("age", age);
        //3、跳轉到showEL.jsp頁面,我們通過EL表達式取出request域中的值
        request.getRequestDispatcher("/showEL.jsp").forward(request, response);
        
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);

    }

}
  • showEL.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>提交後頁面</title>
</head>
<body>
    用戶名:${username} <br>
    年齡:${age} 

</body>
</html>

JSTL(c標籤)

JSP標準標籤庫JSP Standard Tag Library)是Java EE網絡應用程序開發平臺的組成部分。它在JSP規範的基礎上,擴充了一個JSP的標籤庫來完成一些通用任務,比如XML數據處理、條件執行、數據庫訪問、循環和國際化

JSTL標籤介紹

  • 通用標籤 set out remove
  • 條件標籤 if choose
  • 迭代標籤 forEach
  • 案例1
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

    <!-- set、out、remove標籤 -->
    <!-- set標籤主要往指定的域中存放數據 -->
    <c:set var="user" value="張三" scope="request"></c:set>
    <!-- 將數據打印輸出 -->
    <c:out value="${user}"></c:out>
    <!-- remove標籤 -->
    <c:remove var="user" scope="request" />
    <c:out value="${user}"></c:out>
    <!-- if標籤 test:按判斷的條件,如果條件爲true,執行標籤體中的內容 -->
    <c:set var="age" value="12" scope="request"></c:set>
    <c:if test="${age==11}">
        您的年齡爲12歲
    </c:if>
    <hr>
    <!-- choose標籤 -->
    <c:set var="age1" value="13" scope="request"></c:set>
    <c:choose>
        <c:when test="${age1==12}">
            您的年齡爲12歲
        </c:when>
        <c:otherwise>
            您的年齡不是12歲
        </c:otherwise>
    </c:choose>

</body>
</html>
  • 案例2 foreach
  • servelet
package com.alan.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/JSTLELServlet")
public class JSTLELServlet extends HttpServlet {
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        //1、將輸入存入List中
        Map<String,Object> dataMap1 =  new HashMap<>();
        dataMap1.put("shopName", "聯想筆記本");
        dataMap1.put("address", "北京");
        dataMap1.put("price", "4999");
        Map<String,Object> dataMap2 =  new HashMap<>();
        dataMap2.put("shopName", "神州筆記本");
        dataMap2.put("address", "南京");
        dataMap2.put("price", "3999");
        Map<String,Object> dataMap3 =  new HashMap<>();
        dataMap3.put("shopName", "小米筆記本");
        dataMap3.put("address", "深圳");
        dataMap3.put("price", "5999");
        
        List<Map<String,Object>> dataList = new ArrayList<>();
        dataList.add(dataMap1);
        dataList.add(dataMap2);
        dataList.add(dataMap3);
        
        //2、將List賦值到request的作用域中
        request.setAttribute("list", dataList);
        //3、在jsp頁面取出List
        request.getRequestDispatcher("/2.jsp").forward(request, response);
    
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
  • jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>通過JSTL+EL表達式迭代List集合</title>
</head>
<body>

    <table border="1" cellspacing="0" >
        <tr>
            <td>商品名稱</td>
            <td>產地</td>
            <td>價格</td>
        </tr>
        <c:forEach items="${list}" var="map">
            <tr>
                <td>${map.shopName }</td>
                <td>${map.address }</td>
                <td>${map.price}</td>
            </tr>
        </c:forEach>
    </table>
</body>
</html>

OGNL表達式(s標籤)

OGNL(Object Graphic Navigation Language)對象圖導航語言的縮寫,它是一個開源項目。Struts2框架使用OGNL作爲默認的表達式語言。

  • 作用:取值,獲取javaBean中的屬性,獲取List或者數組元素,獲得Map的鍵值對,還可以執行邏輯運算。
  • 要求:我們必須把OGNL表達式寫在struts的標籤中。
  • ognl對普通方法的調用
<%--  在<s:property/>的value屬性中內部是ognl表達式,如果要輸出字符串要加''--%>
<s:property value="'zhangsan'"/>

<%--  在<s:property/>的value屬性中內部是ognl表達式,可以使用java的api--%>
<s:property value="'zhangs'.toUpperCase()"/>
  • ognl對靜態變量和靜態方法的使用

靜態變量和靜態方法的調用都要使用@中間是類的全路徑@【靜態變量或靜態方法】,但是如果是靜態方法的調用必須要在struts.xml中配置開啓對靜態方法的調用

<!-- 開啓ognl對靜態方法的調用 -->
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>

<s:property value="@java.lang.Integer@MAX_VALUE"/>
<s:property value="@java.lang.Math@abs(-100)"/>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章