本文介绍了乔尔&Springboot - 资源文件夹未解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是 css & 包js 文件未解析.

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

我正在将我的 WAR 应用程序迁移到 Spring Boot 应用程序.

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

这是jawr 的servlet 3.0 配置:

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;
        }
    }
}

可能 application.properties 的一部分:

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

以及它是如何在我的 JSP 中声明的

And how it's declared in my JSP

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

如果我设置了jawr.debug.on=true一切正常:

If I set jawr.debug.on=trueEverything 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">

如果我设置了jawr.debug.on=false一个哈希前缀链接到版本它.但是id不再解析.

If I set jawr.debug.on=falseA 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.

推荐答案

我解决了我的问题.

这是我的新配置:

@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;
        }
    }
}

并使用 *.css、*.js 映射我的 servlet 调度程序

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

这篇关于乔尔&amp;Springboot - 资源文件夹未解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 14:51