如何在具有所有参数的JSP中使用JSTL或自定义lib生成“返回搜索结果”URL并指定排除参数?

时间:2011-02-15 08:45:06

标签: java jsp jstl taglib

我想在JSP中生成一个'返回搜索结果'网址。我使用JSTL taglib并且它可以工作:

<c:url var="backUrl" value="list.html">
    <c:forEach items="${param}" var="currentParam">
        <c:param name="${currentParam.key}" value="${currentParam.value}"/>
    </c:forEach>
</c:url>
<a href="${backUrl}">&lt; return to search results / list</a>

我希望有一个更好的解决方案,例如自定义lib,以生成带有一个行代码的反向链接,例如:

<tagname:url var="backUrl" value="list.html" includeAllParams="true" excludeParams="id, question"/>
<a href="${backUrl}">&lt; return to search results / list</a>

因为,我想在排除参数中重复使用此代码:

<c:url var="backUrl" value="list.html">    
    <c:forEach items="${param}" var="currentParam">
        <c:if test="${currentParam.key ne 'id'}">
            <c:param name="${currentParam.key}" value="${currentParam.value}"/>
        </c:if>
    </c:forEach>
</c:url>
<a href="${backUrl}">&lt; return to search results / list</a>

我的问题是什么是最佳做法,如何创建自定义库以通过一行生成链接:

<tagname:url var="backUrl" value="list.html" includeAllParams="true" excludeParams="id, question"/>

谢谢。

1 个答案:

答案 0 :(得分:0)

好的,我创建了一个自定义标签:

类别:

package com.test;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;    
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;    
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
import org.apache.taglibs.standard.tag.el.core.UrlTag;

public class CustomUrlTag extends UrlTag {

    private static final long serialVersionUID = 1L;    
    private String includeAllParams;    
    private String excludeParams;

    public CustomUrlTag() {
        init();
    }

    @Override
    public int doStartTag() throws JspException {
        return super.doStartTag();
    }

    @Override
    public int doEndTag() throws JspException {

        if (isIncludeAllParam()) {

          final HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();

          // iterate over request parameters
          final List<String> requestParameterNames = Collections.list((Enumeration<String>) request.getParameterNames());

          // exclude parameters
          if (null != excludeParams) {
              for (final String currentExcludeParam : excludeParams.split("[,]")) {
                  requestParameterNames.remove(currentExcludeParam);
              }
          }

          for (final String parameterName : requestParameterNames) {
              // add parameters
              addParameter(parameterName, request.getParameter(parameterName));
          }
        }
        return super.doEndTag();
    }

    @Override
    public void release() {
        super.release();
        init();
    }

    private void init() {
        includeAllParams = null;
    }

    private boolean isIncludeAllParam() throws JspException {
        final Object r = ExpressionEvaluatorManager.evaluate("includeAllParams", includeAllParams,
                java.lang.Boolean.class, this, pageContext);
        if (r != null) {
            return ((Boolean) r).booleanValue();
        }
        return false;
    }

    public void setIncludeAllParams(String includeAllParams) {
        this.includeAllParams = includeAllParams;
    }

    public void setExcludeParams(String excludeParams) {
        this.excludeParams = excludeParams;
    }
}

TLD:

<tag>
        <description>
            Creates a URL with optional query parameters.
        </description>
        <name>customUrl</name>
        <tag-class>com.test.CustomUrlTag</tag-class>
        <body-content>JSP</body-content>
        <attribute>
            <description>
                Name of the exported scoped variable for the
                processed url. The type of the scoped variable is
                String.
            </description>
            <name>var</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <description>URL to be processed.</description>
            <name>value</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>    
        <attribute>
            <description></description>
            <name>includeAllParams</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>    
        <attribute>
            <description></description>
            <name>excludeParams</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>

在jsp中:

<%@ taglib prefix="thePrefix" uri="http://www.test.com/project/tags" %>

<thePrefix:customUrl var="backUrl" value="list.html" includeAllParams="true" excludeParams="id"/>