这是可行的具有自定义
ResourceHandler它返回
createResource()一个
Resource这反过来又返回上一个“未映射”的URL
Resource#getRequestPath()。您仅需要将默认的JSF资源前缀添加
/javax.faces.resource/*到映射
<url-pattern>列表中
FacesServlet,以便无论如何都能触发它。
此外,您需要重写
isResourceRequest()以检查URL是否以JSF资源前缀开头,并且还
handleResourceRequest()需要定位和流传输正确的资源。
总而言之,这应该做到:
public class UnmappedResourceHandler extends ResourceHandlerWrapper { private ResourceHandler wrapped; public UnmappedResourceHandler(ResourceHandler wrapped) { this.wrapped = wrapped; } @Override public Resource createResource(final String resourceName, final String libraryName) { final Resource resource = super.createResource(resourceName, libraryName); if (resource == null) { return null; } return new ResourceWrapper() { @Override public String getRequestPath() { ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); String mapping = externalContext.getRequestServletPath(); if (externalContext.getRequestPathInfo() == null) { mapping = mapping.substring(mapping.lastIndexOf('.')); } String path = super.getRequestPath(); if (mapping.charAt(0) == '/') { return path.replaceFirst(mapping, ""); } else if (path.contains("?")) { return path.replace(mapping + "?", "?"); } else { return path.substring(0, path.length() - mapping.length()); } } @Override // Necessary because this is missing in ResourceWrapper (will be fixed in JSF 2.2). public String getResourceName() { return resource.getResourceName(); } @Override // Necessary because this is missing in ResourceWrapper (will be fixed in JSF 2.2). public String getLibraryName() { return resource.getLibraryName(); } @Override // Necessary because this is missing in ResourceWrapper (will be fixed in JSF 2.2). public String getContentType() { return resource.getContentType(); } @Override public Resource getWrapped() { return resource; } }; } @Override public boolean isResourceRequest(FacesContext context) { return ResourceHandler.RESOURCE_IDENTIFIER.equals(context.getExternalContext().getRequestServletPath()); } @Override public void handleResourceRequest(FacesContext context) throws IOException { ExternalContext externalContext = context.getExternalContext(); String resourceName = externalContext.getRequestPathInfo(); String libraryName = externalContext.getRequestParameterMap().get("ln"); Resource resource = context.getApplication().getResourceHandler().createResource(resourceName, libraryName); if (resource == null) { super.handleResourceRequest(context); return; } if (!resource.userAgentNeedsUpdate(context)) { externalContext.setResponseStatus(HttpServletResponse.SC_NOT_MODIFIED); return; } externalContext.setResponseContentType(resource.getContentType()); for (Entry<String, String> header : resource.getResponseHeaders().entrySet()) { externalContext.setResponseHeader(header.getKey(), header.getValue()); } ReadableByteChannel input = null; WritableByteChannel output = null; try { input = Channels.newChannel(resource.getInputStream()); output = Channels.newChannel(externalContext.getResponseOutputStream()); for (ByteBuffer buffer = ByteBuffer.allocateDirect(10240); input.read(buffer) != -1; buffer.clear()) { output.write((ByteBuffer) buffer.flip()); } } finally { if (output != null) try { output.close(); } catch (IOException ignore) {} if (input != null) try { input.close(); } catch (IOException ignore) {} } } @Override public ResourceHandler getWrapped() { return wrapped; }}
将其注册如下
faces-config.xml:
<application> <resource-handler>com.example.UnmappedResourceHandler</resource-handler></application>
通过以下方式扩展
FacesServletURL模式
ResourceHandler.RESOURCE_IDENTIFIER:
<servlet-mapping> <servlet-name>facesServlet</servlet-name> <url-pattern>*.xhtml</url-pattern> <url-pattern>/javax.faces.resource/*</url-pattern></servlet-mapping>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)