当前位置: 首页 > 知识库问答 >
问题:

SpringMVC与FreeMarker的集成

贺高飞
2023-03-14

我正在通过在线示例学习使用FreeMarker的SpringMVC。我遇到了这个错误,但是我不知道我的getFreemarkerConfig()方法有什么问题,一整天我都在试图修复它,但是没有成功。

@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "com.capfer")
public class Webconfig extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver getViewResolver() {
        FreeMarkerViewResolver frViewResolver = new FreeMarkerViewResolver();
        frViewResolver.setCache(false);
        // frViewResolver.setPrefix("");
        frViewResolver.setSuffix(".ftl");

        return frViewResolver;
    }

    @Bean
    public FreeMarkerConfigurer getFreemarkerConfig() throws IOException, TemplateException {
        FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
        factory.setTemplateLoaderPath("/WEB-INF/pages/");
        factory.setDefaultEncoding("UTF-8");
        FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
        freeMarkerConfigurer.setConfiguration(factory.createConfiguration());
        return freeMarkerConfigurer;
    }
}


SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getFreemarkerConfig' defined in com.capfer.configuration.Webconfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer]: Factory method 'getFreemarkerConfig' threw exception; nested exception is java.lang.NoSuchMethodError: freemarker.template.Configuration.<init>(Lfreemarker/template/Version;)V
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4135)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4630)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:445)
    at org.apache.catalina.startup.Embedded.start(Embedded.java:825)
    at org.codehaus.mojo.tomcat.AbstractRunMojo.startContainer(AbstractRunMojo.java:558)
    at org.codehaus.mojo.tomcat.AbstractRunMojo.execute(AbstractRunMojo.java:255)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:862)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:286)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:197)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer]: Factory method 'getFreemarkerConfig' threw exception; nested exception is java.lang.NoSuchMethodError: freemarker.template.Configuration.<init>(Lfreemarker/template/Version;)V
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    ... 45 more

共有1个答案

卫俊誉
2023-03-14

根错误是这样的:“java.lang.NoSuchMethodError:freemarker.template.Configuration(Lfreemarker/template/Version;)这意味着您有一个旧的FreeMarker依赖项,其中还没有配置(版本)构造函数。因此,请确保您使用的是最新的FreeMarker版本(2.3.26-incubating是本文撰写时最新的稳定版本)。(如果旧版本是可传递依赖项,则需要将其排除在外,以便在依赖关系图发生变化时,它不会再次获胜。)

 类似资料:
  • 我的样本代码在这里 尝试运行junit测试时,收到以下错误消息。 JAVAlang.IllegalStateException:未能加载ApplicationContext 原因:org。springframework。豆。工厂BeanCreationException:创建名为“nameDao”的bean时出错:调用init方法失败;嵌套的异常是java。lang.IllegalArgument

  • 下载地址: https://jfrog.nutz.cn/artifactory/jcenter/org/nutz/nutz-plugins-views/ maven项目的关联 <dependency> <groupId>org.nutz</groupId> <artifactId>nutz-plugins-views</artifactId>

  • 目前,我的应用程序使用SpringMVC进行所有控制器映射。我正在尝试实现一个tinyMCE拼写检查,它包括一个Servlet,我不确定如何在不修改该文件本身的情况下正确集成该Servlet。我想避免修改,这样如果我们以后有新版本,我们就可以了。 Servlet看起来像...

  • 我试图理解Spring MVC Restful架构。想知道有多少种方法可以将SpringMVC与RESTfulWeb服务集成在一起。我可以看到其中一个是使用Rest模板。 这是在Spring MVC中使用rest Web服务的唯一方法吗? 我们可以单独使用SpringMVC开发web应用程序而不使用任何web服务吗。 如果我说错了,请指正。 谢谢你的帮助。

  • 本文向大家介绍springmvc整合freemarker配置的详细步骤,包括了springmvc整合freemarker配置的详细步骤的使用技巧和注意事项,需要的朋友参考一下 一、对应的导包(有些包是不必须的) 二、在spring配置文件中配置 三、jdbc连接 jdbc.properties 四、log4j配置 与本项目无关的mybatis配置  以上就是本文的全部内容,希望对大家的学习有所帮助

  • 主要内容:1 XML配置方式,2 注解方式如果你使用 Spring MVC 来构建 Web 应用并对性能有较高的要求的话,可以使用 Fastjson 提供的FastJsonHttpMessageConverter 来替换 Spring MVC 默认的 HttpMessageConverter 以提高 @RestController @ResponseBody @RequestBody 注解的 JSON序列化速度。下面是配置方式,非常简单。