使用 app.context 时 Grails 重定向到错误的地址?

发布于 2024-12-13 12:15:16 字数 1110 浏览 2 评论 0原文

当我以某种方式设置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

街角迷惘 2024-12-20 12:15:16

我不想这么说,但我也无法复制它。我创建了一个带有一个控制器(名为 Hello)的测试项目,使用您的代码创建一个除了重定向之外不执行任何操作的操作:

HelloController.groovy

package test1

class HelloController {
   def index() {
      def model = [:]
      model.content = 'content...'
      model
   }

   def redir() {
      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`
   }
}

我创建了一个索引页面,index.gsp 在views/hello中

index.gsp

<h1>index.gsp</h1>
<html>
   <body>
      <p>This data is coming from the model:</p>
      <p>content: ${content}</p>
   </body>
</html>

在中设置helloworld UrlMappings 映射到 hello 控制器的索引操作:

UrlMappings.groovy

class UrlMappings {
    static mappings = {
        "/helloworld"(controller: "hello", action: "index")

        "/$controller/$action?/$id?"{
            constraints {
                // apply constraints here
            }
        }

        "/"(view:"/index")
        "500"(view:'/error')
    }
}

并将 application.properties 更改为 app.context=/

application.properties

#Grails Metadata file
#Sun Nov 06 14:51:56 EST 2011
app.grails.version=2.0.0.RC1
app.context=/
app.name=test1
app.servlet.version=2.5
app.version=0.1

当我运行应用程序时,我可以转到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

package test1

class HelloController {
   def index() {
      def model = [:]
      model.content = 'content...'
      model
   }

   def redir() {
      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`
   }
}

I created an index page, index.gsp in the views/hello

index.gsp

<h1>index.gsp</h1>
<html>
   <body>
      <p>This data is coming from the model:</p>
      <p>content: ${content}</p>
   </body>
</html>

Setup helloworld in the UrlMappings to map to the index action of the hello controller:

UrlMappings.groovy

class UrlMappings {
    static mappings = {
        "/helloworld"(controller: "hello", action: "index")

        "/$controller/$action?/$id?"{
            constraints {
                // apply constraints here
            }
        }

        "/"(view:"/index")
        "500"(view:'/error')
    }
}

And changed the application.properties to have the app.context=/

application.properties

#Grails Metadata file
#Sun Nov 06 14:51:56 EST 2011
app.grails.version=2.0.0.RC1
app.context=/
app.name=test1
app.servlet.version=2.5
app.version=0.1

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.

酸甜透明夹心 2024-12-20 12:15:16
  def yourAction = {

       redirect(uri:"helloworld")

  }

那对你不起作用吗?

  def yourAction = {

       redirect(uri:"helloworld")

  }

That doesnt work for you?

淡水深流 2024-12-20 12:15:16

这不是对你的问题的直接回答,但我的做法是:我从不修改 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文