开发者

How to override <struts:text name="..."/> (Struts 2 Text jsp tag) to print out the name attribute in an HTML comment?

When you use a <struts:text name="..."/> Struts 2 Text jsp tag, you get a text from a .properties file in the page.

For example, if the .properties-file contains

foo.bar = This is the text.

The jsp tag <struts:text name="foo.bar"/> will output

This is the text.

No开发者_如何学编程w i would like for all instances of <struts:text name="..."> in the webapp to also output the name attribute inside an HTML comment, like so:

<!-- foo.bar -->This is the text.

I tried creating my own jsp tag file (to use instead of <struts:text>) that contained

<%@attribute name="name" required="true"%>

<!-- ${name} --><struts:text name="${name}"/>

but it doesn't work because <rtexprvalue> is set to false for the name attribute of the struts:text tag.

Any ideas how this can be accomplished?


# PROPERTIES
copyright.year=2011

(B) : Override default theme (is acceptable)

label.ftl

Add following code <!-- ${parameters.name?html} -->

<!-- ${parameters.name?html} -->

<label<#rt/>
...
</label>

.jsp

<s:label key="copyright.year" />

HTML output :

2011

HTML source :

<!-- copyright.year -->
<label id="form_name_copyright_year">2011</label>

OR (not recommend)

label.ftl with only 4 lines of code

<#if parameters.nameValue??>
<!-- ${parameters.name?html} --><#rt/>
<@s.property value="parameters.nameValue"/><#t/>
</#if>

HTML source :

<!-- copyright.year -->2011

  (A)  

/**
 * Overridden/replacement for {@link ActionSupport}.
 */
public class MyActionSupport extends ActionSupport {

    public String text(String fieldName) {
        return "<!-- " + fieldName + " --> " + getText(fieldName);
    }
}


public class MyAction extends MyActionSupport {

}

.jsp

<s:property value="text('copyright.year')" escapeHtml="false" />

HTML output :

2011

HTML source :

<!-- copyright.year --> 2011
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