如何从 URL 查询字符串中删除以前的请求参数

How to remove previous request parameter from URL query string

提问人:Vo Hong Thai 提问时间:10/31/2023 最后编辑:BalusCVo Hong Thai 更新时间:10/31/2023 访问量:28

问:

当我点击一个类别时,它会显示该类别的产品列表,URL 将是:

http://localhost:9999/toyshop/home?category=Đồ%20chơi%20theo%20phim

但是当我单击下一个分页页面时,URL 将是

http://localhost:9999/toyshop/home?category{Đồ%20chơi%20theo%20phim}=Đồ%20chơi%20theo%20phim&page=2

这是我的jsp代码:

<c:remove var="param" scope="session" />
    <c:remove var="paramValue" scope="session" />
    <c:remove var="href" scope="session" />

    <c:remove var="param" scope="request" />
    <c:remove var="paramValue" scope="request" />
    <c:remove var="href" scope="request" />
    <c:choose>
        <c:when test="${not empty sessionScope.productName}">
            <c:set var="servlet" value="search"/>
            <c:set var="param" value="product"/>
            <c:set var="paramValue" value="${sessionScope.productName}"/>
        </c:when>
        <c:when test="${not empty sessionScope.categoryName}">
            <c:set var="servlet" value="home"/>
            <c:set var="param" value="category"/>
            <c:set var="paramValue" value="${sessionScope.categoryName}"/>
        </c:when>
        <c:otherwise>
            <!-- Handle the case where both are empty -->
            <c:set var="servlet" value="home"/>
            <c:set var="param" value=""/>
            <c:set var="paramValue" value=""/>
        </c:otherwise>
    </c:choose>

    <c:choose>
        <c:when test="${empty paramValue}">
            <c:set var="href" value="${servlet}?"/>
        </c:when>
        <c:otherwise>
            <c:set var="href" value="${servlet}?${param}=${paramValue}"/>
        </c:otherwise>
    </c:choose>


    <c:if test="${currentPaginationPage > 1}">
        <a href="${href}&page=${currentPaginationPage - 1}" class="pagination__item">
            <i class="pagination__icon fas fa-chevron-left"></i>
        </a>
    </c:if>

    <c:if test="${startPage > 1}">
        <div class="pagination__item pagination__item--none">...</div>
    </c:if>

    <c:forEach begin="${startPage}" end="${endPage}" var="page">
        <c:choose>
            <c:when test="${page == currentPaginationPage}">
                <a href="${href}&page=${page}" class="pagination__item pagination__item--active">${page}</a>
            </c:when>
            <c:otherwise>
                <a href="${href}&page=${page}" class="pagination__item">${page}</a>
            </c:otherwise>
        </c:choose>
    </c:forEach>

    <c:if test="${endPage < totalPaginationPage}">
        <div class="pagination__item pagination__item--none">...</div>
    </c:if>

    <c:if test="${currentPaginationPage < totalPaginationPage}">
        <a href="${href}&page=${currentPaginationPage + 1}" class="pagination__item">
            <i class="pagination__icon fas fa-chevron-right"></i>
        </a>
    </c:if>

我希望网址会是

http://localhost:9999/toyshop/home?category=Đồ%20chơi%20theo%20phim&page=2
JSP EL公司

评论

0赞 Roman C 10/31/2023
如果您在同一页面上同时拥有产品和类别,则不清楚您尝试分页的内容。此外,如果再次设置作用域变量,则无需删除该变量。

答:

0赞 BalusC 10/31/2023 #1

是 EL 中的保留变量。它基本上是 的隐式对象外观。您不应尝试删除/覆盖它。${param}HttpServletRequest#getParameter()

使用与 不同的名称。例如。${param}${paramName}

<c:set var="paramName" value="category"/>
<c:set var="paramValue" value="${sessionScope.categoryName}"/>
<c:set var="href" value="${servlet}?${paramName}=${paramValue}"/>

另请参阅: