如何评估存储在字符串中的 JSP 标记?

发布于 2024-12-05 10:15:14 字数 2317 浏览 1 评论 0原文

我有一个 Struts 操作类,它使用自定义 JSP 标记的标记设置 String 作为请求属性。操作类将其转发到 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 标记 。我怎样才能让JSP评估它呢?以下代码是 的标记处理程序,不会评估正文,如上面的输出所示。

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;
    }
}

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'> </td>
      </tr>
      <tr>
        <td align='center'>
          <div class='label'>
            <strong style='white-space: nowrap;'>STARTDATE : </strong>
          </div>
        </td>
        <td>
          <div class='label'>
            <strong style='white-space: nowrap;'>
              <reconTags:reportDatesDropDown id="STARTDATE_3_3" /> 
              <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 : </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 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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事与诗 2024-12-12 10:15:14

reconTag 应该与初始代码本身一起出现,而不是作为字符串输出添加...

请注意,JSP 的作用是:

1 - 解析文档中的标签。

2 - 填写文档请求的 Java 输出。

由于此调用仅在解释标签后完成,因此这些标签以纯文本形式显示是正常的。

如果您想在文档中添加某种动态标签,则必须在解析文档之前找到一种使用这些标签构建文档的方法...但是,如果不是的话,这可能会非常令人头疼不可能的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文