开发者

How to evaluate a JSP tag which is stored in a String?

I have a Struts action class that sets a String with the markup of a custom JSP tag as request attribute. The action class forwards this to a JSP page, which contains another tag wherein the request attribute is printed. However, the custom JSP tag is not parsed and is displayed as plain text. The following shows how the JSP has rendered it:

<%@ 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>

Note the unparsed custom JSP tag <reconTags:reportDatesDropDown id="STARTDATE_3_3" />. How can I let JSP evaluate it? The following code is the taghandler for <reconTags:renderHTML> and does not evaluate the body, as shown in the output above.

public class DynamicHTMLRendererTagHandler extends BodyTagSupport 
{

    private static final long serialVersionUID = 6457283471933854138L;

    public int doStartTag() throws JspException 
    {
        return EVA开发者_StackOverflow中文版L_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;
    }
}


that reconTag should be with the initial code itself, and not by being added as a String output...

Do Note that what JSP does is:

1 - parse the document for tags.

2 - fills up with the Java outputs that are requested by the document.

Since this call is only done AFTER the tags are interpreted it's normal for these tags to come out as plain text.

If you want to add some sort of dynamic tags to your document you'll have to figure out a way of building the document with these tags in place prior to having it parsed... this however, might be a huge headache, if not impossible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