1. 程式人生 > >javaweb之jstl標籤庫

javaweb之jstl標籤庫

JSTL標籤庫

Apache提供的標籤庫, jar包:jstl-1.2.jar,如果傅MyEclipse,它會在我們匯入jar包,無需自己匯入,如果沒有使用MyEclipse那麼需要自行匯入。

<c:set>

  • <c:set var=“a” value=“hello”/> 建立名為a,值為hello的域屬性,範圍:page
  • <c:set var=“a” value=“hello” scope=“session”/> 範圍為session

<c:out>

  • <c:out value=“aaa”/> 輸出字串aaa
  • <c:out value="KaTeX parse error: Expected '}', got 'EOF' at end of input: …/> 輸出域屬性aaa,其中與
    {aaa}相同
  • <c:out value="aaa&quot;default=&quot;xxx&quot;/&gt;{aaa}&quot; default=&quot;xxx&quot;/&gt;如果{aaa}不存在,那麼輸出xxx字串
  • <c:out value="aaa&quot;escapeXml=&quot;true&quot;/&gt;{aaa}&quot; escapeXml=&quot;true&quot;/&gt;如果
    {aaa}中包含特殊字元,那麼轉義它。這可以防止javascript攻擊

<c:remove>

  • <c:remove var=“a”/> 刪除名為a的域屬性
  • <c:remove var=“a” scope=“page”/> 刪除page域中名為a的域屬性

<c:url>

  • <c:url value="/AServlet"/> 輸出URL:/專案名/AServlet
  • <c:url value="/AServlet" var=“url” scope=“page”/> 把生成的url儲存到page域中,而不會輸出
  • <c:url value="/AServlet">:輸出URL:/專案名/AServlet?username=%xx%xx%xx%xx%xx%xx,其中張三會被URL編碼 <c:param name=“username” value=“張三”/> </c:url/>

<c:if>

  • <c:if test="${條件}"> 當條件為true時執行標籤體內容 hello </c:if>

<c:choose>

  • <c:choose> <c:when test="1&quot;&gt;a&lt;/c:when&gt;&lt;c:whentest=&quot;{條件1}&quot;&gt;a&lt;/c:when&gt; &lt;c:when test=&quot;{條件2}">b</c:when> <c:when test="${條件3}">c</c:when> <c:otherwise>d</c:otherwise> </c:choose>

    等同與: if() { } esle if() { } esle if() { } else if() { } else { }

<c:forEach>

可以用來遍歷陣列、List、Map、

  1. 計數迴圈

<c:forEach begin=“1” end=“10” var=“i”> ${i} </c:forEach> 等同於 for(int i = 1; i <= 10; i++) { out.println(i); }

<c:forEach begin=“1” end=“10” var=“i” step=“2”> ${i} </c:forEach> 等同於 for(int i = 1; i <= 10; i+=2) { out.println(i); }

  1. 遍歷陣列

<% String[] names = {“zhangSan”, “liSi”, “wangWu”, “zhaoLiu”}; pageContext.setAttribute(“ns”, names); %> <c:forEach var=“item " items=”${ns } "> <c:out value="name: ${item } "/> </c:forEach>

  1. 遍歷List

<% List names = new ArrayList(); names.add(“zhangSan”); names.add(“liSi”); names.add(“wangWu”); names.add(“zhaoLiu”); pageContext.setAttribute(“ns”, names); %> <c:forEach var=“item” items="${ns }"> <c:out value=“name: ${item }”/> </c:forEach>

  1. 遍歷Map

<% Map<String,String> stu = new LinkedHashMap<String,String>(); stu.put(“number”, “N_1001”); stu.put(“name”, “zhangSan”); stu.put(“age”, “23”); stu.put(“sex”, “male”); pageContext.setAttribute(“stu”, stu); %> <c:forEach var=“item " items=”stu&quot;&gt;&lt;c:outvalue=&quot;{stu }&quot;&gt; &lt;c:out value=&quot;{item.key }: ${item.value } "/> </c:forEach>

  1. 迴圈狀態物件

迴圈狀態物件是用來說明迴圈的狀態的,屬性如下: count:int型別,當前以遍歷元素的個數; index:int型別,當前元素的下標; first:boolean型別,是否為第一個元素; last:boolean型別,是否為最後一個元素; current:Object型別,表示當前專案。

<c:forEach var=“item” items="ns&quot;varStatus=&quot;vs&quot;&gt;&lt;c:iftest=&quot;{ns }&quot; varStatus=&quot;vs&quot; &gt; &lt;c:if test=&quot;{vs.first } “>第一行:</c:if> <c:if test=”vs.last&quot;&gt;&lt;/c:if&gt;&lt;c:outvalue=&quot;{vs.last } &quot;&gt;最後一行:&lt;/c:if&gt; &lt;c:out value=&quot;第{vs.count } 行: “/> <c:out value=”[${vs.index } ]: "/> <c:out value="name: ${vs.current } "/> </c:forEach>

