Getting the URL of the current page in Grails

前端 未结 5 1099
深忆病人
深忆病人 2020-12-14 17:23

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

相关标签:
5条回答
  • 2020-12-14 17:37

    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
    
    0 讨论(0)
  • 2020-12-14 17:45

    The answer is request.forwardURI (details here).

    0 讨论(0)
  • 2020-12-14 17:45

    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>
    
    0 讨论(0)
  • 2020-12-14 17:51

    My solution (Grails 1.3.7) is this one, you can copy and paste it into your controller:

    boolean includePort = true;
    String scheme = request.getScheme();
    String serverName = request.getServerName();
    int serverPort = (new org.springframework.security.web.PortResolverImpl()).getServerPort(request)
    String contextPath = request.getContextPath();
    boolean inHttp = "http".equals(scheme.toLowerCase());
    boolean inHttps = "https".equals(scheme.toLowerCase());
    
    if (inHttp && (serverPort == 80)) {
        includePort = false;
    } else if (inHttps && (serverPort == 443)) {
        includePort = false;
    }
    String redirectUrl = scheme + "://" + serverName + ((includePort) ? (":" + serverPort) : "") + contextPath;
    

    In our application, we cannot just use g.createLink(.. absolute:true) because we have different end-user URLs because of multiple customers.

    0 讨论(0)
  • 2020-12-14 17:53

    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();
    }
    
    0 讨论(0)
提交回复
热议问题