日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

学会怎样使用Jsp 内置标签、jstl标签库及自定义标签

發(fā)布時(shí)間:2024/7/19 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 学会怎样使用Jsp 内置标签、jstl标签库及自定义标签 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

學(xué)習(xí)jsp不得不學(xué)習(xí)jsp標(biāo)簽,一般來說,對于一個(gè)jsp開發(fā)者,可以理解為jsp頁面中出現(xiàn)的java代碼越少,對jsp的掌握就越好,而替換掉java代碼的重要方式就是使用jsp標(biāo)簽。

?jsp標(biāo)簽的分類:

  1)內(nèi)置標(biāo)簽(動(dòng)作標(biāo)簽): 不需要在jsp頁面導(dǎo)入標(biāo)簽

  2)jstl標(biāo)簽: 需要在jsp頁面中導(dǎo)入標(biāo)簽

  3)自定義標(biāo)簽 : 開發(fā)者自行定義,需要在jsp頁面導(dǎo)入標(biāo)簽

? 1、內(nèi)置標(biāo)簽(動(dòng)作標(biāo)簽): ?

<jsp:forward />轉(zhuǎn)發(fā)標(biāo)簽:

  語法:<jsp:forward page="/MyJsp001.jsp"></jsp:forward>
相當(dāng)于java代碼:request.getRequestDispatcher("/MyJsp001.jsp?name=jxf").forward(request, response);
注意:但是java代碼的轉(zhuǎn)發(fā)可以通過url帶參數(shù)的方式進(jìn)行傳遞參數(shù),而轉(zhuǎn)發(fā)標(biāo)簽需要借助于下面的
<jsp:param>標(biāo)簽實(shí)現(xiàn)參數(shù)傳遞

<jsp:pararm/>參數(shù)標(biāo)簽:?

語法: <jsp:param value="jxf" name="name"/> <%-- 傳遞一個(gè)名為name,值為jxf的參數(shù),參數(shù)一般作為其他標(biāo)簽的子標(biāo)簽使用--%>結(jié)合<jsp:forward>標(biāo)簽用法: <jsp:forward page="/MyJsp001.jsp"><jsp:param value="jxf" name="name"/> </jsp:forward> 

<jsp:include/>包含標(biāo)簽:?

語法:<jsp:include page="/MyJsp001.jsp"><jsp:param value="jxf" name="name"/><%--可以將參數(shù)傳遞給包含進(jìn)來的頁面--%> </jsp:include>jsp中還有一個(gè)包含指令,也是將一個(gè)頁面包含另外的頁面他們之間的區(qū)別:1、首先是語法不同<jsp:include page="/MyJsp001.jsp"><%@inclue file="被包含的頁面"%>2、<jsp:include>可以傳遞參數(shù),<%@inclue%>不可以3、<jsp:include>:包含頁面與被包含頁面分別編譯為兩個(gè)java源文件,在運(yùn)行時(shí)引用<%@inclue%>:包含頁面與被包含頁面合并編譯為一個(gè)java源文件

?2、jstl標(biāo)簽: ??

?    JSTL?(java ?standard ?tag ?libarary ??- ?java標(biāo)準(zhǔn)標(biāo)簽庫)

  jstl標(biāo)簽的類型:核心標(biāo)簽庫 (c標(biāo)簽庫)//這里主要介紹c標(biāo)簽庫,因?yàn)橛玫恼娴暮芏?/span>國際化標(biāo)簽(fmt標(biāo)簽庫)EL函數(shù)庫(fn函數(shù)庫)xml標(biāo)簽庫(x標(biāo)簽庫)//一般不用該庫,這屬于數(shù)據(jù)操作,而數(shù)據(jù)操作應(yīng)于dao層中,jsp頁面主要用于展示數(shù)據(jù)sql標(biāo)簽庫(sql標(biāo)簽庫)//與xml標(biāo)簽庫類似,不應(yīng)在jsp頁面中操作數(shù)據(jù)(當(dāng)然他們是可以在jsp頁面寫)

使用jstl標(biāo)簽庫使用前提(此處以c標(biāo)簽庫為例):

1、導(dǎo)入jstl支持包:jstl-1.2.jar(如果用MyEclipse創(chuàng)建項(xiàng)目時(shí)候選擇java EE5.0,則不需要導(dǎo)包,工程中已經(jīng)包含)

