【问题标题】:failed deploying war spring - failed to start component部署战争弹簧失败 - 无法启动组件
【发布时间】:2017-11-30 19:16:27
【问题描述】:

我已经尝试将 .WAR spring 部署到 tomcat 9 但出现错误 - 无法启动组件。

AppInitializer:

public class AppInitializer implements WebApplicationInitializer {

    public void onStartup(ServletContext container) throws ServletException {

        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(AppConfig.class);
        ctx.setServletContext(container);

        ServletRegistration.Dynamic servlet = container.addServlet(
                "dispatcher", new DispatcherServlet(ctx));

        servlet.setLoadOnStartup(1);
        servlet.addMapping("/");
    }
}

SpringBootWarDeploymentApplication:

@SpringBootApplication
public class SpringBootWarDeploymentApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootWarDeploymentApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringBootWarDeploymentApplication.class, args);
    }   
}

应用配置:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.project.maven")
public class AppConfig extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/static/");
        registry.addResourceHandler("**/**")
        .addResourceLocations("classpath:/META-INF/resources/"); // harus ada folder resources di webapp/WEB-INF/
    }       

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
        return messageSource;
    }   
}

在 web.xml 中,没有 servlet 配置。空配置。如何解决这个问题呢?当我放置我已经创建的 url 服务时,找不到它,我有一个问题。在tomcat中部署war时必须放置配置吗?尽管如此,我的代码在部署之前运行良好。没问题。

谢谢。 鲍比

【问题讨论】:

    标签: java spring spring-mvc tomcat war


    【解决方案1】:

    您需要配置web.xml。使用下面的参考代码:-

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>sample.traditional.config</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <!-- Disables Servlet Container welcome file handling. Needed for compatibility 
        with Servlet 3.0 and Tomcat 7.0 -->
    <welcome-file-list>
        <welcome-file></welcome-file>
    </welcome-file-list>
    
    </web-app>
    

    更多配置,您可以点击以下链接:-

    http://callistaenterprise.se/blogg/teknik/2014/04/15/a-first-look-at-spring-boot/

    https://dzone.com/articles/webapp-makeover-spring-4-and

    【讨论】:

    • 我已经更改了我的 web.xml,在“sample.traditional.config”中,我更改了我的包配置。真的吗?但仍然是同样的错误..
    【解决方案2】:

    即使我使用 spring boot 也收到了相同的 Hello world 错误消息。但只是尝试了最新版本的 tomcat(我使用的是 tomcat 8.5.23)。它的作用就像魅力!!!

    【讨论】:

      猜你喜欢
      • 2021-01-17
      • 2019-05-02
      • 2016-07-23
      • 1970-01-01
      • 1970-01-01
      • 2018-06-28
      • 1970-01-01
      • 2013-02-06
      • 1970-01-01
      相关资源
      最近更新 更多