开发者

Passing non-string attribute to custom JSTL tag

开发者 https://www.devze.com 2023-01-22 22:24 出处:网络
is it possible to create a custom JSTL tag that accepts a non-string attribute? I would like to create a tag that would be handle pagination using PagedListHolder from Spring MVC.

is it possible to create a custom JSTL tag that accepts a non-string attribute?

I would like to create a tag that would be handle pagination using PagedListHolder from Spring MVC.

<%@tag body-content="scriptless" description="basic page template" pageEncoding="UTF-8"%>

<%-- The list of normal or fragment attributes can be specified here: --%>
<%@attribute name="pagedList" required="true" %>

<%-- any content can be specified here e.g.: --%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:choose>
    <c:when test="${!pagedList.firstPage}"&开发者_Python百科gt;
        <a href="
           <c:url value="pagedList">
               <c:param name="page" value="${pagedList.page - 1}"/>
           </c:url>
           ">&lt;&lt;
        </a>
    </c:when>
    <c:otherwise>
        &lt;&lt;
    </c:otherwise>
</c:choose>

<%-- ...more --%>

This tag would require an instance of PagedListHolder class.

Sort of like this, but I realize this is not valid.

<templ:pagedList pagedList="${players}"/>

Do I need to write a tag handler to achieve this?


You can just specify the type attribute on the attribute directive.

<%@ attribute name="pagedList" required="true" type="org.springframework.beans.support.PagedListHolder" %>


In short: JSTL tags are allowed to have non-string attributes, as you're using spring mvc, your tag class could look like this:

public class PagedListTag extends RequestContextAwareTag {

    private PagedListHolder pagedList;

    @Override
    protected int doStartTagInternal() throws Exception {
        // do something with pagedList
        return SKIP_BODY;
    }

    @Override
    public void doFinally() {
        this.pagedList = null;
        super.doFinally();
    }

    public void setPagedListed(PagedListHolder pagedList) {
        this.pagedList = pagedList;
    }
}

The only thing you have to do is to register it properly with the pagedList attribute in your .tld file:

...
<tag>
    <name>pagedList</name>
    <tag-class>PagedListTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <name>pagedList</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
</tag>
...
0

精彩评论

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

关注公众号