1. 程式人生 > >7-EL表達式和JSTL表達式

7-EL表達式和JSTL表達式

borde body get .get 導入 bsp 文件 center jstl

引入jar包

技術分享圖片

一。EL表達式
1.表達式語言,用於jsp網頁中獲取和計算數據
2.語法:${表達式}
3.用於取值:可以從pageContext,request,session,application這些域中獲取後臺數據。順序是從小到大
4.指定作用域來取值:${requestScope.對象名},${sessionScope.對象名},${applicationScope.對象名}
5.EL表達式的開關:<%@ page isELIgnored="false" %> fasle:EL表達式有效 true:EL表達式無效
6.EL表達式的判斷:
比較大小:> < ==
是否為空:${empty 對象名} ${not empty 對象名}

二。JSTL:jsp的標準標簽庫
使用步驟:
1.在項目中導入jar文件:jstl-1.2.jar,standard-1.1.2.jar
2.在頁面需要通過指令引入標簽庫
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
3.使用標簽:
常用標簽
<c:if test="${判斷條件}">
<c:forEach items="${集合名}" var="叠代對象別名">內容由EL表達式獲取</c:forEach>
<C:redirect src="url">重定向
<c:out value="${}">頁面輸出

<c:set var="變量名" value="${值}" scope="作用域">
<c:remove var="變量名" scope="作用域">

4.fmt標簽:是用作格式化輸出的標簽
引入:<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<fmt:formatDate value="${ci.beginDate }" pattern="yyyy-MM-dd"/>

例:用el表達式替換jsp頁面中的java代碼

<%@page import="com.pojo.Student
"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html> <html> <head> <base href="<%=basePath%>"/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <style type="text/css"> </style> </head> <script type="text/javascript"> function del(id){ if(confirm("是否確定要刪除該數據?")){ window.location.href="updateStu?type=del&id="+id; } } </script> <c:set var="username" value="${stu.stuName}" scope="session"></c:set> <c:remove var="username" scope="session"></c:remove> <body> <div align="center"> <table style="width: 500px;" border="1"> <tr> <th>編號</th> <th>姓名</th> <th>年齡</th> <th>性別</th> <th>生日</th> <th>專業</th> <th>操作</th> </tr> <c:forEach items="${list}" var="stu"> <tr> <td>${stu.stuId}</td> <td>${stu.stuName }</td> <td>${stu.stuAge}</td> <td> <c:if test="${stu.stuSex==‘1‘ }"></c:if> <c:if test="${stu.stuSex==‘2‘ }"></c:if> </td> <td><fmt:formatDate value="${stu.stuDate }" pattern="yyyy-MM-dd"/></td> <td>${stu.showStuProfess}</td> <td><a href="javascript:del(‘${stu.stuId }‘)">刪除</a> <a href="updateStu?type=toupdate&id=${stu.stuId }">修改</a></td> </tr> </c:forEach> </table> </div> </body> </html>

7-EL表達式和JSTL表達式