JSP自己定義標籤入門執行個體具體解釋

來源:互聯網
上載者:User

標籤:圖例   family   pwrite   print   mil   檔案   實現   目的   hit   

JSP自己定義標籤主要能用到的兩個包

javax.servlet.jsp.*;javax.servlet.jsp.tagext.*;

自己定義標籤<userInfo:showUserInfo/>實現對使用者的展現。

例如以下


定義使用者累User.java  get set就不在贅述。

private String userName;
private Integer age;
private String email;

public User(){
this.userName="張三";
this.email="[email protected]";
this.age=28;
}


建立自己定義標籤類UserInfoTag.java

package com.test.tag;


import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;


import com.test.entity.User;


public class UserInfoTag extends TagSupport {

private User user=new User();

@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub

JspWriter out = this.pageContext.getOut();
try {
if (user == null) {
out.print("UserInfo Is Not Found......");
return SKIP_BODY;
}
out.println("<table width=‘500px‘ border=‘1‘ align=‘center‘>");
out.println("<tr>");
out.println("<td width=‘20%‘>Username:</td>");
out.println("<td>" + user.getUserName() + "</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Age:</td>");
out.println("<td>" + user.getAge() + "</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Email:</td>");
out.println("<td>" + user.getEmail() + "</td>");
out.println("</tr>");
out.println("</table>");
} catch (Exception e) {
throw new JspException(e.getMessage());
}
return super.doStartTag();
}

@Override
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
return EVAL_PAGE;
}

@Override
public void release() {
// TODO Auto-generated method stub
super.release();
this.user=null;
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

}


2在Web-Inf建立標籤庫描寫敘述檔案.tdl(Tag Library Description)

<?

xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
<tlib-version>1.0</tlib-version>
<jsp-version>3.0</jsp-version>
<short-name>userInfo</short-name>
<uri>mytaglib</uri>
<tag>
<name>showUserInfo</name>
<tag-class>com.test.tag.UserInfoTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>user</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>

3 配置web.xml 

<jsp-config>

   <taglib>

        <taglib-uri>mytaglib</taglib-uri>

        <taglib-location>/WEB-INF/mytaglib.tld</taglib-location>

    </taglib>

  </jsp-config>

4 須要使用該標籤的頁面頭部引入

<%@ taglib uri="/mytaglib" prefix="cc"%>

<userInfo:showUserInfo/>

標籤說明


我們建立的UserInfoTag類繼承了TagSupport類。而它又實現了Tag介面,Tag介面的生命週期由其所在的容器控制。例如以下:

setPageContext() 將所在jsp頁面的pageContext注入進來,目的是為了在後面的方法中能夠訪問到jsp頁面對象的pageContext屬性

setParent()       設定此標籤的父標籤

setAttribute()   將標籤中的屬性注入到此class的屬性,不須要自己實現但要提供屬性的get與set方法

doStartTag()      在開始標籤屬性設定後調用。假設返回SKIP_BODY則忽略標籤之中的內容。假設返回EVAL_BODY_INCLUDE則將標籤體的內容進行輸出

doEndTag()         在結束標籤之前調用,返回SKIP_PAGE跳過整個jsp頁面後面的輸出,返回EVAL_PAGE運行頁面餘下部分

release()          生命週期結束時調用

至此,一個簡單的自己定義標籤就實現了。

原始碼 http://download.csdn.net/detail/qiyejunlintian/8037433

JSP自己定義標籤入門執行個體具體解釋

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.