1.EL:html
Expression Language,能夠很方便地獲取各類參數:java
${參數名}git
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%>//不一樣版本的tomcat是否默認開啓對EL表達式的支持,是不必定的,加這一句保險些 <%@ 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> <% request.setAttribute("name","god"); session.setAttribute("name","session"); application.setAttribute("name","apply"); pageContext.setAttribute("name","page"); Cookie c = new Cookie("cookie","cool"); response.addCookie(c); //EL會按照從小到大的優先級順序獲取 //${name} //pageContext>request>session>application %> ${requestScope.name } ${sessionScope.name } ${applicationScope.name } ${pageScope.name } <%--獲取cookie名稱(鍵)--%> ${cookie.cookie.name } <%--獲取cookie值 --%> ${cookie.cookie.value } <%--獲取request傳過來的值--%> ${param.hero } <%--獲取web根路徑--%> ${pageContext.request.contextPath } <%--空判斷--%> ${empty name }; <%--equals判斷--%> ${name eq 'god' } </body> </html>
EL表達式可以很方便的獲取對象的屬性,像這樣 ${hero.name} ,就會自動調用getName方法了。web
EL也能夠使用三目運算符:sql
${product.is_hot==1 ?"是":"否"}tomcat
2.JSTL:cookie
JSTL JSP Standard Tag Library 標準標籤庫
JSTL容許開人員能夠像使用HTML標籤 那樣在JSP中開發Java功能。
JSTL庫有core, i18n, fmt, sql 等等。
i18n和sql用的不多,core和fmt在工做中會用到session
經常使用語法:app
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 首先要導入標籤庫jsp
<c:set var="name" value="${'gareen'}" scope="request" />
在做用域request中設置name,至關於<%request.setAttribute("name","gareen")%>
<c:out value="${name}" />
至關於 <%=request.getAttribute("name")%>
<c:remove var="name" scope="request" />
在做用域request中刪掉name,至關於<%request.removeAttribute("name")%>
做用域能夠是pageContext, request, session, application
if else: JSTL中沒有else標籤,但咱們能夠使用!來得到相同效果
<c:if test="布爾值"></c:if>,結合EL來使用
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <c:set var="hp" value="${10}" scope="request" /> <c:if test="${hp<5}"> <p>這個英雄要掛了</p> </c:if> <c:if test="${!(hp<5)}"> <p>這個英雄以爲本身還能夠再搶救搶救</p> </c:if> <% pageContext.setAttribute("weapon", null); pageContext.setAttribute("lastwords", ""); pageContext.setAttribute("items", new ArrayList()); %> <c:if test="${empty weapon}"> <p>沒有裝備武器</p> </c:if> <c:if test="${empty lastwords}"> <p>掛了也沒有遺言</p> </c:if> <c:if test="${empty items}"> <p>物品欄爲空</p> </c:if>
<c:forEach></c:forEach> 加強型for循環
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" import="java.util.*" import="bean.User"%> <%@ 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> <% List<User> ulist = new ArrayList<User>(); User u1 = new User("張三","123",20,"男"); User u2 = new User("李四","123",22,"男"); User u3 = new User("王五","123",18,"男"); ulist.add(u1); ulist.add(u2); ulist.add(u3); request.setAttribute("ulist",ulist); %> <table border="1px"> <c:forEach items="${ulist }" var="user" varStatus="st">//要遍歷的對象 形參 遍歷狀態 <tr> <td><c:out value="${st.count}" /></td>//獲取遍歷的位次 <td><c:out value="${user.uname}" /></td> <td><c:out value="${user.pwd}" /></td> <td><c:out value="${user.sex}" /></td> <td><c:out value="${user.age}" /></td> </tr> </c:forEach> </table> </body> </html>
循環必定次數並輸出數字:<c:forEach var="s" begin="1" end="10">${s}</c:forEach>
format格式化:
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix='fmt' %> 導入標籤庫
格式化小數:
<fmt:formatNumber type="number" value="${money}" minFractionDigits="2"/>
fmt:formatNumber 表示格式化數字
minFractionDigits 小數點至少要有的位數
maxFractionDigits 小數點最多能有的位數
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix='fmt' %> <c:set var="money" value="888.8" /> <c:set var="pi" value="3.1415926" /> 最少兩個小數點: <fmt:formatNumber type="number" value="${money}" minFractionDigits="2"/> <br> 最多兩個小數點: <fmt:formatNumber type="number" value="${pi}" maxFractionDigits="2" />
格式化日期:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix='fmt' %> <% Date now = new Date(); pageContext.setAttribute("now",now); %> 完整日期: <fmt:formatDate value="${now}" pattern="G yyyy年MM月dd日 E"/><br> 完整時間: <fmt:formatDate value="${now}" pattern="a HH:mm:ss.S z"/><br> 常見格式: <fmt:formatDate value="${now}" pattern="yyyy-MM-dd HH:mm:ss"/>