开发者

How do I reverse route a static file?

At first I had this link to a twitter icon:

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

But now I want to show a Twitter-, Facebook- or LinkedIn i开发者_运维问答con 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");

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

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

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");

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


I believe this is what you want:

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


Router.reverse be used generate URL form one action! maybe you can define a route which include your app name and route one action eg:

GET /appname/public/ TestController.test

now,you can use

Router.reverse("TestController.test")

get the URL.


I think it's better to do something like:

GET      /img/     staticDir:public/images

And in the template just:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