【问题标题】:Play! framework 2.0: How to display multiple image?玩! framework 2.0:如何显示多张图片?
【发布时间】:2012-06-04 13:38:40
【问题描述】:

我需要显示照片库。 所以这是我的模板:

@(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:图片位于播放应用程序之外。

【问题讨论】:

  • 请出示您的Photo bean 源代码。
  • 生成的 HTML 代码是什么?
  • 这是什么意思它不起作用?您是否预览了 HTML 页面的生成源代码?某个 URL 或某个文件系统文件夹的绝对路径(即/home/user/pics/1.jpg)?您可以在浏览器的address bar 中打开所需的照片并写入该路径吗?
  • 生成的 html 是一个带有标题库的空白页面。照片未加载。 src 属性的值是文件的绝对路径(/home/user/library/photo1.png)。
  • 不,我无法通过在浏览器地址栏中提供路径来访问照片。正如我所说,照片位于播放应用程序文件夹之外。

标签: java playframework playframework-2.0


【解决方案1】:

看看我非常相似的问题:Direct serving files from outside of Play directories structure,最后我在非常基本的示例中使用了我的第二个建议,它可以显示为:

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://...

很重要

【讨论】:

  • 谢谢。真的很明确的回应。现在我只需要缩小我的照片;)
猜你喜欢
  • 2016-04-16
  • 2013-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-25
  • 2016-10-28
  • 1970-01-01
相关资源
最近更新 更多