JSP分页URL查询字符串参数格式异常问题求助
问题描述
点击分类后,页面显示对应分类的产品列表,此时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
期望的正确URL格式应为:
http://localhost:9999/toyshop/home?category=Đồ%20chơi%20theo%20phim&page=2
错误原因
问题出在手动拼接URL参数的逻辑上:当paramValue包含非ASCII字符(如越南语字符)时,直接通过EL表达式拼接${param}=${paramValue}会触发异常解析,导致参数名被错误替换为category{Đồ%20chơi%20theo%20phim},而非保持category作为参数名。同时,手动拼接未做标准URL编码,进一步加剧了格式问题。
修复方案
改用JSTL的<c:url>和<c:param>标签构建URL,它们会自动处理参数编码和格式,避免手动拼接的错误。修改后的代码如下:
<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:if test="${currentPaginationPage > 1}"> <c:url var="prevPageHref" value="${servlet}"> <c:if test="${not empty param and not empty paramValue}"> <c:param name="${param}" value="${paramValue}"/> </c:if> <c:param name="page" value="${currentPaginationPage - 1}"/> </c:url> <a href="${prevPageHref}" 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:url var="pageHref" value="${servlet}"> <c:if test="${not empty param and not empty paramValue}"> <c:param name="${param}" value="${paramValue}"/> </c:if> <c:param name="page" value="${page}"/> </c:url> <c:choose> <c:when test="${page == currentPaginationPage}"> <a href="${pageHref}" class="pagination__item pagination__item--active">${page}</a> </c:when> <c:otherwise> <a href="${pageHref}" 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}"> <c:url var="nextPageHref" value="${servlet}"> <c:if test="${not empty param and not empty paramValue}"> <c:param name="${param}" value="${paramValue}"/> </c:if> <c:param name="page" value="${currentPaginationPage + 1}"/> </c:url> <a href="${nextPageHref}" class="pagination__item"> <i class="pagination__icon fas fa-chevron-right"></i> </a> </c:if>
关键修改点
- 移除手动拼接URL的逻辑,改用
<c:url>标签统一构建链接,<c:param>负责传递参数,自动完成URL编码和格式校验。 - 每个分页链接单独通过
<c:url>生成,确保分类参数和分页参数都能正确传递且格式符合HTTP标准。
内容的提问来源于stack exchange,提问作者Vo Hong Thai
相关产品推荐
相关产品推荐

