您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页SpringMVC编程式配置HelloWord(5.0.1版本)

SpringMVC编程式配置HelloWord(5.0.1版本)

来源:二三娱乐

SpringMVC编程式配置(5.0.1版本)

编程式HelloWord演示

1 创建一个Maven-webapp工程。

在pom.xml加入以下依赖。(我为了方便添加了Tomcat插件,这个看你个人喜好)

<dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.1.RELEASE</version>
    </dependency>
    
    
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.2</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.2</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.9.2</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>springmvc</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <configuration>
          <port>7080</port>
          <uriEncoding>UTF-8</uriEncoding>
          <path>/</path>
        </configuration>
      </plugin>
    </plugins>
  </build>

编程式编写SpringMVC的配置

类名 主要作用
WebMvcConfigurer 配置SpringMVC
AbstractAnnotationConfigDispatcherServletInitializer 初始化DispatcherServlet

实现WebMvcConfigurer接口

配置SpringMVC其实很简单,只要实现WebMvcConfigurer接口 覆盖该接口中的方法即可完成配置,我这里为了简单的演示HelloWord所以不配置任何的东西。比如拦截器、视图控制器等 只需要覆盖这些方法即可完成。

package com.chenzhipeng.springmvc.config;

import 
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

/**
 * <p>Title:WebConfig</p>
 * <p>Description:SpringMVC的配置项</p>
 * @version V1.0
 * @author ZhiPeng_Chen
 * @date: 2017/11/23
 */
@Configuration
@EnableWebMvc
@ComponentScan("com.chenzhipeng.springmvc.controller")
public class SpringMvcConfig implements WebMvcConfigurer {

}

初始化DispatcherServlet

初始化DispatcherServlet,这个Servlet想必大家都知道,好理解点就相当于SpringMVC的中央处理器,因为不管你做什么样的交互都需要进过它来完成。继承AbstractAnnotationConfigDispatcherServletInitializer
(名字有点长)覆盖其3个方法即可。

package com.chenzhipeng.springmvc.servlet;
import com.chenzhipeng.springmvc.config.SpringMvcConfig;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
 * <p>Title:SpringMVCInitializer</p>
 * <p>Description:SpingMVC初始化DispatcherServlet</p>
 * @version V1.0
 * @author ZhiPeng_Chen
 * @date: 2017/11/23
 */
public class SpringMVCInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {


    @Nullable
    protected Class<?>[] getRootConfigClasses() {
        return null;
    }

    @Nullable
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { SpringMvcConfig.class };
    }

    protected String[] getServletMappings() {
        return  new String[] { "/" };
    }


}

可以看到在getServletConfigClasses()的方法中,它拿到了我的配置类进行初始化。

编写控制器

因为我现在所在的包已经通过实现WebMvcConfigurer接口的时候,使用了@ComponentScan注解进行扫描过了,所以是被发现的。

package com.chenzhipeng.springmvc.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>Title:HelloWordController</p>
 * <p>Description:HelloWord控制器</p>
 * @version V1.0
 * @author ZhiPeng_Chen
 * @date: 2017/11/23
 */
@RestController
public class HelloWordController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello SpringMVC!";
    }

}

运行程序

启动Tomcat发现hello已经映射进来了


启动Tomcat 页面显示

OK!整个流程就完成了。

Copyright © 2019- yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务