2、在頁面中使用Taglib指令導(dǎo)入標(biāo)簽庫  

<%--uri:tld文件中的uri名稱,prefix:標(biāo)簽前綴--%>
<%
@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

?

uri和prefix怎樣寫?以標(biāo)簽庫的為例:

工程名->Java EE 5 Libraries->jstl-1.2.jar->META-INF-c.tld

c標(biāo)簽庫的主要標(biāo)簽:

<c:set>標(biāo)簽:用于保存數(shù)據(jù)到域?qū)ο笾?/p>    <%--var為屬性名 value為屬性值 scope為保存屬性到那個(gè)域?qū)ο?#xff0c;默認(rèn)為page域 --%><%--相當(dāng)于java代碼<%pageContext.setAttribute("name", "jxf") --%><c:set var="name" value="jxf" scope="page"></c:set>${name}<%--EL表達(dá)式輸出page域中的name屬性值--%>

?<c:out>標(biāo)簽:顯示數(shù)據(jù)

<%--value:輸出到瀏覽器的值,${name}EL表達(dá)式取出域?qū)ο髮傩灾禐閚ame的值,
   default:當(dāng)value值為null時(shí)顯示的默認(rèn)值,
   escapeXml:表示是否轉(zhuǎn)義為xml格式(也可以理解為普通文本格式),true表示轉(zhuǎn)義,默認(rèn)false --%>
<c:out value="${name}" default="<h1>標(biāo)題h1<h1>" escapeXml="false"></c:out>

?<c:if>標(biāo)簽:但條件判斷

<%--test支持EL表達(dá)式 --%><c:if test="${true}">條件成立!</c:if>

<c:choose>+<c:when>+<c:otherwise>標(biāo)簽:多條件判斷

<%--相當(dāng)于if else結(jié)構(gòu),當(dāng)有一條件滿足后其他條件都不滿足,證:此實(shí)例只輸出 10>5--%><c:choose><c:when test="${10>5}">10>5<br /></c:when><c:when test="${6>2}">6>2<br /></c:when><c:otherwise>條件都不成立</c:otherwise></c:choose>

<c:forEach>標(biāo)簽:遍歷

<%--begin : 從哪個(gè)元素開始遍歷,默認(rèn)從0開始end : 到哪個(gè)元素結(jié)束。默認(rèn)到最后一個(gè)元素step : 步長,默認(rèn)1items : 需要遍歷的數(shù)據(jù)(集合) var : 每個(gè)元素的名稱 varStatus: 當(dāng)前正在遍歷元素的狀態(tài)對象。(count屬性:當(dāng)前位置,從1開始)
--%>
<%
    //Student{
    //  public String name;
    //  public int age;
    //  public Student(String Name,int Age){
    //    this.name = Name;
    //    this.age = age;
    //  }
    //}
//List數(shù)據(jù)準(zhǔn)備List<Student> list = new ArrayList<Student>();list.add(new Student("name001",18));list.add(new Student("name002",19));list.add(new Student("name003",20));pageContext.setAttribute("list",list);//放入域中//Map數(shù)據(jù)準(zhǔn)備Map<String,Student> map = new HashMap<String,Student>();map.put("001",new Student("name001",18));map.put("002",new Student("name002",19));map.put("003",new Student("name003",20));pageContext.setAttribute("map",map);//放入域中//array數(shù)據(jù)準(zhǔn)備Student[] array = new Student[]{new Student("name001",18),new Student("name002",19),new Student("name003",20)};pageContext.setAttribute("array",array);//放入域中%><%--遍歷list --%><c:forEach var="student" begin="0" end="2" items="${list}" step="1" varStatus="varSta">name:${student.name}---age:${student.age}<br/></c:forEach><%--遍歷map --%><c:forEach var="student" begin="0" end="2" items="${map}" step="1" varStatus="varSta">key:${student.key}---name:${student.value.name}---age:${student.value.age}<br/></c:forEach><%--遍歷array --%><c:forEach var="student" begin="0" end="2" items="${array}" step="1" varStatus="varSta">name:${student.name}---age:${student.age}<br/></c:forEach><%--一般遍歷集合或者數(shù)組都是全部遍歷,所以只寫2個(gè)屬性var和items,其他取默認(rèn)值 --%><c:forEach var="student" items="${array}">name:${student.name}---age:${student.age}<br/></c:forEach>

