這是我使用的標籤文件。將其保存爲/WEB-INF/tags/replaceParam.tag
:
<%@ tag pageEncoding="UTF-8" trimDirectiveWhitespaces="true" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ attribute name="name" required="true" type="java.lang.String" %>
<%@ attribute name="value" required="true" type="java.lang.String" %>
<c:url value="">
<%--
replaces or adds a param to a URL
if $name in query then replace its value with $value.
copies existing
--%>
<c:forEach items="${paramValues}" var="p">
<c:choose>
<c:when test="${p.key == name}">
<c:param name="${name}" value="${value}"/>
</c:when>
<c:otherwise>
<c:forEach items="${p.value}" var="val">
<c:param name="${p.key}" value="${val}"/>
</c:forEach>
</c:otherwise>
</c:choose>
</c:forEach>
<%-- if $name not in query, then add --%>
<c:if test="${empty param[name] }">
<c:param name="${name}" value="${value}"/>
</c:if>
</c:url>
使用在另一頁(例如URL是/category/9?page=3
):
<%@ taglib prefix="my" tagdir="/WEB-INF/tags" %>
<my:replaceParam name='language' value='en' />
輸出/category/8?page=3&language=en
那你試試這麼遠嗎?你的代碼是什麼樣的?答案將完全基於意見。在沒有提供進一步信息的情況下,您的問題可能會被關閉 – GottZ
我已經用我嘗試過的東西更新了帖子,但並不知道我還能寫什麼。問題非常簡單 - 點擊一個鏈接後,我想像我寫的那樣向當前URL添加另一個參數。 –