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的配置

一般传统的SpingMVC需要在web.xml中设定,还需要配置一个的XML的配置文件来完成等等一系列的操作。但是现在不需要再那么繁琐、在代码中就可以设定好。最主要的是涉及到两个类,当然远远不止只有这些,这里只是为了简单的演示。
更加详细的学习可以到Spring的官网学习,点击这里你可以详细的学习SpringMVC的内容。

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

实现WebMvcConfigurer接口

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

package com.chenzhipeng.springmvc.config;

import org.springframework.context.annotation.ComponentScan;
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!";
    }

}

如果对注解不理解的话,可以参考Spring的官网查看注解的解释和定义。

运行程序

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

启动Tomcat

在游览器中输入:http://127.0.0.1:7080/hello

页面显示

OK!整个流程就完成了。

猜你喜欢

转载自www.linuxidc.com/Linux/2017-11/148905.htm