零xml搭建springmvc项目

零xml搭建springmvc项目

一,新建web项目

image-20200511153807328

二,导入相关依赖

<!--springmvc-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.5.RELEASE</version>
    </dependency>
    <!--spring-web-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.2.5.RELEASE</version>
    </dependency>
    <!--servlet-api至少在3.0以上-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
    </dependency>

三,编写springmvc的配置类

在基于xml配置的springmvc项目中,需要我们配置spring-mvc.xml,这里的配置类相当于spring-mvc.xml.

@Configuration
@ComponentScan("com.zhen.controller") //相当于xml中的<context:component-scan base-package="com.zhen.controller"/>
public class AppConfig extends WebMvcConfigurationSupport {
    //处理器适配器
    @Bean
    public HandlerAdapter handlerAdapter(){
        return new SimpleControllerHandlerAdapter();
    }
    //处理器映射器
    @Bean
    public HandlerMapping handlerMapping(){
        return new BeanNameUrlHandlerMapping();
    }
    //视图解析器
    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/jsp/"); //前缀
        viewResolver.setSuffix(".jsp"); //后缀
        return viewResolver;
    }

}

四,配置web应用初始化环境

4.1 编写一个类实现WebApplicationInitializer接口

4.2 实现onStartup方法

//当web服务器启动时,会自动加载MyWebAppApplication,并自动调用onStartup方法
public class MyWebAppApplication implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        //一annotation方法启动spring初始化容器
        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
         //加载springmvc配置类
        applicationContext.register(AppConfig.class);
        //配置DispatchServlet
        DispatcherServlet servlet = new DispatcherServlet(applicationContext);
        ServletRegistration.Dynamic dispatcherServlet = servletContext.addServlet("DispatcherServlet", servlet);
        //启动级别
        dispatcherServlet.setLoadOnStartup(1);
        dispatcherServlet.addMapping("/");

    }
}
为什么当web服务器启动时,会自动加载MyWebAppApplication,并自动调用onStartup方法

Servlet 3.0 API的一个新规范 SPI,如果需要某个类或者方法需要在容器启动的时候被调用,可以在项目的根路径下面指定一个META-INF/services下建立配置文件。

image-20200511162217043

//HandlesTypes 该注解会将WebApplicationInitializer的实现类以set集合的方式传到onStartup方法的Set<Class<?>参数中,并且会将set集合遍历
@HandlesTypes(WebApplicationInitializer.class)
public class SpringServletContainerInitializer implements ServletContainerInitializer {
	@Override
	public void onStartup(@Nullable Set<Class<?>> webAppInitializerClasses, ServletContext servletContext)
        
	}
}

五,测试

@Controller
public class TestController {
    @RequestMapping("/test")
    public String test(){
        return "test";
    }
}

test.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>successful</h1>
</body>
</html>

image-20200511164229039

posted @ 2020-05-11 16:46  lzhya  阅读(265)  评论(0)    收藏  举报