<c:forTokens>標(biāo)簽:切割字符串

<%--切割字符串,相當(dāng)于split函數(shù)var:切割出來的每一部分字符串items:要切割的字符串delims:分割的字符,若內(nèi)容為字符串,則這個(gè)字符串的每個(gè)字符都會(huì)當(dāng)作一個(gè)分割的字符如:items="a-b-c=d=e" dilims="-=",分割的結(jié)果為 a,b,c,d,e--%><%//數(shù)據(jù)準(zhǔn)備String str = "a-b-c=d=e";pageContext.setAttribute("str", str);%><c:forTokens var="item" items="${str}" delims="-=">${item}<br /></c:forTokens>

?3、自定義標(biāo)簽: ? ??

  當(dāng)JSTL標(biāo)簽庫已經(jīng)無法滿足我們的需求時(shí)候,就需要自己開發(fā)自定義標(biāo)簽,來滿足我們的需求,自定義標(biāo)簽實(shí)際上是一個(gè)普通的java類,繼承SimpleTagSupport類。

在介紹自定義標(biāo)簽之前,先介紹SimpleTagSupport類:

SimpleTagSupport類繼承自SimpleTag接口,而SimpleTag接口主要有以下4個(gè)方法,也可將這四個(gè)方法理解為標(biāo)簽處理器類的生命周期:

1 public interface SimpleTag extends JspTag {    2 3    /** 4 * 執(zhí)行標(biāo)簽時(shí)調(diào)用的方法,一定會(huì)調(diào)用 5 */ 6 public void doTag() throws javax.servlet.jsp.JspException, java.io.IOException; 7 8 /** 9 * 設(shè)置父標(biāo)簽對象,傳入父標(biāo)簽對象,當(dāng)標(biāo)簽存在父標(biāo)簽時(shí)會(huì)調(diào)用 10 */ 11 public void setParent( JspTag parent ); 12 13 /** 14 * 設(shè)置JspContext對象,其實(shí)他真實(shí)傳入的是其子類PageContext 15 */ 16 public void setJspContext( JspContext pc ); 17 18 /** 19 * 設(shè)置標(biāo)簽體內(nèi)容。標(biāo)簽體內(nèi)容封裝到JspFragment對象 中,然后傳入JspFragment對象 20 */ 21 public void setJspBody( JspFragment jspBody ); 22 23 }

SimpleTagSupport類在其基礎(chǔ)上封裝出了3個(gè)方法,方便自定義標(biāo)簽類編寫,該方法實(shí)現(xiàn)也比較簡單,無非就是內(nèi)部維護(hù)一個(gè)變量,通過set方法將其賦值,再返回該對象,這樣繼承SimpleTagSupport拿到如PageContext對象就直接調(diào)用getJspContext()即可。

1 /*SimpleTagSupport 類的一部分*/ 2 public class SimpleTagSupport implements SimpleTag 3 { 4 /** Reference to the enclosing tag. */ 5 private JspTag parentTag; 6 7 /** The JSP context for the upcoming tag invocation. */ 8 private JspContext jspContext; 9 10 /** The body of the tag. */ 11 private JspFragment jspBody; 12 13 public void setParent( JspTag parent ) { 14 this.parentTag = parent; 15 } 16 17 public JspTag getParent() { 18 return this.parentTag; 19 } 20 21 public void setJspContext( JspContext pc ) { 22 this.jspContext = pc; 23 } 24 25 protected JspContext getJspContext() { 26 return this.jspContext; 27 } 28 29 public void setJspBody( JspFragment jspBody ) { 30 this.jspBody = jspBody; 31 } 32 33 protected JspFragment getJspBody() { 34 return this.jspBody; 35 } 36 }

編寫自定義標(biāo)簽的步驟:

以寫一個(gè)自定義標(biāo)簽為例:功能:向?yàn)g覽器輸出一句話“酒香逢 博客園歡迎您”

