2011-09-20 34 views
2

我有一個Struts操作類,它將自定義JSP標記的標記設置爲String作爲請求屬性。動作類將其轉發給一個JSP頁面,該頁面包含打印請求屬性的另一個標籤。但是,自定義JSP標記未被解析並顯示爲純文本。下面顯示JSP如何呈現它:如何評估存儲在字符串中的JSP標記?

<%@ taglib uri="/tld/CdrReconTags.tld" prefix="reconTags" %> 

<reconTags:renderHTML> 
    <form id=F_3_2> 

    <table align='center' width='100%' style='border:1px solid black;' cellpadding='0' cellspacing='0'> 
     <tr> 
     <td colspan='2'>&nbsp;</td> 
     </tr> 
     <tr> 
     <td align='center'> 
      <div class='label'> 
      <strong style='white-space: nowrap;'>STARTDATE :&nbsp;</strong> 
      </div> 
     </td> 
     <td> 
      <div class='label'> 
      <strong style='white-space: nowrap;'> 
       <reconTags:reportDatesDropDown id="STARTDATE_3_3" />&nbsp; 
       <span style='color:red;font-weight: bold; font-size: 20px;'>*</span> 
      </strong> 
      </div> 
     </td> 
     <td align='center'> 
      <div class='label'> 
      <strong style='white-space: nowrap;'>ENDDATE :&nbsp;</strong> 
      </div> 
     </td> 
     <td> 
      <div class='label'> 
      <strong style='white-space: nowrap;'> 
</reconTags:renderHTML> 

請注意未解析的自定義JSP標記<reconTags:reportDatesDropDown id="STARTDATE_3_3" />。我如何讓JSP評估它?以下代碼是<reconTags:renderHTML>的標記處理程序,不會評估正文,如上面的輸出中所示。

public class DynamicHTMLRendererTagHandler extends BodyTagSupport 
{ 

    private static final long serialVersionUID = 6457283471933854138L; 

    public int doStartTag() throws JspException 
    { 
     return EVAL_BODY_BUFFERED; 
    } 

    public int doAfterBody() throws JspException 
    { 
     /* Grab the body content */ 
     BodyContent body = this.getBodyContent(); 

     try 
     { 
      body.writeOut(body.getEnclosingWriter()); 
     } catch (IOException e) 
     { 
       throw new JspTagException(e.toString());  
     } 
     return SKIP_BODY; 
    } 
} 

回答

2

是reconTag應與最初的代碼本身,而不是被添加爲一個字符串輸出...

請注意,所謂JSP做的是:

1 - 解析文檔爲標籤。

2 - 填充文檔請求的Java輸出。

由於此調用僅在解釋標籤後完成,因此這些標籤以純文本出現是正常的。

如果你想添加一些動態標籤到你的文檔中,你將不得不找出一種方法,在解析之前使用這些標籤來構建文檔......然而,這可能是一個巨大的頭痛,如果不是不可能的話。

+0

Gonçalo,Balusc:謝謝你的建議 –