2011-09-08 114 views
7

起初,我有這個鏈接到Twitter的圖標:如何反向路由靜態文件?

@{'/public/images/twitter-icon.png'/} 

但現在我想說明根據類型Twitter-,Facebook-或LinkedIn圖標。所以,我創建了一個FastTag,它將類型作爲參數,代碼如下所示:

In the view: 
#{myApp.icon contact.type/} 

FastTag Java Code: 
String type = (String) args.get("arg"); 
out.print("/public/images/" + type + "-icon.png"); 

它工作正常。但是,我們構建服務器上運行,我們用前綴的URI這樣

http://ourdomain.com/appname/... 

應用顯然/公/圖像...不會在這裏工作。所以我想我必須要求路由器正確的地址。我試過以下沒有成功:

Router.reverse("/public/images/" + type + "-icon.png"); 
Router.reverse("/public/"); 
Router.reverse("staticDir:public"); 

所有這三個都導致NoRouteFoundException。我怎樣才能獲得我的圖標的正確路線?

在routes文件我有默認路由靜態文件

GET  /public/  staticDir:public 

回答

9

我相信這是你想要什麼:

String imageUrl = Router.reverse(VirtualFile.fromRelativePath("public/images/" + type + "-icon.png")); 
0

Router.reverse被用來生成URL形式的一個動作! 也許你可以定義,其中包括你的應用程序名稱和路由一個動作如路線:

GET /應用程序的名字/公/ TestController.test

現在,您可以使用

Router.reverse(「的TestController .test「)

獲取URL。

+0

任何機會,你可以嘗試和擴大這個答案一點?它並沒有真正說出任何使用現在:/ – obfuscation

0

我覺得這是更好地做一些事情,如:

GET  /img/  staticDir:public/images 

而且在模板剛:

out.print("/img/" + type + "-icon.png"); 
+0

這不會在我們的情況下運行在這樣的網址中運行:http://ourdomain.com/appname/img/ -icon.png 。你不會得到/ appname/img/ -icon.png just/img/ -icon.png –