Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the URL of the current page in Grails

Tags:

grails

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?

like image 348
knorv Avatar asked Sep 09 '09 16:09

knorv


4 Answers

The answer is request.forwardURI (details here).

like image 94
knorv Avatar answered Oct 20 '22 00:10

knorv


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();
}
like image 33
Pablo Moretti Avatar answered Oct 20 '22 00:10

Pablo Moretti


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
like image 4
Carlos André Oliveira Avatar answered Oct 20 '22 00:10

Carlos André Oliveira


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>
like image 3
John Wagenleitner Avatar answered Oct 19 '22 22:10

John Wagenleitner