如何反向路由静态文件



一开始我有一个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

我相信这就是你想要的:

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

路由器。反向使用生成URL的形式一个动作!也许你可以定义一个包含你的应用名和路由一个动作的路由,例如:

GET/appname/public/TestController.test现在,你可以用

Router.reverse (TestController.test)

我认为这样做比较好:

GET      /img/     staticDir:public/images

在模板中只是:

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

最新更新