【问题标题】:How can I use SSI with spring-boot?如何将 SSI 与 spring-boot 一起使用?
【发布时间】:2018-04-23 14:18:48
【问题描述】:

我有一个简单的“单页”应用程序,其中包含大量 JavaScript 和 jQueryUI 对话框。我正在为 REST API 使用 spring-boot,并且目前从 /resources/public 文件夹提供 *.html 页面。我现在想将 jQueryUI Dialog div 提取到单独的文件中以使代码更清晰,但我没有找到一种简单的方法来将它们作为服务器端包含。我希望只对 SSI 使用嵌入式 tomcat 指令:https://tomcat.apache.org/tomcat-7.0-doc/ssi-howto.html 但这似乎没有得到评估。我是否错过了 application.properties 中的某些配置,或者是否有其他简单的方法可以实现这一点?

【问题讨论】:

    标签: java spring-boot ssi


    【解决方案1】:

    rich p 的回答让我想起了这个老问题,当我找到替代解决方案时,我决定将其添加为答案,以防其他人有类似问题:

    我发现的一种方法是使用thymeleaf 或更准确地说是他们的page layouts。有了这个,我可以在单独的 HTML 文件中定义 sn-ps 并添加 th:fragment 属性。然后可以使用th:insertth:replace 将它们包含在其他文件中。查看第二个链接以获取有关如何使用它的示例。

    【讨论】:

    • 我的 0.02 美元 - @Thomas - 包括您采用的方法是个好主意。我听说它对你有用,坦率地说,如果其他人做了我所做的搜索(“我可以用 Spring Boot 做 SSI 吗?”),这个页面应该会出现。还不如分享你学到的东西!感谢您发布此内容。
    【解决方案2】:

    (承认这是一个古老的问题...)

    也许您可以使用this other SO page 上的建议之一注册 Tomcat 过滤器 (SSIFilter)。例如,Haim's (approved) solution 包括使用如下代码注册过滤器。我没有立即看到的与您的用例相关的唯一一点是如何配置SSIFilter :)

    下面的代码是猜想——我实际上并没有尝试过。作为风险评估问题,我一直在调查是否可以按照您的要求进行操作。我很感兴趣 - 你真的做到了吗?有人吗?

        import org.apache.catalina.ssi.SSIFilter;
    
        @Bean
        public FilterRegistrationBean someFilterRegistration() {
    
            FilterRegistrationBean registration = new FilterRegistrationBean();
            registration.setFilter(someFilter());
            registration.addUrlPatterns("/url/*");
            registration.addInitParameter("paramName", "paramValue");
            registration.setName("someFilter");
            registration.setOrder(1);
            return registration;
        } 
    
        public Filter someFilter() {
            // SSIFilter filt = new SSIFilter();
            // ..create and configure the  new SSIFilter() ...
    
            Filter filt = new SSIFilter() {
                public void reconfigure( FilterConfig config ) {
                    this.config = config;
    
                    // reconfigure only what you care about
                    this.allowExec = config.allowExec;
                    this.contentTypeRexEx = config.contentTypeRegEx;
                    this.debug = config.debug;
                    this.expires = config.expires;
                    this.isVirtualWebappRelative = config.isVirtualWebappRelative;
                }
            };
    
            /*
                From:  https://tomcat.apache.org/tomcat-7.0-doc/ssi-howto.html 
    
                contentType - A regex pattern that must be matched before SSI processing is applied.
                    When crafting your own pattern, don't forget that a mime content type
                    may be followed by an optional character set in the form "mime/type;
                    charset=set" that you must take into account. Default is
                    "text/x-server-parsed-html(;.*)?".
                debug - Debugging detail level for messages logged by this servlet. Default 0.
                expires - The number of seconds before a page with SSI directives will expire.
                    Default behaviour is for all SSI directives to be evaluated for every request.
                isVirtualWebappRelative - Should "virtual" SSI directive paths be interpreted
                    as relative to the context root, instead of the server root? Default false.
                allowExec - Is the exec command enabled? Default is false.
             */
    
            FilterConfig fcfg = new FilterConfig() {
                public FilterConfig withChanges( boolean allowExec, Pattern contentTypeRegEx, int debug, Long expires, boolean isVirtualWebappRelative ) {
                    this.allowExec = allowExec;
                    this.contentTypeRegEx = contentTypeRegEx;
                    this.debug = debug;
                    this.expires = expires;
                    this.isVirtualWebappRelative = isVirtualWebappRelative;
                }
            };
    
            filt.reconfigure(
                fcfg.withChanges(
                    false,
                    "text/x-server-partsed-html(;.*)?",
                    java.util.logging.Level.FINEST.intValue(),
                    (Instant.now().getEpochSecond() + (24*60*60)),
                    false
                )
            );
    
            // ok hopefully that configured it!
            return filt;
        }
    

    【讨论】:

    • 感谢您迟到的答复。我放弃了这种方法,现在使用 thymeleaf,它也允许我将代码分成可重用的 sn-ps。
    猜你喜欢
    • 2020-11-02
    • 2021-01-02
    • 2020-03-09
    • 2020-12-29
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    相关资源
    最近更新 更多