匯入JSTL格式化標籤庫

<% Date date = new Date(); pageContext.setAttribute(“d”, date); %> <fmt:formatDate value="${d }" pattern="yyyy-MM-dd HH:mm:ss "/>

<% double d1 = 3.5; double d2 = 4.4; pageContext.setAttribute(“d1”, d1); pageContext.setAttribute(“d2”, d2); %> <fmt:formatNumber value="d1&quot;pattern=&quot;0.00&quot;/&gt;&lt;br/&gt;&lt;fmt:formatNumbervalue=&quot;{d1 }&quot; pattern=&quot;0.00 &quot;/&gt;&lt;br/&gt; &lt;fmt:formatNumber value=&quot;{d2 }" pattern="#.## "/>

  • pattern:0.00,表示小數不足兩位時,使用0補足兩位
  • pattern:#.##,表示小數不足兩位時,有幾位顯示幾位,不會補足

============================

============================

自定義標籤

自定義標籤:

  1. 實現Tag介面,即傳統標籤。不建議使用!
  2. 實現SimpleTag介面,即簡單標籤。建議使用!

JavaWeb中還提供了SimpleTagSupport類,繼承它要比實現SimpleTag介面方便。

步驟:

  1. 標籤處理類:繼承SimpleTagSupport類 public class HelloTag extends SimpleTagSupport { public void doTag() throws JspException, IOException { // 獲取JspContext物件,再獲取out物件,再向頁面輸出 // 獲取到的JspContext其實就是當前頁面的pageContext物件 this.getJspContext().getOut().write(“

    Hello SimpleTag!

    ”) ; } }
  2. 標籤描述符檔案(tld)

/WEB-INF/tlds/itcast.tld

<?xml version="1.0" encoding="UTF-8"?>
<tlib-version>1.0</tlib-version> 
<short-name>itcast</short-name> 
<uri>http://www.itcast.cn/tags</uri> 
<tag> 
	<name>hello</name> <!--標籤名稱-->
	<tag-class>cn.itcast.tag.HelloTag</tag-class> <!--標籤處理類名稱-->
	<body-content>empty</body-content> <!--標籤體為空,即空標籤-->
</tag>
  1. jsp頁面中使用自定義標籤

<%@ taglib prefix=“it” uri="/WEB-INF/hello.tld" %> … it:hello/

有標籤體的標籤

  1. 標籤處理類 public class HelloTag extends SimpleTagSupport { public void doTag() throws JspException, IOException { PageContext pc = (PageContext) this.getJspContext(); HttpServletRequest req = (HttpServletRequest) pc.getRequest(); String s = req.getParameter(“exec”); if(s != null && s.endsWith(“true”)) { // 獲取標籤體物件 JspFragment body = this.getJspBody() ; // 執行標籤體 body.invoke (null); }

    } }

  2. tld

    hello cn.itcast.tags.HelloTag scriptless

不執行標籤下面的頁面內容

public void doTag() throws JspException, IOException {
	this.getJspContext().getOut().print("<h1>只能看到我!</h1>");
	throw new SkipPageException();
}

帶有屬性的標籤

public class IfTag extends SimpleTagSupport { private boolean test;//設定屬性,提供getter/setter方法 public boolean isTest() { return test; } public void setTest (boolean test) { this.test = test; } @Override public void doTag() throws JspException, IOException { if(test) {//如果test為true,執行標籤體內容 this.getJspBody().invoke(null); } } }

<tag> 
	<name>if</name> 
	<tag-class>cn.itcast.tag.IfTag</tag-class> 
	<body-content>scriptless</body-content>
	<!--部署屬性-->
	<attribute> 
		<name>test</name> <!--屬性名-->
		<required>true</required> <!--屬性是否為必須的-->
		<rtexprvalue>true</rtexprvalue> <!--屬性值是否可以為EL或JSTL等-->
	</attribute> 
</tag>

MVC

  1. 什麼是MVC   MVC模式(Model-View-Controller)是軟體工程中的一種軟體架構模式,把軟體系統分為三個基本部分:模型(Model)、檢視(View)和控制器(Controller)。
  • 控制器Controller:對請求進行處理,負責請求轉發;
  • 檢視View:介面設計人員進行圖形介面設計;
  • 模型Model:程式編寫程式應用的功能(實現演算法等等)、資料庫管理;
  1. Java與MVC

JSP Model1第一代:JSP + DB JSP Model1第二代:JSP + javabean + DB JSP Model2:JSP + Servlet + JavaBean + DB

JavaWeb三層框架

  • Web層(表述層):與Web相關的,例如jsp、servlet都是Web層
  • Business層(業務邏輯層):封裝業務邏輯,通常對應一個業務功能,例如登入、註冊都是一個業務功能。
  • Data層(資料訪問層):封裝對資料庫的操作,通常對應一次對資料庫的訪問,例如新增、修改、刪除、查詢等。