如何在弹簧靴中使用SSI?

时间:2018-04-23 14:18:49

标签: java spring-boot ssi

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

请问这是一个古老的问题...

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

下面的代码是 conjecture -我实际上没有尝试过。我一直在调查是否有可能做您要问的事情,作为风险评估的问题。我很感兴趣-您真的做到了吗?有人吗?

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