其他分享
首页 > 其他分享> > springMVC

springMVC

作者:互联网

SpringMVC

代码

SpringMvcConfig

package com.yang.config;

import com.yang.controller.UserController;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;

@Configuration
@ComponentScan("com.yang.controller")
//@ComponentScan(value = "com.yang",excludeFilters = @ComponentScan.Filter(
//       type = FilterType.ANNOTATION,
//       classes = UserController.class)
//       )
public class SpringMvcConfig {
}

SpringMvc容器

package com.yang.container;

import com.yang.config.SpringConfig;
import com.yang.config.SpringMvcConfig;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
//简化版
public class SpringMvcContainer extends AbstractAnnotationConfigDispatcherServletInitializer {
   //配Spring
   @Override
   protected Class<?>[] getRootConfigClasses() {
       return new Class[0];
  }
   //配springMvc:(ServletConfig)
   @Override
   protected Class<?>[] getServletConfigClasses() {
       return new Class[]{SpringMvcConfig.class};
  }
   //配SpringMvc的请求路径
   @Override
   protected String[] getServletMappings() {
       return new String[]{"/"};
  }
}

/**
* 启动服务器的初始化过程
* 1.服务器启动,执行类SpringMvcContainer
* 2.执行createServletApplicationContext方法,创建了WebApplicationContext对象
* 3.加载SpringMvcConfig
* 4.执行@ComponentScan加载bean
* 5.加载UserController,@RequestMapping方法
* 6.执行getServletMappings()方法,定义所有请求都要通过SpringMvc
*/
/*public class SpringMvcContainer extends AbstractDispatcherServletInitializer {
   //初始化servlet容器,并加载SpringMvc配置,
   @Override
   protected WebApplicationContext createServletApplicationContext() {
       AnnotationConfigWebApplicationContext aca = new AnnotationConfigWebApplicationContext();
       aca.register(SpringMvcConfig.class);
       return aca;
   }
   //设置SpringMvc技术处理请求
   @Override
   protected String[] getServletMappings() {
       return new String[]{"/"};
   }
   //加载Spring配置
   @Override
   protected WebApplicationContext createRootApplicationContext() {
//       AnnotationConfigWebApplicationContext aca = new AnnotationConfigWebApplicationContext();
//       aca.register(SpringConfig.class);
//       return aca;
       return null;
   }
}*/

Controller

package com.yang.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("SpringMvc...");
       return "{'name':'haha'}";
  }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>springMvc</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.10.RELEASE</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
           <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <port>80</port>
                <path>/</path>
            </configuration>
        </plugin>
    </plugins>
</build>

</project>

注意:要<scope>provided</scope>,不然会和Tomcat中的jar包产生冲突

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
 

 

标签:springMVC,springframework,yang,import,org,com,class
来源: https://www.cnblogs.com/yzlworld/p/16500287.html