玩! 框架2.0:如何显示多个图像?(Play! framework 2.0: How to d

2019-06-25 04:24发布

我需要显示的照片画廊。 因此,这里是我的模板:

@(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>
}

这里是我的控制器方法:

public static Result getPhotos() {
    return ok(views.html.photo.gallery.render(Photo.get()));
}

这里是我的照片豆:

    @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();
}

    }

我把IMG节点的src属性的照片绝对路径,但不起作用。 什么是实现这一目标的最佳途径?

PS:图片所在的播放应用之外。

Answer 1:

看看我的非常类似的问题: 引导从播放目录结构之外提供文件 ,最后我用我的第二个建议非常基本的样本中可以表现为:

public static Result serve(String filepath){
    // some stuff if required
    return ok(new File("/home/user/files/"+filepath));
}

路线(使用星号与*filepath ,使里面斜杠字符串):

GET   /files/*filepath    controllers.Application.serve(filepath : String)

视图(缺乏@字符之前photo.path不是偶然的)

<img src="@routes.Application.serve(photo.path)" alt="@photo.alt" />

编辑:

当然,你不需要提供文件槽控制器,如果您有任何HTTP server并创建新的子域/别名指向目录的能力。 在这种情况下,你可以存储的链接, http://pics.domain.tld/holidays_2012/1.jpg甚至更好,因为holidays_2012/1.jpg (然后在子域模板前缀的话)。

最后,你可以设置一些别名即。 与Apache使用您domain.tld/*为指针Play应用程序和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>

在这样的情况下,放置很重要ProxyPass /pics ! 之前 ProxyPass / http://...



文章来源: Play! framework 2.0: How to display multiple image?