EL表達(dá)式
表達(dá)式語(yǔ)言(Expression Language)审孽,或稱EL表達(dá)式钳吟,簡(jiǎn)稱EL,是Java中的一種特殊的通用編程語(yǔ)言鸳址,借鑒于JavaScript和XPath。主要作用是在Java Web應(yīng)用程序嵌入到網(wǎng)頁(yè)(如JSP)中泉懦,用以訪問頁(yè)面的上下文以及不同作用域中的對(duì)象 稿黍,取得對(duì)象屬性的值,或執(zhí)行簡(jiǎn)單的運(yùn)算或判斷操作崩哩。EL在得到某個(gè)數(shù)據(jù)時(shí)巡球,會(huì)自動(dòng)進(jìn)行數(shù)據(jù)類型的轉(zhuǎn)換言沐。
表達(dá)式語(yǔ)法
${變量名}
- 使用時(shí)需要導(dǎo)入外部包,在tomcat服務(wù)器上找到此包辕漂,并導(dǎo)入呢灶。
apache-tomcat-8.5.34/lib/servlet-api.jar - 案例從1.jsp 頁(yè)面提交,通過servlet跳轉(zhuǎn)到showEL.jsp頁(yè)面完成賦值的輸出
- 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、跳轉(zhuǎn)到showEL.jsp頁(yè)面跋涣,我們通過EL表達(dá)式取出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>提交后頁(yè)面</title>
</head>
<body>
用戶名:${username} <br>
年齡:${age}
</body>
</html>
JSTL
JSP標(biāo)準(zhǔn)標(biāo)簽庫(kù)(JSP Standard Tag Library)是Java EE網(wǎng)絡(luò)應(yīng)用程序開發(fā)平臺(tái)的組成部分缨睡。它在JSP規(guī)范的基礎(chǔ)上,擴(kuò)充了一個(gè)JSP的標(biāo)簽庫(kù)來(lái)完成一些通用任務(wù)陈辱,比如XML數(shù)據(jù)處理奖年、條件執(zhí)行、數(shù)據(jù)庫(kù)訪問沛贪、循環(huán)和國(guó)際化陋守。
- 需要導(dǎo)入此jar包
- jsp頁(yè)面導(dǎo)入<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
JSTL標(biāo)簽介紹
- 通用標(biāo)簽 set out remove
- 條件標(biāo)簽 if choose
- 迭代標(biāo)簽 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標(biāo)簽 -->
<!-- set標(biāo)簽主要往指定的域中存放數(shù)據(jù) -->
<c:set var="user" value="張三" scope="request"></c:set>
<!-- 將數(shù)據(jù)打印輸出 -->
<c:out value="${user}"></c:out>
<!-- remove標(biāo)簽 -->
<c:remove var="user" scope="request" />
<c:out value="${user}"></c:out>
<!--
if標(biāo)簽
test:按判斷的條件,如果條件為true,執(zhí)行標(biāo)簽體中的內(nèi)容
-->
<c:set var="age" value="12" scope="request"></c:set>
<c:if test="${age==11}">
您的年齡為12歲
</c:if>
<hr>
<!-- choose標(biāo)簽 -->
<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", "聯(lián)想筆記本");
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頁(yè)面取出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表達(dá)式迭代List集合</title>
</head>
<body>
<table border="1" cellspacing="0" >
<tr>
<td>商品名稱</td>
<td>產(chǎn)地</td>
<td>價(jià)格</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>