我正忙着自己的资源实施.不会调用getInputStream方法.
我的经纪人:
我的经纪人:
public class ResourceHandlerWrapperImpl extends ResourceHandlerWrapper { private final ResourceHandler wrapped; public ResourceHandlerWrapper(final ResourceHandler wrapped) { this.wrapped = wrapped; } @Override public ResourceHandler getWrapped() { return wrapped; } @Override public Resource createResource(final String resourceName,final String libraryName) { if (AppConstants.RESOURCE_MEDIA_LIB.equals(libraryName)) { return new MediaResource(resourceName); } else { return super.createResource(resourceName,libraryName); } } /** * @see javax.faces.application.ResourceHandlerWrapper#libraryExists(java.lang.String) */ @Override public boolean libraryExists(final String libraryName) { if (AppConstants.RESOURCE_MEDIA_LIB.equals(libraryName)) { return true; } else { return super.libraryExists(libraryName); } } /** * @see javax.faces.application.ResourceHandlerWrapper#isResourceRequest(javax.faces.context.FacesContext) */ @Override public boolean isResourceRequest(final FacesContext context) { return super.isResourceRequest(context); } }
我的资源实施:
public class MediaResource extends Resource { private final String mediaId; public MediaResource(final String mediaId) { setLibraryName(AppConstants.RESOURCE_MEDIA_LIB); setResourceName(mediaId); setContentType("image/png"); this.mediaId = mediaId; } @Override public InputStream getInputStream() throws IOException { if (mediaId != null) { System.out.println("Yeahhh!!!"); } return null; } @Override public Map<String,String> getResponseHeaders() { return new HashMap<String,String>(); } @Override public String getRequestPath() { final FacesContext context = FacesContext.getCurrentInstance(); return context .getApplication() .getViewHandler() .getResourceURL( context,ResourceHandler.RESOURCE_IDENTIFIER + "/" + mediaId + "?ln=" + AppConstants.RESOURCE_MEDIA_LIB); } @Override public URL getURL() { return null; } @Override public boolean userAgentNeedsUpdate(final FacesContext context) { return true; } }
在我的faces-config.xml中:
<application> <resource-handler>com.foo.bbb.ResourceHandlerWrapperImpl</resource-handler> </application>
在我的jsf中:
<h:graphicImage library="media_lib" name="66" width="50" />
以html输出:
<img src="/foo/javax.faces.resource/66?ln=media_lib" width="50" />
从getRequestPath返回:/foo/javax.faces.resource/66?ln=media_lib
调用并初始化MediaResource,但不调用getInputStream. FireBug在此URL上显示404(称为两次).
我完全不知道我在这里做错了什么.
谢谢
强尼
解决方法
发现了错误.我的rescource实现的getRequestPath有问题.我忘记了faces-mapping(Util.getFacesMapping(context))到url中的faces-servlet:
@Override public String getRequestPath() { final FacesContext context = FacesContext.getCurrentInstance(); return context .getApplication() .getViewHandler() .getResourceURL( context,ResourceHandler.RESOURCE_IDENTIFIER + "/" + mediaId + Util.getFacesMapping(context) + "?ln=" + AppConstants.RESOURCE_MEDIA_LIB);
现在一切都按预期工作了.
感谢BalusC的帮助.
干杯强尼