Play! framework 2.0: How to display multiple image?

ぃ、小莉子 提交于 2019-12-18 17:29:43

问题


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 :

    @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. What is the best way to achieve this ?

PS: Image are located outside the play application.


回答1:


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:

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):

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

view (lack of @ character before photo.path is not accidental)

<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. 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).

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

<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 ! before ProxyPass / http://...



来源:https://stackoverflow.com/questions/10882313/play-framework-2-0-how-to-display-multiple-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!