首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jawr和Springboot -资源文件夹未解析

Jawr和Springboot -资源文件夹未解析
EN

Stack Overflow用户
提问于 2021-07-19 12:49:38
回答 1查看 52关注 0票数 1

我的问题是css & js文件的捆绑包没有被解析。

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

下面是jawr的servlet 3.0配置:

代码语言:javascript
复制
@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;
        }
    }
}

may application.properties的一部分:

代码语言:javascript
复制
# 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中声明它

代码语言:javascript
复制
<jawr:style src="/base.css" />

如果我设置了jawr.debug.on=true,一切都可以正常工作:

代码语言:javascript
复制
<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不再被解析。

代码语言:javascript
复制
<link rel="stylesheet" type="text/css" media="screen" href="/2096063500/base.css">

我已经试着纠正这个问题一个多星期了。没有成功。

有没有人遇到过这个问题?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-23 13:20:28

我解决了我的问题。

这是我的新配置:

代码语言:javascript
复制
@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;
        }
    }
}

并将我的servlet调度程序与*.css、*.js进行映射

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68435234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档