开发者

How generate 'back to search results' url by JSTL or custom lib in a JSP with all parameters and specify exclude parameters?

开发者 https://www.devze.com 2023-02-11 15:42 出处:网络
I want to generate a \'back to search result\' url in a JSP. I use JSTL taglib and it works: <c:url var=\"backUrl\" value=\"list.html\">

I want to generate a 'back to search result' url in a JSP. I use JSTL taglib and it works:

<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>

I want to have a better solution, like a custom lib to generate the back link with one line code, by example:

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

Because, I want to re-use this code with an exclude param:

<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>

My question is what is the best practice and how can I create a custom lib to generate link by one line:

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

Thank you.


Ok, I have created a custom tab:

Class:

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>

And in the jsp:

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

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

精彩评论

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