我有一个使用Spring io.spring.platform 1.0.1.RELEASE构建的Spring IO项目,该项目现在已经寿终正寝了。我非常成功地将thymeleaf-spring4以及dandelion-core 0.10.1和dandelion-thymeleaf 0.10.1一起用于处理JavaScript和CSS依赖项绑定。快进了4年,我试图将我的项目更新为Spring 5,无论是否安装了春季靴,而我的障碍之一是缺少一些示例,这些示例说明了如何使用更新的spring和thymeleaf框架配置蒲公英,我喜欢不够聪明,无法自行解决。自2015年以来没有更新,蒲公英项目似乎已经停滞不前。http://dandelion.github.io/blog/和https://github.com/dandelion/dandelion。我不确定这是不可能的,但是当我将spring4与蒲公英0.10.0一起使用时,我将spring上下文名称空间架构保持为3.2,而1.1.1样本仍全部基于spring3。因此,这可能意味着它从未在Spring 3之前进行过更新,但也可能只是其他一些处理AMD加载的方法,好像使用require.js可能已经使对蒲公英捆扎机的需求过时了,而我只是没有知道。
有人让蒲公英与spring5配合使用,可以说这是如何工作的吗?如果不需要,是否有其他方法来处理将复杂的JS包加载到呈现的模板中?我记得,在正常的AMD模式中无法使用require.js来限制使用百里香叶渲染的镜腿时加载哪些资源,因为必须将包含声明为标头的一部分。蒲公英负责捆绑包中的依赖关系和排序,并构建必需的包并适当地排序它们。尽管新的js和css内联解析看起来很方便https://www.baeldung.com/spring-thymeleaf-3,但仍然不能满足包含js库集的需求。任何意见,将不胜感激。
我一直在致力于将项目转换为纯Java配置,以解决已知的xml名称空间问题。但是尝试加载蒲公英方言时仍然出现此Java错误:
2019-07-08 16:56:56,898警告[RMI TCP Connection(3)-127.0.0.1] support.AbstractApplicationContext(AbstractApplicationContext.java:557)-上下文初始化期间遇到异常-取消刷新尝试:org.springframework .beans.factory.BeanCreationException:创建在类路径资源[com / srcsolutions / ecumcms / springJavaConfig / DandelionConfig.class]中定义的名称为“ dandelionDialect”的bean时出错:通过工厂方法实例化Bean失败;嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[com.github.dandelion.thymeleaf.dialect.DandelionDialect]:工厂方法'dandelionDialect'抛出了异常;嵌套的异常是java.lang.NoSuchMethodError:org.thymeleaf.dialect.AbstractDialect:方法()V未找到 2019-07-08 16:56:56,902错误[RMI TCP连接(3)-127.0.0.1] servlet.FrameworkServlet(FrameworkServlet.java:533)-上下文初始化失败 org.springframework.beans.factory.BeanCreationException:创建在类路径资源[com / srcsolutions / ecumcms / springJavaConfig / DandelionConfig.class]中定义的名称为'dandelionDialect'的bean时出错:通过工厂方法实例化Bean失败;嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[com.github.dandelion.thymeleaf.dialect.DandelionDialect]:工厂方法'dandelionDialect'抛出了异常;嵌套的异常是java.lang.NoSuchMethodError:org.thymeleaf.dialect.AbstractDialect:方法()V未找到 在org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:627)〜[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE] 在org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:456)〜[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
我的Pom有:`
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-conditionalcomments</artifactId>
<version>2.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-spring-environment-dialect</artifactId>
<version>0.3.0</version>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
<version>2.4.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.dandelion</groupId>
<artifactId>dandelion-core</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.github.dandelion</groupId>
<artifactId>dandelion-thymeleaf</artifactId>
<version>1.1.1</version>
</dependency>
`
然后我有一些配置类:
package com.srcsolutions.ecumcms.springJavaConfig;
import com.github.dandelion.core.web.DandelionFilter;
import com.github.dandelion.thymeleaf.dialect.DandelionDialect;
import nz.net.ultraq.thymeleaf.LayoutDialect;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.thymeleaf.extras.conditionalcomments.dialect.ConditionalCommentsDialect;
import org.thymeleaf.extras.springsecurity5.dialect.SpringSecurityDialect;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;
import javax.servlet.Filter;
@Configuration
@EnableWebMvc
public class ThymeleafContextConfig implements ApplicationContextAware {
private ApplicationContext applicationContext;
public void setApplicationContext(final ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.addDialect(new LayoutDialect());
templateEngine.addDialect(new DandelionDialect());
templateEngine.addDialect(new ConditionalCommentsDialect());
templateEngine.addDialect(new SpringSecurityDialect());
// Enabling the SpringEL compiler with Spring 4.2.4 or newer can
// speed up execution in most scenarios, but might be incompatible
// with specific cases when expressions in one template are reused
// across different data types, so this flag is "false" by default
// for safer backwards compatibility.
templateEngine.setEnableSpringELCompiler(false);
return templateEngine;
}
@Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setApplicationContext(this.applicationContext);
templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html");
// HTML is the default value, added here for the sake of clarity.
templateResolver.setTemplateMode(TemplateMode.HTML);
// Template cache is true by default. Set to false if you want
// templates to be automatically updated when modified.
templateResolver.setCacheable(true);
return templateResolver;
}
@Bean
public ThymeleafViewResolver viewResolver(){
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
// NOTE 'order' and 'viewNames' are optional
viewResolver.setOrder(1);
return viewResolver;
}
}
and :
package com.srcsolutions.ecumcms.springJavaConfig;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import com.github.dandelion.core.web.DandelionFilter;
import com.github.dandelion.core.web.DandelionServlet;
public class ApplicationInitializer implements WebApplicationInitializer {
private ApplicationContext applicationContext;
public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// Register the Web application context
AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
mvcContext.register(WebContextConfig.class);
// Context loader listener
servletContext.addListener(new ContextLoaderListener(mvcContext));
// Register the Dandelion filter
//FilterRegistration.Dynamic dandelionFilter = servletContext.addFilter("dandelionFilter", new DandelionFilter());
//dandelionFilter.addMappingForUrlPatterns(null, false, "/*");
// Register the Dandelion servlet
ServletRegistration.Dynamic dandelionServlet = servletContext.addServlet("dandelionServlet", new DandelionServlet());
dandelionServlet.setLoadOnStartup(2);
dandelionServlet.addMapping("/dandelion-assets/*");
}
}
`
所以我仍然不确定春季和蒲公英的组合是否完全兼容。现在,如果我注释掉templateEngine.addDialect(new DandelionDialect());,错误消失了,并且我能够获取要渲染的基本模板,但是未处理与蒲公英json资源文件的捆绑。