java Grails 智能获取战争 baseUrl?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5317380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:36:41  来源:igfitidea点击:

Grails get war baseUrl intelligently?

javatomcatgrailsgroovywar

提问by Stefan Kendall

My baseUrl is http://localhost:8080/whatever. On other machines, it's http://localhost/whatever.

我的 baseUrl 是http://localhost:8080/whatever. 在其他机器上,它是http://localhost/whatever.

Is there a smart way to get this information without using

有没有一种聪明的方法来获取这些信息而不使用

<%
    String baseUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath()
%>

I'm trying to dynamically create some links to path parameterized rest URLS, i.e:

我正在尝试动态创建一些指向路径参数化其余 URLS 的链接,即:

http://localhost:8080/a/b/${param1}/c/${param2}

http://localhost:8080/a/b/${param1}/c/${param2}

If there's a better way to do this, let me know.

如果有更好的方法来做到这一点,请告诉我。

回答by Hoàng Long

In case you forget, isn't that was answered in the question here. Just set the grails.serverURLand grails.app.contextin the configuration file of your deployed machine. Then use createLinkto make the URL. Or I missed something?

万一你忘记了,这里的问题不是已经回答了。只需在部署机器的配置文件中设置grails.serverURLgrails.app.context。然后createLink用来制作网址。或者我错过了什么?

回答by sebnukem

This solution doesn't require grails.serverURL to be set:

此解决方案不需要设置 grails.serverURL:

<g:createLink absolute='true' uri='/WHATEVER'/>or

<g:createLink absolute='true' uri='/WHATEVER'/>或者

${g.createLink(absolute:true, uri:"/WHATEVER")}

${g.createLink(absolute:true, uri:"/WHATEVER")}

will generate:

将产生:

http://HOST:PORT/APPNAME/WHATEVER

http://HOST:PORT/APPNAME/WHATEVER

Set the URIto point to where you want to in the app. Use '/'for the base URL of your app:

URIto设置为您想要在应用程序中的位置。使用'/'你的应用程序的基本URL:

http://HOST:PORT/APPNAME/

http://HOST:PORT/APPNAME/

回答by Dana

the <g:link>tag accepts a parameter, "absolute=true", that will build a URL that respects the current environment's serverUrl and contextPath. I recommend combining that with a named URL mappingto build your URL.

所述<克:链路>标签接受一个参数,“绝对=真”,即将建立一个URL,它方面与当前环境的的serverUrl和contextPath中。我建议将其与命名 URL 映射结合起来构建您的 URL。

If you're unable to build your entire URL at page rendering time, Grails can still build part of the URL for you:

如果您无法在页面呈现时构建整个 URL,Grails 仍可以为您构建部分 URL:

var url = '${g.createLink(absolute: true, mapping: "mymappingname")}/' + param1 + '/c/' + param2;
console.log(url); // "http://localhost:8080/whatever/a/b/something/c/somethingelse"

And in UrlMappings.groovy:

在 UrlMappings.groovy 中:

class UrlMappings {

name mymappingname: "/a/b/$param1?/c/$param2?" {
        controller = 'myRestHandler'
    }
}

回答by user3461853

I solve using createLink(action: 'index', controller: 'Root', params: [id: id], absolute: true)

我使用 createLink(action: 'index', controller: 'Root', params: [id: id], absolute: true) 解决