我有一个简单的“单一”页面应用程序,包含大量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中的一些配置,还是有另一种简单的方法来实现这一目标?
rich p
的回答让我想起了这个老问题,当我找到一个替代解决方案时,我决定将其作为答案添加,以防其他人有类似的问题:
我发现的一种方法是使用thymeleaf或更精确的page layouts。有了这个,我可以在单独的HTML文件中定义片段并添加th:fragment
属性。然后可以将这些文件包含在th:insert
或th:replace
的其他文件中。查看第二个链接以获取有关如何使用它的示例。
(告诉你这是一个古老的问题...)
也许你可以使用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;
}