从数据库挂载静态资源(映像)

时间:2013-11-05 09:57:14

标签: java wicket wicket-1.5

我正在使用wicket 1.5.10,tomcat 7和Java 6.在我的代码中我做了:

public final class Images{
  public static void mountGlobalStaticImages(){
    //loading images from database - works perfect
    List<Object> imagesParams = GlobalTemplatesDAO.getGlobalImages();
    for(Object record : imagesParams){
      Map<String, Object> image = (Map<String, Object>) record;
      //here mount point is path like '/images/mylogo.png'
      String imagePath = (String) image.get("file_mount_point");
      //ComponentDynamicImage is extending DynamicImageResource, looks it works (it provides required byte[] data)
      ComponentDynamicImage imageData = new ComponentDynamicImage((byte[]) image.get("file_data"));
      //problem is here: looks that it mount image isn't in app and in log is http status code '302'
      //AppStart is extending WebApplication class
      AppStart.get().getSharedResources().add(imagePath, imageData);
    }
  }
}

问题是图片不在这里,在localhost_access_log.2013-11-04.txt中是行:

127.0.0.1 - - [04/Nov/2013:23:52:09 +0100] "GET /images/images/icon-fb.png HTTP/1.1" 302 -
127.0.0.1 - - [04/Nov/2013:23:52:09 +0100] "GET /images/images/cz-ico.gif?53 HTTP/1.1" 200 9067
127.0.0.1 - - [04/Nov/2013:23:52:09 +0100] "GET /images/images/icon-fb.png?54 HTTP/1.1" 200 9068
127.0.0.1 - - [04/Nov/2013:23:52:09 +0100] "GET /images/images/en-ico.gif HTTP/1.1" 302 -
127.0.0.1 - - [04/Nov/2013:23:52:09 +0100] "GET /images/images/en-ico.gif?55 HTTP/1.1" 200 9067

如何正确安装它们?据我所知,问题出在AppStart.get().getSharedResources().add(imagePath, imageData) 但不知道如何正确地做到这一点。

更新 对于该页面上的其他人,这是基于以下答案的工作代码:

public static void mountGlobalStaticImages(){
    //loading images from database
    List<Object> imagesParams = GlobalTemplatesDAO.getGlobalImages();
    for(Object record : imagesParams){
        Map<String, Object> image = (Map<String, Object>) record;
        //here mount point is path like '/images/mylogo.png'
        String imagePath = (String) image.get("file_mount_point");
        //ComponentDynamicImage extends DynamicImageResource, it provides required byte[] data
        ComponentDynamicImage imageData = new ComponentDynamicImage((byte[]) image.get("file_data"));
        //add imageData into shared resources on path
        AppStart.get().getSharedResources().add(imagePath, imageData);
        //mount from shared resources on path
        AppStart.get().mountResource(imagePath, new SharedResourceReference(imagePath));
    }
}

1 个答案:

答案 0 :(得分:2)

您不仅需要将资源添加到共享资源,还需要注册资源引用来访问它(此时我假设您确保imageData包含正确的数据)

getSharedResources().add("resourcePath", imageData);
mountResource("resourcePath", new SharedResourceReference("resourceName"));

之后,您可以使用

访问代码中的所有位置的共享资源
new SharedResourceReference("resourceName")