1)編寫一個(gè)普通的java類(OutInfo.java),繼承SimpleTagSupport,并重寫doTag方法。(jsp文件最后編譯為java文件,查看該java文件可知_jspService方法中,會(huì)創(chuàng)建標(biāo)簽類OutInfo對象,并執(zhí)行doTag方法。編譯后原文件路徑:如D:\Program Files\Tomcat\apache-tomcat-6.0.39\work\Catalina\localhost\Tag\org\apache\jsp\xxx.java

1 /* 2 * OutInfo.java 3 */ 4 public class OutInfo extends SimpleTagSupport { 5 6 @Override 7 public void doTag() throws JspException, IOException { 8 PageContext pageContext = (PageContext)getJspContext(); 9 pageContext.getOut().write("酒香逢 博客園歡迎您"); 10 } 11 }

?2)在web項(xiàng)目的WEB-INF目錄下建立tld文件(jxf.tld),這個(gè)tld文件為標(biāo)簽庫的聲明文件,并配置好相應(yīng)的信息。(可以參考核心標(biāo)簽庫的tld文件,如:項(xiàng)目/Java EE 5 Libraries/jstl-1.2jar/META-INF/c.tld)

1 <?xml version="1.0" encoding="UTF-8" ?> 2 3 <taglib xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" 6 version="2.1"> 7 8 <description>JSTL 1.1 core library</description> 9 <display-name>JSTL core</display-name> 10 <tlib-version>1.1</tlib-version> 11 <!--標(biāo)簽庫前綴 taglib指令中的prefix屬性 --> 12 <short-name>jxf</short-name> 13 <!--tld文件的唯一標(biāo)記 taglib指令中的uri屬性 --> 14 <uri>http://jxf.tag</uri> 15 16 <tag> 17 <description> 18 這是自定義標(biāo)簽的描述信息,可以在MyEclipse中有提示 19 </description> 20 <!-- 標(biāo)簽名 --> 21 <name>outInfo</name> 22 <!-- 標(biāo)簽類的完名 --> 23 <tag-class>jxf.OutInfo</tag-class> 24 <body-content>scriptless</body-content> 25 <!-- <attribute> 26 <description> 27 這是屬性的描述信息 28 </description> 29 <name>var</name> 30 <required>false</required> 31 <rtexprvalue>false</rtexprvalue> 32 </attribute> --> 33 </tag> 34 35 </taglib>

3)在jsp頁面的頭部導(dǎo)入自定義標(biāo)簽庫

<%@taglib uri="http://jxf.tag" prefix="jxf" %>

4)在jsp中使用自定義標(biāo)簽 

<jxf:outInfo></jxf:outInfo>

5)jsp頁面

1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <%@taglib uri="http://jxf.tag" prefix="jxf" %> 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 4 <html> 5 <head> 6 <title>自定義標(biāo)簽</title> 7 </head> 8 <body> 9 <jxf:outInfo></jxf:outInfo> 10 </body> 11 </html>

自定義標(biāo)簽?zāi)軌蜃龅?#xff1a;

1)控制標(biāo)簽體內(nèi)容輸出

1 /* 2 * 1)輸出標(biāo)簽題內(nèi)容 3 */ 4 JspFragment jspBody = getJspBody(); 5 //方法:invoke(java.io.Writer out) 6 //當(dāng)參數(shù)為null時(shí)等價(jià)于 jspBody.invoke(getJspContext().getOut()); 7 jspBody.invoke(null);

2)控制標(biāo)簽余下內(nèi)容輸出

1 /* 2 * 2)控制標(biāo)簽體余下內(nèi)容是否輸出 3 * 拋出一個(gè)SkipPageException異常后,標(biāo)簽體余下內(nèi)容輸出 4 */ 5 throw new SkipPageException();

3)改變標(biāo)簽體內(nèi)容

1 /* 2 * 3)改變標(biāo)簽體內(nèi)容并輸出到瀏覽器 3 */ 4 //獲取到標(biāo)簽體內(nèi)容,并保存到自定義的款沖字符流中 5 StringWriter sw = new StringWriter(); 6 //jspBodyString得到的即是標(biāo)簽體的內(nèi)容 7 String jspBodyString = sw.toString(); 8 jspBodyString = "簡單將字符串的值改變掉"; 9 getJspContext().getOut().write(jspBodyString);//輸出到瀏覽器

4)帶屬性的標(biāo)簽(以模仿c標(biāo)簽的<c:choose>+<c:when>+<c:otherwise>標(biāo)簽為例)

編寫3個(gè)自定義標(biāo)簽處理器類 ChooseTag.javaWhen.javaOtherwise.jave

