[英]Play! framework 2.0: How to display multiple image?
I need to display a gallery of photos. 我需要显示一个照片库。 So here is my 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 is my controller method: 这是我的控制器方法:
public static Result getPhotos() {
return ok(views.html.photo.gallery.render(Photo.get()));
}
And here is my Photo bean : 这是我的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 the photo absolute path in src attribute of img node, but doesn't work. 我把照片绝对路径放在img节点的src属性中,但不起作用。 What is the best way to achieve this ?
实现这一目标的最佳方法是什么?
PS: Image are located outside the play application. PS:图像位于播放应用程序之外。
Take a look at my very similar question: Direct serving files from outside of Play directories structure , finally I used my second suggestion in very basic sample it can be showed as: 看看我非常相似的问题: 从Play目录结构外部直接提供文件 ,最后我在非常基本的示例中使用了我的第二个建议,它可以显示为:
public static Result serve(String filepath){
// some stuff if required
return ok(new File("/home/user/files/"+filepath));
}
route (use asterisk with *filepath
to allow strings with slashes inside): route(使用带
*filepath
星号来允许带有斜杠的字符串):
GET /files/*filepath controllers.Application.serve(filepath : String)
view (lack of @
character before photo.path
is not accidental) view(在
photo.path
之前缺少@
字符不是偶然的)
<img src="@routes.Application.serve(photo.path)" alt="@photo.alt" />
edit: 编辑:
You of course don't need to serve files trough the controller if you have any HTTP server
and ability to create new subdomain/alias pointing to directory. 如果您有任何
HTTP server
并且能够创建指向目录的新子域/别名,那么您当然不需要通过控制器提供文件。 In such case you can just store links as http://pics.domain.tld/holidays_2012/1.jpg
or even better as holidays_2012/1.jpg
(and then prefix it in the template with subdomain). 在这种情况下,您可以将链接存储为
http://pics.domain.tld/holidays_2012/1.jpg
,甚至更好地存储为holidays_2012/1.jpg
(然后将其作为子域名添加到模板中)。
Finally you can set-up some alias ie. 最后你可以设置一些别名,即。 with Apache to use your
domain.tld/*
as pointer to Play app and domain.tld/pics/*
as pointer to some folder 与Apache一起使用你的
domain.tld/*
作为Play app的指针和domain.tld/pics/*
作为指向某个文件夹的指针
<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 from all
</Directory>
</VirtualHost>
in such case it's important to place ProxyPass /pics !
在这种情况下,放置
ProxyPass /pics !
很重要ProxyPass /pics !
before ProxyPass / http://...
在
ProxyPass / http://...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.