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

查看:18
本文介绍了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.

推荐答案

多年来我一直想要这个功能,再次谷歌搜索后,我写了自己的.我认为 tag/jsp 文件 &自定义标签类很棒,但对于像你描述的简单的一次性标签来说通常是多余的.

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天全站免登陆