gpt4 book ai didi

java - 如何通过 JSTL 或 JSP 中的自定义库生成包含所有参数的 'back to search results' url 并指定排除参数?

转载 作者:行者123 更新时间:2023-12-01 05:49:52 25 4
gpt4 key购买 nike

我想在 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"/>

谢谢。

最佳答案

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

类(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;
}
}

顶级域名:

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

关于java - 如何通过 JSTL 或 JSP 中的自定义库生成包含所有参数的 'back to search results' url 并指定排除参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5001422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com