如何 运行 spring-boot embedded tomcat with tiles in eclipse?

How to run spring-boot embedded tomcat with tiles in eclipse?

我有一个 spring-boot 应用程序打包为可执行 warfile,支持嵌入式 tomcat 与 spring mvc、jsp 和 tiles。

在 shell 中使用 java -jar app.war 启动构建的 Web 应用程序时,我成功获得了服务 运行ning。相反,当我尝试 运行 eclipse 中的启动配置时,sprint-boot 无法完成 tile 的初始化,但出现以下异常:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tilesConfigurer' defined in class path resource [au/com/inspiredgroup/config/MvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No URL for class path resource [WEB-INF/tiles.xml]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1566)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:762)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:688)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:958)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:947)
    at au.com.inspiredgroup.Application.main(Application.java:86)
Caused by: java.lang.IllegalArgumentException: No URL for class path resource [WEB-INF/tiles.xml]
    at org.springframework.web.servlet.view.tiles3.SpringWildcardServletTilesApplicationContext.getResources(SpringWildcardServletTilesApplicationContext.java:96)
    at org.springframework.web.servlet.view.tiles3.TilesConfigurer$SpringTilesContainerFactory.getSources(TilesConfigurer.java:306)
    at org.apache.tiles.factory.BasicTilesContainerFactory.createLocaleDefinitionDao(BasicTilesContainerFactory.java:182)
    at org.apache.tiles.factory.BasicTilesContainerFactory.createDefinitionsFactory(BasicTilesContainerFactory.java:137)
    at org.springframework.web.servlet.view.tiles3.TilesConfigurer$SpringTilesContainerFactory.createDefinitionsFactory(TilesConfigurer.java:354)
    at org.apache.tiles.factory.BasicTilesContainerFactory.createContainer(BasicTilesContainerFactory.java:86)
    at org.springframework.web.servlet.view.tiles3.TilesConfigurer$SpringTilesContainerFactory.createContainer(TilesConfigurer.java:297)
    at org.apache.tiles.startup.AbstractTilesInitializer.createContainer(AbstractTilesInitializer.java:114)
    at org.apache.tiles.startup.AbstractTilesInitializer.initialize(AbstractTilesInitializer.java:64)
    at org.springframework.web.servlet.view.tiles3.TilesConfigurer.afterPropertiesSet(TilesConfigurer.java:271)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562)
    ... 15 more
Caused by: java.io.FileNotFoundException: class path resource [WEB-INF/tiles.xml] cannot be resolved to URL because it does not exist
    at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:187)
    at org.springframework.web.servlet.view.tiles3.SpringWildcardServletTilesApplicationContext.getResources(SpringWildcardServletTilesApplicationContext.java:91)
    ... 26 more

这是 MVC 配置:

@EnableWebMvc
@Configuration
public class MvcConfiguration extends WebMvcConfigurerAdapter {

    /**
     * Basic setup for JSP views.
     */
    @Bean(name = "viewResolver")
    public InternalResourceViewResolver configureInternalViewResolver() {
        final InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix(PREFIX);
        resolver.setSuffix(SUFFIX);
        resolver.setOrder(1);
        return resolver;
    }

    @Bean
    public TilesViewResolver tilesViewResolver() {
        final TilesViewResolver tilesViewResolver = new TilesViewResolver();
        tilesViewResolver.setOrder(0);
        return tilesViewResolver;
    }

    @Bean
    public TilesConfigurer tilesConfigurer() {
        final TilesConfigurer configurer = new TilesConfigurer();
        configurer.setDefinitions(new String[] { "classpath:/WEB-INF/tiles.xml" });
        return configurer;
    }
    [...]
}

eclipse 中的启动器配置为普通 Java 应用程序。

这发生在一个导入的项目中,它的 eclipse 设置没有在源代码控制存储库中共享(理解,在我看来,这通常是明智的(不是));可能是因为这个原因,也可能不是,无论如何使用 maven 原型创建新项目不会重现问题。

为了修复我在 eclipse 中的设置,我必须编辑启动配置(对于 运行 作为 Java Application),进入类路径选项卡,然后单击“高级...”按钮,然后 'Add folder' 并导航以添加 /src/main/webapp 文件夹。它将添加到 'User Entries'。