javaspring-bootspring-mvcjawr

Jawr & Springboot - Resources folders not resolved


my problem is that the bundle of css & js files are not resolved.

I am migrating my WAR application to a Spring Boot application.

Here is the servlet 3.0 configuration for jawr :

@Configuration
public class WebJawrConfiguration {

    private final Properties properties;

    public WebJawrConfiguration(final WebSelfiscProperties webProperties) {
        this.properties = new Properties();
        this.properties.putAll(webProperties.getJawr().entrySet().stream().collect(Collectors.toMap(e -> "jawr." + e.getKey(), Entry::getValue)));
    }

    @Bean
    public JawrSpringController jawrBinaryController() {
        final JawrSpringController jawrBinaryController = new JawrSpringController();
        jawrBinaryController.setConfiguration(properties);
        jawrBinaryController.setType(JawrConstant.BINARY_TYPE);
        return jawrBinaryController;
    }

    @Bean
    @DependsOn("jawrBinaryController")
    public JawrSpringController jawrCssController() {
        final JawrSpringController jawrCssController = new JawrSpringController();
        jawrCssController.setConfiguration(properties);
        jawrCssController.setType(JawrConstant.CSS_TYPE);
        // jawrCssController.setMapping("/public/bundles/css");
        return jawrCssController;
    }

    @Bean
    @DependsOn("jawrCssController")
    public JawrSpringController jawrJsController() {
        final JawrSpringController jawrJsController = new JawrSpringController();
        jawrJsController.setConfiguration(properties);
        jawrJsController.setType(JawrConstant.JS_TYPE);
        // jawrJsController.setMapping("/public/bundles/js");
        return jawrJsController;
    }

    @Configuration
    @ConditionalOnMissingBean(name = "jawrHandlerMapping")
    @DependsOn("jawrJsController")
    public class jawrHandlerMappingConfiguration {

        private final JawrSpringController jawrJsController;
        private final JawrSpringController jawrCssController;
        private final JawrSpringController jawrBinaryController;

        /**
         * Constructeur
         *
         * @param jawrJsController
         * @param jawrCssController
         * @param jawrBinaryController
         */
        public jawrHandlerMappingConfiguration(final JawrSpringController jawrJsController, final JawrSpringController jawrCssController, final JawrSpringController jawrBinaryController) {
            super();
            this.jawrJsController = jawrJsController;
            this.jawrCssController = jawrCssController;
            this.jawrBinaryController = jawrBinaryController;
        }

        @Bean
        public HandlerMapping jawrHandlerMapping() {
            final SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
            handlerMapping.setOrder(Ordered.HIGHEST_PRECEDENCE);

            final Map<String, Object> urlMap = new HashMap<>();
            urlMap.put("**/*.css", jawrCssController);
            urlMap.put("**/*.eot", jawrBinaryController);
            urlMap.put("**/*.gif", jawrBinaryController);
            urlMap.put("**/*.ico", jawrBinaryController);
            urlMap.put("**/*.jpg", jawrBinaryController);
            urlMap.put("**/*.jpeg", jawrBinaryController);
            urlMap.put("**/*.js", jawrJsController);
            urlMap.put("**/*.png", jawrBinaryController);
            urlMap.put("**/*.ttf", jawrBinaryController);
            urlMap.put("**/*.woff", jawrBinaryController);
            urlMap.put("**/*.woff2", jawrBinaryController);
            urlMap.put("**/*.svg", jawrBinaryController);
            handlerMapping.setUrlMap(urlMap);

            return handlerMapping;
        }
    }
}

A part of may application.properties :

# Common properties
jawr.debug.on=false
jawr.gzip.on=false
jawr.gzip.ie6.on=false
jawr.charset.name=UTF-8

jawr.css.bundle.base.id=/base.css
jawr.css.bundle.base.mappings=/public/css/blue-theme.css,/public/css/main.css

And how it's declared in my JSP

<jawr:style src="/base.css" />

If I set jawr.debug.on=true Everything is working fine:

<link rel="stylesheet" type="text/css" media="screen" href="/sel/public/css/blue-theme.css?d=1136145833">
<link rel="stylesheet" type="text/css" media="screen" href="/sel/public/css/main.css?d=762931402">

If I set jawr.debug.on=false A hash prefixes the link to version it. But the id is no longer resolved.

<link rel="stylesheet" type="text/css" media="screen" href="/2096063500/base.css">

I have been trying to correct this problem for over a week. Without success.

Has anyone ever encountered this problem? Thank you.


Solution

  • I resolved my issue.

    Here is my new configuration :

    @Configuration
    public class WebJawrConfiguration {
    
        private final Properties properties;
    
        /**
         * Constructor
         *
         * @param webProperties
         */
        public WebJawrConfiguration(final WebSelfiscProperties webProperties) {
            this.properties = new Properties();
            this.properties.putAll(webProperties.getJawr().entrySet().stream().collect(Collectors.toMap(e -> "jawr." + e.getKey(), Entry::getValue)));
        }
    
        @Bean
        @DependsOn("jawrBinaryController")
        public JawrSpringController jawrCssController() {
            final JawrSpringController jawrCssController = new JawrSpringController();
            final Properties propertiesCss = new Properties();
            propertiesCss.putAll(properties.entrySet().stream().filter(e -> !e.getKey().toString().contains("jawr.js"))
                    .collect(Collectors.toMap(Entry::getKey, Entry::getValue)));
            jawrCssController.setConfiguration(propertiesCss);
            jawrCssController.setType(JawrConstant.CSS_TYPE);
            return jawrCssController;
        }
    
        @Bean
        @DependsOn("jawrCssController")
        public JawrSpringController jawrJsController() {
            final JawrSpringController jawrJsController = new JawrSpringController();
            final Properties propertiesJs = new Properties();
            propertiesJs.putAll(properties.entrySet().stream().filter(e -> !e.getKey().toString().contains("jawr.css"))
                    .collect(Collectors.toMap(Entry::getKey, Entry::getValue)));
            jawrJsController.setConfiguration(propertiesJs);
            jawrJsController.setType(JawrConstant.JS_TYPE);
            return jawrJsController;
        }
    
        @Configuration
        @DependsOn("jawrJsController")
        public class jawrHandlerMappingConfiguration {
    
            private final JawrSpringController jawrJsController;
            private final JawrSpringController jawrCssController;
    
    
            /**
             * Constructor
             *
             * @param jawrJsController
             * @param jawrCssController
             */
            public jawrHandlerMappingConfiguration(final JawrSpringController jawrJsController, final JawrSpringController jawrCssController) {
                super();
                this.jawrJsController = jawrJsController;
                this.jawrCssController = jawrCssController;
            }
    
            @Bean
            @DependsOn("jawrJsController")
            public HandlerMapping jawrHandlerMapping() {
                final SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
                handlerMapping.setOrder(Ordered.HIGHEST_PRECEDENCE);
    
                final Map<String, Object> urlMap = new HashMap<>();
                urlMap.put("**/*.css", jawrCssController);
                urlMap.put("**/*.js", jawrJsController);
                handlerMapping.setUrlMap(urlMap);
    
                return handlerMapping;
            }
        }
    }
    

    And map my servlet dispatcher with *.css, *.js