开发者

Refresh adding an extra parameter

开发者 https://www.devze.com 2023-01-27 14:37 出处:网络
I need to refresh the webpage, but in the refresh request I want to add an extra parameter, so I have though in something like:

I need to refresh the webpage, but in the refresh request I want to add an extra parameter, so I have though in something like:

<c:url value="currentUrl" var="newUrl">
    <c:param name="newParam" value="newValue"/>
</c:url>
<a href="${newUrl}">Refresh</a>

How can I get the currentUrl with the params (for instance http://localhost:8080/mywebapp?param1=var1&param2=var2) of the request from the implicit objects of the jsp. I have though in something like ${pageCont开发者_如何学运维ext.request.requestURL}, but this returns the url of the jsp, not the request url.

Thanks


If the JSP has been forwarded, you can get the original request URL by ${requestScope['javax.servlet.forward.request_uri']} and the original request query string by ${requestScope['javax.servlet.forward.query_string']}.

You can by the way find an overview of all those "hidden" forward attributes here and here.


Based on BalusC's answer

You can try these two,

${requestScope['javax.servlet.forward.request_uri']}
${requestScope['javax.servlet.forward.query_string']}

JavaScript Approach

Do you mean the URL in address bar? If that’s the case you can write a JavaScript function to get the parameters and then modify your href in question.

For example,

<a id="refresh" href="${newUrl}">Refresh</a>
...

var url = window.location.href;
url += url.split("?").length > 1 ? "&newParam=value" : "?newParam=value";
document.getElementById("refresh").href = url;

It would much shorter, if you are using jQuery or PrototypeJS like JavaScript framework.

0

精彩评论

暂无评论...
验证码 换一张
取 消