qq_19430115
2017-05-23 23:10
采纳率: 0%
浏览 7.7k
已采纳

spring boot无法加载web.xml配置

刚写了一个spring-bootMVC 的dome, web.xml的配置写的是注解,书上说 只要继承了 WebApplicationInitializer 接口,启动是就会被自动获取到
web.xml ==>WebInitializer.java

 package com.sesame.config.web;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import com.sesame.config.Config;


public class WebInitializer implements WebApplicationInitializer {

    /**
     * 启动容器
     */
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        System.out.println("启动容器啦");
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(Config.class);
        ctx.setServletContext(servletContext);//将注册配置类和当前servletContext关联

        // 注册springMVC的DispatcherServlet
        Dynamic servlet =  servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);

    }
}

config.java

 package com.sesame.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
@ComponentScan("com.sesame")
public class Config {

    @Bean
    public InternalResourceViewResolver viewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/classes/views/");
        viewResolver.setSuffix(".jsp");
        viewResolver.setViewClass(JstlView.class);

        return viewResolver;
    }
}

但是我在启动后
System.out.println("启动容器啦");
这行代码没有被执行,也就是说 WebInitializer.java 没有被容器识别到,
谁知道这个问题呀?求帮助

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

  • qq_19430115 2017-05-24 03:08
    最佳回答

    已经解决了 改一下容器启动方式就好了

    已采纳该答案
    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题