使用 app.context 时 Grails 重定向到错误的地址?
当我以某种方式设置 app.context
时,我注意到 redirect
出现了一些奇怪的行为。我在 Grails JIRA 中发现了一个错误,它完美地描述了我的问题,但它被标记为 UTR: http://jira.grails .org/browse/GRAILS-7546
这是我对问题的描述:
我目前正在使用 Grails 2.0M2。我在 application.properties 文件中定义了以下属性:
app.context=/
app.name=foobar
当我在控制器中调用 redirect
时,redirect
会将应用程序名称添加到我提供的 uri 上,这会导致a 404。我是这样做的:
String partialUrl = createLink(mapping: 'helloworld') // returns `/hello/world`
redirect(uri: partialUrl) // INCORRECTLY redirects to
// `http://mysite.com/foobar/hello/world`
// instead of `http://mysite.com/hello/world`
假设我在 UrlMappings.groovy
文件中定义了一个名为 helloworld
的 URL 映射,路径为/hello/world
。
因此,长话短说,如果我将 app.context
设置为 /
,我不会期望 app.name 显示在我的最终重定向 URL 中。
这是错误还是预期行为?关于无需执行太多手动步骤即可构建重定向 URL 的最简单方法有什么想法吗?
I'm noticing some odd behavior with redirect
when I set my app.context
a certain way. I found a bug in the Grails JIRA which describes my problem perfectly, but it was marked as UTR: http://jira.grails.org/browse/GRAILS-7546
Here is my description of the problem:
I'm currently using Grails 2.0M2. I have the following properties defined in my application.properties file:
app.context=/
app.name=foobar
When I call redirect
in a controller, redirect
is adding the app name onto the uri I provide, which then causes a 404. Here is how I'm doing this:
String partialUrl = createLink(mapping: 'helloworld') // returns `/hello/world`
redirect(uri: partialUrl) // INCORRECTLY redirects to
// `http://mysite.com/foobar/hello/world`
// instead of `http://mysite.com/hello/world`
Assume that I have a URL mapping named helloworld
defined in my UrlMappings.groovy
file with a path of /hello/world
.
So, long story short, if I set the app.context
to /
, I would NOT expect the app.name
to show up in my final redirect URL.
Is this a bug or expected behavior? Any idea on the easiest way I could build up the redirect URL without doing too many manual steps?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不想这么说,但我也无法复制它。我创建了一个带有一个控制器(名为 Hello)的测试项目,使用您的代码创建一个除了重定向之外不执行任何操作的操作:
HelloController.groovy
我创建了一个索引页面,index.gsp 在views/hello中
index.gsp
在中设置helloworld UrlMappings 映射到 hello 控制器的索引操作:
UrlMappings.groovy
并将 application.properties 更改为 app.context=/
application.properties
当我运行应用程序时,我可以转到localhost:8080/hello 它会显示我的简单 GSP。我尝试了 localhost:8080/helloworld 并且也按照映射的预期得到了它。然后我尝试了 localhost:8080/hello/redir,我被正确重定向到 localhost:8080/helloworld。
但是,如果您仍然面临这个问题,我有一些建议
1) 尝试使用 2.0 中提供的新链接生成器而不是 createLink。它可能不会做任何不同的事情,但值得一试:
grailsLinkGenerator.link(mapping: 'helloworld')
2) 如果仅在控制器内进行重定向,您可以将 http://mysite.com 部分自行添加到部分网址。
3) 最后的手段,编写一个附加到 afterView 的过滤器,它执行正则表达式搜索并替换 mysite.com/foobar 的内容。虽然不确定这是否会捕获重定向,但如果有的话,我认为就是这样,因为过滤器可以广泛应用。
I hate to say it, but I cannot reproduce it either. I created a test project with one controller (named Hello), using your code to create an action that does nothing but redirect:
HelloController.groovy
I created an index page, index.gsp in the views/hello
index.gsp
Setup helloworld in the UrlMappings to map to the index action of the hello controller:
UrlMappings.groovy
And changed the application.properties to have the app.context=/
application.properties
When I ran the app, I could go to localhost:8080/hello and it would show my simple GSP. I tried localhost:8080/helloworld and also got it as expected per the mapping. Then I tried localhost:8080/hello/redir and I was properly redirected to localhost:8080/helloworld.
However, if you're still facing this issue, I have a few suggestions
1) Try using the new link generator available in 2.0 instead of createLink. It may not do anything different, but worth a try:
grailsLinkGenerator.link(mapping: 'helloworld')
2) If it's only on redirects from within controllers, you could just add the http://mysite.com portion yourself to the partialUrl.
3) Last resort, write a Filter attached to afterView that does a regex search and replace on the contents for mysite.com/foobar. Not sure this will catch redirects though, but if anything would, I'd assume this would be it since filters can be applied at broad level.
那对你不起作用吗?
That doesnt work for you?
这不是对你的问题的直接回答,但我的做法是:我从不修改 grails 属性中的 app.context ,因为 tomcat 在生产中覆盖它,并且我不关心它在我的开发中使用哪个上下文。
It's not a direct anwser to your question but my practice is : I never modify app.context in the grails properties since tomcat override it on production and I don't care which context it uses on my dev.