This is a method to dynamically add parameters at build time to get the latest Javascript and CSS loaded by JSF Facelets. Before modification) vendor.js After modification) vendor.js? V = 12345678
Goodbye to Super Reload.
Create a class that inherits the ResourceHandlerWrapper class.
CustomResourceHandler.java
package com.sample;
import java.util.Arrays;
import javax.faces.application.Resource;
import javax.faces.application.ResourceHandler;
import javax.faces.application.ResourceHandlerWrapper;
import javax.faces.application.ResourceWrapper;
public class CustomResourceHandler extends ResourceHandlerWrapper {
private final ResourceHandler wrapped;
public CustomResourceHandler(ResourceHandler wrapped) {
this.wrapped = wrapped;
}
@Override
public Resource createResource(String resourceName) {
return createResource(resourceName, null, null);
}
@Override
public Resource createResource(String resourceName, String libraryName) {
return createResource(resourceName, libraryName, null);
}
@Override
public Resource createResource(String resourceName, String libraryName, String contentType) {
final Resource resource = super.createResource(resourceName, libraryName, contentType);
if (resource == null) {
return null;
}
if (!Arrays.asList("js", "css").contains(libraryName)) {
//Target Javascript and CSS
return resource;
}
return new ResourceWrapper() {
@Override
public String getRequestPath() {
return super.getRequestPath() + "&(Arbitrary query parameters)";
}
@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 ResourceHandler getWrapped() {
return wrapped;
}
}
Register the handler created above in faces-config.xml.
WEB-INF/faces-config.xml
<application>
<resource-handler>com.sample.CustomResourceHandler</resource-handler>
</application>
Recommended Posts