struts2 自訂標籤 執行個體

來源:互聯網
上載者:User
(1) 其實,開發自訂標籤並不需要Struts2的支援,一般情況下,只需要繼承 javax.servlet.jsp.tagext.BodyTagSupport類,重寫doStartTag,doEndTag等方法即可。 在struts2.x中實現自訂標籤時,繼承的2個類分別是org.apache.struts2.views.jsp.ComponentTagSupport   和 org.apache.struts2.components.Component. ComponentTagSupport: 實際上是對BodyTagSupport的一次封裝,繼承ComponentTagSupport類是為了獲得JSP頁面中使用者自訂的標籤中設定的屬性值,並封裝成Component對象。 Component:
繼承Component類是為了從Struts2中的ValueStack中獲得相對應的值。 (2)分析: tag類裡面,
須聲明的屬性:
需要定義你JSP頁面傳來的標籤設定的屬性。如maxlength,並相應的set get。 須的兩個方法是:
1.
public Component getBean(ValueStack stack, HttpServletRequest req, HttpServletResponse res);
此方法就是獲得一個基本類的對象。在基本類裡面需要有傳入ValueStack的建構函式,如果基本類邏輯裡面需要request或者response,那麼需要有傳入ValueStack stack, HttpServletRequest req, HttpServletResponse res的建構函式;
2.
protected void populateParams()此方法是用來將JSP頁面傳來的屬性值賦給基本類。這裡是繼承來的,所以首先調用super的相應方法。我理解上面第一個方法返回的對象就是全域的一個Component對象,也就是第二個方法裡面使用的對象。 (3)
實現過程分兩步:
第一步,產生基本類,且將ValueStack值,還有其它可選值賦給它。
第二步,JSP頁面傳來的屬性值,通過ComponentTagSupport,利用其getter,setter來擷取.將JSP頁面傳來的屬性值,賦給基本類執行個體。
執行個體:
(1)
頁面:
<%@ taglib prefix="jason" uri="/WEB-INF/tlds/time.tld" %>
<jason:jtime who="hwj" message="你好"/> (2)
JtimeTag要定義tld檔案中定義的各對應屬性. public class JtimeTag extends ComponentTagSupport {
private String message;
private String who; public String getMessage() {
   return message;
} public void setMessage(String message) {
   this.message = message;
} public String getWho() {
   return who;
} public void setWho(String who) {
   this.who = who;
}
public Component getBean(ValueStack arg0, HttpServletRequest arg1,
    HttpServletResponse arg2) {
   return new JtimeComponent(arg0);
} protected void populateParams() {
   super.populateParams();
   JtimeComponent jtime = (JtimeComponent) component;
   jtime.setWho(who);
   jtime.setMessage(message);
}
} (3)
JtimeTag要定義tld檔案中定義的各對應屬性.因為JtimeTag要賦值給它.:)
public class JtimeComponent extends Component {

private String message; private String who;

private String a; public String getMessage() {
   return message;
} public void setMessage(String message) {
   this.message = message;
} public String getWho() {
   return who;
} public void setWho(String who) {
   this.who = who;
} public JtimeComponent(ValueStack arg0) {
   super(arg0);
} //將自己需要輸出的邏輯通過writer輸出字串就可以了。
public boolean start(Writer writer) {
   boolean result = super.start(writer);
   try {
    a=this.getStack().findString("pageSize");
     writer.write(toHTML(who,message));
   } catch (IOException ex) {
   }
  
   return result;
}

protected String toHTML(String who,String message) {
   Date date=new Date();
   return who+message+" 當前的時間是:"+date+"頁面數:"+a;
}
} (4)
<taglib>   
    <tlib-version>2.2.3</tlib-version>   
    <jsp-version>1.2</jsp-version>   
    <short-name>my</short-name>   
    <uri>/jtime</uri> -無起作用    
    <display-name>"jason time tab"</display-name>   
        <tag>   
            <name>jtime</name>   
            <tag-class>com.coship.dhm.dm.common.taglib.JtimeTag</tag-class>    
            <body-content>empty</body-content>       
            <attribute>   
                <name>message</name>   
                <required>false</required>   
                <rtexprvalue>true</rtexprvalue>   
            </attribute>
            <attribute>   
                <name>who</name>   
                <required>true</required>   
                <rtexprvalue>true</rtexprvalue>   
            </attribute>
        </tag>   
</taglib> (5)
執行過程:
1
頁面開啟時,load入<%@ taglib prefix="jason" uri="/WEB-INF/tlds/time.tld" %>;
當程式讀到"<jason"時,會根據上面的標籤定義到time.tld-->JtimeTag.java;
2.
JtimeTag繼承ComponentTagSupport,會將JSP頁面標籤屬性進行擷取,因為此類有定義各屬性及其getter,setter --> 產生一個基本類執行個體,賦值-->將各屬性值賦給基本類執行個體.
3.
基本類執行個體,執行start()方法-->執行super.start()-->擷取action中的屬性-->將自訂的資訊用writer.write()輸出.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.