如何生成“返回搜索结果” url 通过 JSTL 或 JSP 中的自定义 lib 包含所有参数并指定排除参数?

发布于 2024-10-17 14:00:50 字数 1278 浏览 2 评论 0原文

我想在 JSP 中生成“返回搜索结果”URL。我使用 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>

我想要一个更好的解决方案,例如自定义库来使用一行代码生成反向链接,例如:

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

谢谢。

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}">< 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}">< 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}">< 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莫多说 2024-10-24 14:00:50

好的,我创建了一个自定义选项卡:

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>

并在 jsp 中:

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

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

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"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文