在JSP中编写URL [英] Composing URL in JSP
问题描述
可以说我当前的网址是:
/app.jsp?filter=10&sort=name
.
Lets say my current URL is:
/app.jsp?filter=10&sort=name
.
我在JSP中有一个分页组件,其中应包含类似以下内容的链接:
/app.jsp?filter=10&sort=name&page=xxx
.
I have a pagination component in JSP which should contain links like:
/app.jsp?filter=10&sort=name&page=xxx
.
如何通过在当前URL中添加新参数来在JSP中创建有效的URL?我不想在JSP中使用Java代码,也不想以如下URL结尾:
/app.jsp?filter=10&sort=name&?&page=xxx
或/app.jsp?&page=xxx
等
How do I create valid URLs in JSP by adding new parameters to current URL? I dont want use Java code in JSP, nor end up with URLs like:
/app.jsp?filter=10&sort=name&?&page=xxx
, or /app.jsp?&page=xxx
, etc.
推荐答案
好,我找到了答案.第一个问题是我必须保留URL中的所有当前参数,并且仅更改page
参数.为此,我必须遍历所有当前参数并添加我不想更改为URL的参数.然后,我添加了想要更改或添加的参数.所以我最终得到了这样的解决方案:
Ok, I found answer. First problem is that I have to preserve all current parameters in URL and change only page
parameter. To do this I have to iterate over all current parameters and add those I don't want to change to URL. Then I added parameters I want to either change or add. So I ended up with solution like this:
<c:url var="nextUrl" value="">
<c:forEach items="${param}" var="entry">
<c:if test="${entry.key != 'page'}">
<c:param name="${entry.key}" value="${entry.value}" />
</c:if>
</c:forEach>
<c:param name="page" value="${some calculation}" />
</c:url>
这将创建与请求中的page
参数无关的漂亮且干净的URL.这种方法的好处是URL可以是任何东西.
This will create nice and clean URL independent of page
parameter in request. Bonus to this approach is that URL can be just anything.
这篇关于在JSP中编写URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!