如何反向路由静态文件? [英] How do I reverse route a static file?

查看:76
本文介绍了如何反向路由静态文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

起初,我有一个指向Twitter图标的链接:

At first I had this link to a twitter icon:

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

但是现在我想根据类型显示Twitter,Facebook或LinkedIn图标.因此,我创建了一个以类型作为参数的FastTag,代码如下所示:

But now I want to show a Twitter-, Facebook- or LinkedIn icon depending on type. So, I created a FastTag that takes the type as a parameter and the code looks like this:

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

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

工作正常.但是,在我们的构建服务器上,我们以这样的方式在uri上运行带有前缀的应用程序

It works fine. But, on our build server we run the app with a prefix on the uri like this

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

显然/public/images ...在这里不起作用.所以我想我必须问路由器正确的地址.我尝试了以下失败的尝试:

Obviously /public/images... won't work here. So I figured I have to ask the Router for the proper address. I've tried the following with no success:

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

所有这三个结果均导致NoRouteFoundException.如何获得图标的正确路线?

All three result in a NoRouteFoundException. How can I get the correct route for my icons?

在路由文件中,我具有静态文件的默认路由

In the routes file I have the default route for static files

GET     /public/        staticDir:public

推荐答案

我相信这是您想要的:

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

这篇关于如何反向路由静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