0

我正在使用 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));
    }
}
4

1 回答 1

2

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

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

之后,您可以通过使用在代码中的任何地方访问共享资源

new SharedResourceReference("resourceName")
于 2013-11-11T08:23:01.110 回答