1 /* 2 * ChooseTag.java 3 */ 4 public class ChooseTag extends SimpleTagSupport { 5 private boolean flag = false; 6 7 public boolean isFlag() { 8 return flag; 9 } 10 11 public void setFlag(boolean flag) { 12 this.flag = flag; 13 } 14 @Override 15 public void doTag() throws JspException, IOException { 16 getJspBody().invoke(null); 17 } 18 } /** WhenTag.java*/ public class WhenTag extends SimpleTagSupport {private boolean test;//tld文件中的屬性必須要有對應(yīng)的set方法,否則報(bào)錯(cuò)public void setTest(boolean test) {this.test = test;}@Overridepublic void doTag() throws JspException, IOException {ChooseTag parent = (ChooseTag)getParent();if(!parent.isFlag()){if(test){parent.setFlag(true);getJspBody().invoke(null);}}} } 1 /* 2 * OtherwiseTag.java 3 */ 4 public class OtherwiseTag extends SimpleTagSupport { 5 @Override 6 public void doTag() throws JspException, IOException { 7 ChooseTag parent = (ChooseTag)getParent(); 8 if(!parent.isFlag()){ 9 getJspBody().invoke(null); 10 } 11 } 12 }

tld文件

1 <?xml version="1.0" encoding="UTF-8" ?> 2 3 <taglib xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" 6 version="2.1"> 7 8 <description>JSTL 1.1 core library</description> 9 <display-name>JSTL core</display-name> 10 <tlib-version>1.1</tlib-version> 11 <!--標(biāo)簽庫前綴 taglib指令中的prefix屬性 --> 12 <short-name>jxf</short-name> 13 <!--tld文件的唯一標(biāo)記 taglib指令中的uri屬性 --> 14 <uri>http://jxf.tag</uri> 15 16 <tag> 17 <name>chooseTag</name> 18 <tag-class>jxf.ChooseTag</tag-class> 19 <body-content>scriptless</body-content> 20 </tag> 21 <tag> 22 <description> 23 自定義的when標(biāo)簽 24 </description> 25 <name>whenTag</name> 26 <tag-class>jxf.WhenTag</tag-class> 27 <body-content>scriptless</body-content> 28 <attribute> 29 <description> 30 when標(biāo)簽屬性test 31 </description> 32 <!-- 標(biāo)簽處理器類中的必須要有對應(yīng)的set方法,否則會(huì)報(bào)錯(cuò),如此時(shí)對應(yīng)的標(biāo)簽處理器類中應(yīng)有g(shù)etTest()方法。 --> 33 <name>test</name> 34 <!-- 該屬性是否是必須的 ,true為必須的,false為非必須的--> 35 <required>true</required> 36 <!-- 該屬性是否支持EL表達(dá)式 true支持,false不支持 --> 37 <rtexprvalue>true</rtexprvalue> 38 </attribute> 39 </tag> 40 <tag> 41 <name>otherwiseTag</name> 42 <tag-class>jxf.OtherwiseTag</tag-class> 43 <body-content>scriptless</body-content> 44 </tag> 45 </taglib>

jsp代碼

1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <%@taglib uri="http://jxf.tag" prefix="jxf"%> 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 4 <html> 5 <head> 6 <title>模仿c標(biāo)簽庫的choose,when,otherwise</title> 7 </head> 8 <body> 9 <jxf:demoTag>標(biāo)簽體內(nèi)容</jxf:demoTag> 10 標(biāo)簽體余下內(nèi)容 11 <br /> 12 <jxf:chooseTag> 13 <jxf:whenTag test="${4>5}"> 14 4>5是不輸出的 15 </jxf:whenTag> 16 <jxf:whenTag test="${10>5}"> 17 10>5是成立的 18 </jxf:whenTag> 19 <jxf:whenTag test="${9>5}"> 20 9>5是成立的,但是不輸出 21 </jxf:whenTag> 22 <jxf:otherwiseTag> 23 當(dāng)條件都不成立時(shí)候輸出(由于第二個(gè)提條件成立,故不應(yīng)輸出) 24 </jxf:otherwiseTag> 25 </jxf:chooseTag> 26 </body> 27 </html>

?

轉(zhuǎn)載于:https://www.cnblogs.com/fnz0/p/5662038.html

總結(jié)

以上是生活随笔為你收集整理的学会怎样使用Jsp 内置标签、jstl标签库及自定义标签的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。