java - Play! framework 2.0: How to display multiple image? -
i need display gallery of photos. here template:
@(photos: list[photo]) @title = { <bold>gallery</bold> } @main(title,"photo"){ <ul class="thumbnails"> @for(photo <- photos) { <li class="span3"> <a href="#" class="thumbnail"> <img src="@photo.path" alt=""> </a> </li> } </ul> } and here controller method:
public static result getphotos() { return ok(views.html.photo.gallery.render(photo.get())); } and here photo bean :
@entity public class photo extends model { @id public long id; @required public string label; public string path; public photo(string path, string label) { this.path = path; this.label = label; } private static finder<long, photo> find = new finder<long, photo>( long.class, photo.class); public static list<photo> get() { return find.all(); } public static photo get(long id) { return find.byid(id); } public static void create(photo photo) { photo.save(); } public static void delete(long id) { find.ref(id).delete(); } } i put photo absolute path in src attribute of img node, doesn't work. best way achieve ?
ps: image located outside play application.
take @ similar question: direct serving files outside of play directories structure , used second suggestion in basic sample can showed as:
public static result serve(string filepath){ // stuff if required return ok(new file("/home/user/files/"+filepath)); } route (use asterisk *filepath allow strings slashes inside):
get /files/*filepath controllers.application.serve(filepath : string) view (lack of @ character before photo.path not accidental)
<img src="@routes.application.serve(photo.path)" alt="@photo.alt" /> edit:
you of course don't need serve files trough controller if have http server , ability create new subdomain/alias pointing directory. in such case can store links http://pics.domain.tld/holidays_2012/1.jpg or better holidays_2012/1.jpg (and prefix in template subdomain).
finally can set-up alias ie. apache use domain.tld/* pointer play app , domain.tld/pics/* pointer folder
<virtualhost *:80> proxypreservehost on servername domain.tld proxypass /pics ! proxypass / http://127.0.0.1:9000/ proxypassreverse / http://127.0.0.1:9000/ alias /pics/ /home/someuser/somefolder_with_pics/ <directory /home/someuser/somefolder_with_pics/> order allow,deny allow </directory> </virtualhost> in such case it's important place proxypass /pics ! before proxypass / http://...
Comments
Post a Comment