In a Grails application I'd like to send a user from page A, then to a form on page B and then back to page A again.
To keep track of which URL to return to I send a "returnPage" parameter to page B containing the URL of page to return to (page A).
Currently I'm using request.getRequestURL() on page A to retrieve the page's URL. However, the URL I get from getRequestURL() does not correspond to the URL the end-user has in his/hers address bar:
request.getRequestURL() == "http://localhost:8080/something/WEB-INF/grails-app/views/layouts/main.gsp"
URL in address bar == "http://localhost:8080/something/some/thing"
How do I obtain the "end-user" URL?
The answer is request.forwardURI
(details here).
I built this method to get current url.
static String getCurrentUrl(HttpServletRequest request){
StringBuilder sb = new StringBuilder()
sb << request.getRequestURL().substring(0,request.getRequestURL().indexOf("/", 8))
sb << request.getAttribute("javax.servlet.forward.request_uri")
if(request.getAttribute("javax.servlet.forward.query_string")){
sb << "?"
sb << request.getAttribute("javax.servlet.forward.query_string")
}
return sb.toString();
}
I prefer to use:
createLink(action: "index", controller:"user", absolute: true)
// http://localhost:8080/project/user
when I need to get an absolute url!
It's interesting to get relative path too:
createLink(action: "index", controller:"user")
// /project/user
When creating the link to page B you can use the createLink tag to set the returnPage parameter:
<g:link controller="pageB"
action="someaction"
params='[returnPage:createLink(action:actionName, params:params)]'>
Go to Page B
</g:link>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With