2011-11-02 54 views
5

我注意到一些奇怪的行爲redirect當我設置我的app.context某種方式。我發現,在Grails的JIRA這恰如其分地描述了我的問題的錯誤,但它被標記爲UTR:http://jira.grails.org/browse/GRAILS-7546使用app.context時,Grails會重定向到錯誤的地址?


這裏是我的問題描述:
我目前使用的Grails 2.0M2。我在application.properties定義了以下屬性文件:

app.context=/ 
app.name=foobar 

當我打電話redirect在控制器,redirect是增加了應用程序的名稱到URI我提供,然後導致404.這是如何我「M這樣做:

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最簡單的方式,而不需要做太多的手動步驟?

回答

0

這不是一個直接的問題,但我的做法是:我從不修改grails屬性中的app.context,因爲tomcat在生產中覆蓋它,我不關心它在我的開發中使用哪個上下文。

+1

正確... Tomcat可能會在生產中覆蓋app.context,但問題仍然是'重定向',即在構建重定向URL而不是使用app.context時,Grails正在使用app.name。 – Polaris878

1

我討厭這麼說,但我不能重現它。我創建了一個測試項目,一個控制器(名爲你好),使用您的代碼來創建一個什麼也不做的動作,但重定向:

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中的視圖/你好

index.gsp中在UrlMappings

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

設置的HelloWorld映射到該問候控制器的索引作用:

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部分自己添加到partialUrl。 3)最後的手段,編寫一個附加到afterView的Filter,執行正則表達式搜索並替換mysite.com/foobar的內容。不知道這會捕捉重定向,但如果有的話,我會認爲這是因爲過濾器可以廣泛應用。

1
def yourAction = { 

     redirect(uri:"helloworld") 

    } 

這不適合你嗎?