JSP:制作可重用的代码(标签,宏)并在同一页面上使用 [英] JSP: Make a reusable code (tag, macro) and use it on the same page

查看:176
本文介绍了JSP:制作可重用的代码(标签,宏)并在同一页面上使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在一个JSP页面上创建某种参数化的宏,并在同一页面上重复使用几次.可以使用JSP标记,但是每个标记我必须制作一个文件.

Is there any way to make some sort of parametrized macro on one JSP page and reuse it few times on the same page. JSP tags could be used but I would have to make one file per tag.

推荐答案

多年以来,我一直都希望使用此功能,在再次进行谷歌搜索之后,我编写了自己的功能.我认为标签/jsp文件&自定义标记类很棒,但对于您所描述的简单一次性而言,通常过于矫kill过正.

I've been wanting this functionality for years, and after googling yet again, I wrote my own. I think tag / jsp files & custom tag classes are great, but are often overkill for simple one-offs like you describe.

这是我新的宏"标签现在起作用的方式(这里用于可排序表标题的简单html呈现):

This is how my new "macro" tag now works (here used for simple html rendering of sortable table headers):

<%@ taglib prefix="tt" uri="/WEB-INF/tld/tags.tld" %>

<!-- define a macro to render a sortable header -->
<tt:macro id="sortable">
    <th class="sortable">${headerName}
        <span class="asc" >&uarr;</span>
        <span class="desc">&darr;</span>
    </th>
</tt:macro>

<table><thead><tr>
    <!-- use the macro for named headers -->
    <tt:macro id="sortable" headerName="Name (this is sortable)" />
    <tt:macro id="sortable" headerName="Age (this is sortable)" />
    <th>Sex (not sortable)</th>
    <!-- etc, etc -->

在/WEB-INF/tld/tags.tld中,我添加了:

In /WEB-INF/tld/tags.tld, I added:

<tag>
    <name>macro</name>
    <tag-class>com.acme.web.taglib.MacroTag</tag-class>
    <body-content>scriptless</body-content>
    <attribute>
        <description>ID of macro to call or define</description>
        <name>id</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <dynamic-attributes>true</dynamic-attributes>
</tag>

最后是Java标记类:

And, finally, the Java tag class:

public class MacroTag
    extends SimpleTagSupport implements DynamicAttributes
{
    public static final String PREFIX = "MacroTag_";
    private boolean bodyless = true;
    private String id;
    private Map<String, Object> attributes = new HashMap<String, Object>();

    @Override public void setJspBody(JspFragment jspFragment) {
        super.setJspBody(jspFragment);
        getJspContext().setAttribute(PREFIX + id, jspFragment, PageContext.REQUEST_SCOPE);
        bodyless = false;
    }

    @Override public void doTag() throws JspException, IOException {
        if (bodyless) {
            JspFragment jspFragment = (JspFragment) getJspContext().getAttribute(PREFIX + id, PageContext.REQUEST_SCOPE);
            JspContext ctx = jspFragment.getJspContext();
            for (String key : attributes.keySet())
                ctx.setAttribute(key, attributes.get(key));
            jspFragment.invoke(getJspContext().getOut());
            for (String key : attributes.keySet()) {
                ctx.removeAttribute(key);
            }
        }
    }

    public void setId(String id) {
        this.id = id;
    }
    @Override public void setDynamicAttribute(String uri, String key, Object val) throws JspException {
        attributes.put(key, val);
    }
}

实现是非常基本的.如果标记具有主体,则假定我们正在定义一个宏,然后存储该JspFragment.否则,我们假设正在调用宏,因此我们将对其进行查找,然后将任何动态属性复制到其上下文中,以便对其进行适当的参数化,然后将其呈现到调用输出流中.

The implementation is pretty basic. If the tag has a body, we assume we're defining a macro, and we store that JspFragment. Otherwise, we assume we're calling a macro, so we look it up, and copy any dynamic attributes into its context so it be properly parameterized, and render it into the calling output stream.

疯了,这不是JSP内置的.

Crazy this isn't built into JSP.

这篇关于JSP:制作可重用的代码(标签,宏)并在同一页面上使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