SpringMVC使用步驟

1.导入依赖坐标

	<dependency>
      <groupId>javax.servletgroupId>
      <artifactId>javax.servlet-apiartifactId>
      <version>3.1.0version>
      
      <scope>providedscope>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>5.2.10.RELEASEversion>
    dependency>

2.添加tomcat插件

	  <plugin>
        <groupId>org.apache.tomcat.mavengroupId>
        <artifactId>tomcat7-maven-pluginartifactId>
        <version>2.1version>
        <configuration>
          <port>80port>
          <path>/path>
        configuration>
      plugin>

3.创建SpringMVC控制器类

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {
    //设置请求的访问路径
    @RequestMapping("/save")
    //设置响应的返回值类型,方法的返回值类型就是响应的返回值类型
    @ResponseBody
    public String save() {
        System.out.println("user save");
        return "{'info':'successful'}";
    }
}

4.初始化SpringMVC环境,设定SpringMVC加载对应的bean

注:对bean的加载控制做细致划分

package config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;

@Configuration
@ComponentScan(value = "com.example",
        excludeFilters = @ComponentScan.Filter(
                type = FilterType.ANNOTATION,
                value = Controller.class
        )
)
public class SpringConfig {
}
package config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan({"com.example.controller"})
public class SpringMVCConfig {
}

5.初始化Servlet容器,加载SpringMVC环境,并设置SpringMVC技术处理的请求

package config;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;

public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
    //加载SpringMVC容器配置
    @Override
    protected WebApplicationContext createServletApplicationContext() {
        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
        applicationContext.register(SpringMVCConfig.class);
        return applicationContext;
    }
    
    //设置哪些请求归属SpringMVC处理
    @Override
    protected String[] getServletMappings() {
    	// “/”代表所有请求
        return new String[]{"/"};
    }
    
	//加载spring容器配置
    @Override
    protected WebApplicationContext createRootApplicationContext() {
        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
        applicationContext.register(SpringConfig.class);
        return applicationContext;
    }
}

简化的书写方式

package config;


import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

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

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

你可能感兴趣的:(Spring,spring)