gpt4 book ai didi

嵌入式 Tomcat 中带有 JSP 标签库的 Spring-Boot

转载 作者:行者123 更新时间:2023-12-04 02:39:52 29 4
gpt4 key购买 nike

我目前正在迁移 Spring MVC Webapp(xml-config 到 java-config,tomcat 通过 spring-boot 到嵌入式 tomcat)。

webapp 使用 freemarker 作为模板引擎和 JSP Taglibs。现在,当我调用 freemarker 页面时,出现以下错误:

freemarker.ext.jsp.TaglibFactory$TaglibGettingException: 
No TLD was found for the "http://www.springframework.org/tags/form" JSP taglib URI. (TLD-s are searched according the JSP 2.2 specification. In development- and embedded-servlet-container setups you may also need the "MetaInfTldSources" and "ClasspathTlds" freemarker.ext.servlet.FreemarkerServlet init-params or the similar system properites.)

freemarker-header.ftl 以以下代码段开头:
<#assign form=JspTaglibs["http://www.springframework.org/tags/form"]>
<#assign core=JspTaglibs["http://java.sun.com/jstl/core"]>
<#assign spring=JspTaglibs["http://www.springframework.org/tags"]>
<#assign osc=JspTaglibs["/WEB-INF/osc.tld"]>

我没有找到 MetaInfTldSources 和 ClasspathTlds 的任何可用搜索结果。以前有人解决过这个问题吗?

韩国
哈比卜

最佳答案

这真的应该是内置的。

首先,禁用内置 FreeMarkerAutoConfiguration在您的 Application :

@SpringBootApplication
@EnableAutoConfiguration(exclude = {FreeMarkerAutoConfiguration.class})
public class Application extends WebMvcConfigurerAdapter {
...
]

然后添加这个自定义配置:

(改编自 https://github.com/isopov/fan/blob/master/fan-web/src/main/java/com/sopovs/moradanen/fan/WebApplicationConfiguration.java ;在 ObjectWrapper 中添加了 TaglibFactory 并删除了 addResourceHandlers() 覆盖)
import freemarker.cache.ClassTemplateLoader;
import freemarker.ext.jsp.TaglibFactory;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import javax.servlet.ServletContext;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Locale;
import java.util.Properties;

@Configuration
public class CustomFreemarkerConfiguration extends WebMvcConfigurerAdapter {


@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
registry.addInterceptor(localeChangeInterceptor);
}

@Bean
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages");
messageSource.setFallbackToSystemLocale(false);
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}

@Bean
public SessionLocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(Locale.ENGLISH);
return localeResolver;
}

@Bean
@Autowired
public freemarker.template.Configuration freeMarkerConfig(ServletContext servletContext) throws IOException,
TemplateException {
FreeMarkerConfigurer freemarkerConfig = configFreeMarkerConfigurer(servletContext);
return freemarkerConfig.getConfiguration();
}

@Bean
@Autowired
public TaglibFactory taglibFactory(ServletContext servletContext) throws IOException, TemplateException {
FreeMarkerConfigurer freemarkerConfig = configFreeMarkerConfigurer(servletContext);
TaglibFactory taglibFactory = freemarkerConfig.getTaglibFactory();
taglibFactory.setObjectWrapper(freemarker.template.Configuration.getDefaultObjectWrapper(freemarker.template.Configuration.getVersion()));
return taglibFactory;
}

@Autowired
@Bean
public FreeMarkerConfig springFreeMarkerConfig(ServletContext servletContext) throws IOException, TemplateException {
return new MyFreeMarkerConfig(freeMarkerConfig(servletContext), taglibFactory(servletContext));
}

private static FreeMarkerConfigurer configFreeMarkerConfigurer(ServletContext servletContext) throws IOException,
TemplateException {
FreeMarkerConfigurer freemarkerConfig = new FreeMarkerConfigurer();
freemarkerConfig
.setPreTemplateLoaders(new ClassTemplateLoader(CustomFreemarkerConfiguration.class, "/templates/"));
ServletContext servletContextProxy = (ServletContext) Proxy.newProxyInstance(
ServletContextResourceHandler.class.getClassLoader(),
new Class<?>[] { ServletContext.class },
new ServletContextResourceHandler(servletContext));
freemarkerConfig.setServletContext(servletContextProxy);
Properties settings = new Properties();
settings.put("default_encoding", "UTF-8");
freemarkerConfig.setFreemarkerSettings(settings);
freemarkerConfig.afterPropertiesSet();
return freemarkerConfig;
}

@Bean
public FreeMarkerViewResolver viewResolver() {
FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver();
viewResolver.setCache(false);
viewResolver.setSuffix(".ftl");
viewResolver.setContentType("text/html;charset=UTF-8");
return viewResolver;
}


private static class ServletContextResourceHandler implements InvocationHandler
{

private final ServletContext target;

private ServletContextResourceHandler(ServletContext target) {
this.target = target;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if ("getResourceAsStream".equals(method.getName())) {
Object result = method.invoke(target, args);
if (result == null) {
result = CustomFreemarkerConfiguration.class.getResourceAsStream((String) args[0]);
}
return result;
} else if ("getResource".equals(method.getName())) {
Object result = method.invoke(target, args);
if (result == null) {
result = CustomFreemarkerConfiguration.class.getResource((String) args[0]);
}
return result;
}

return method.invoke(target, args);
}
}

private static class MyFreeMarkerConfig implements FreeMarkerConfig {

private final freemarker.template.Configuration configuration;
private final TaglibFactory taglibFactory;

private MyFreeMarkerConfig(freemarker.template.Configuration configuration, TaglibFactory taglibFactory) {
this.configuration = configuration;
this.taglibFactory = taglibFactory;
}

@Override
public freemarker.template.Configuration getConfiguration() {
return configuration;
}

@Override
public TaglibFactory getTaglibFactory() {
return taglibFactory;
}
}
}

将以下内容添加到您的 pom.xml :
    <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>

然后你可以加载你的模板:
<#assign s=JspTaglibs["/META-INF/spring.tld"] />

<a href="${s.mvcUrl("IC#index").build()}">Home</a>

关于嵌入式 Tomcat 中带有 JSP 标签库的 Spring-Boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33755964/

29 4 0