[JAVA] Add arbitrary parameters to the URL output by <h: outputScript> <h: outputStylesheet> of JSF

Introduction

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.

procedure

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>

reference

Recommended Posts

Add arbitrary parameters to the URL output by <h: outputScript> <h: outputStylesheet> of JSF
Add the date to the GC statistics acquired by gcutil and output it.
Output of the book "Introduction to Java"
How to make the schema of the URL generated by Rails URL helper https
I want to output the day of the week
Add empty data to the top of the list
Output of how to use the slice method
[Java] How to get the URL of the transition source
Format of the log output by Tomcat itself in Tomcat 8
Convert the array of errors.full_messages to characters and output
[Rails] How to get the contents of strong parameters
I want to change the log output settings of UtilLoggingJdbcLogger
[Java] To know the type information of type parameters at runtime
How to add elements without specifying the length of the array
[Rails] How to display the list of posts by category
Add -parameters option to javac to get argument names by reflection