springMVC

2022/7/21 6:25:56

本文主要是介绍springMVC,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

SpringMVC

  • SpringMVC是一种基于java实现的MVC模型的轻量级web框架

  • 优点: 1. 使用简单,开发便捷 2. 灵活性强

  • Controller加载控制与业务bean加载控制

    *扫描包的时候排除掉Controller
    @ComponentScan(value = "com.yang",excludeFilters = @ComponentScan.Filter(
            type = FilterType.ANNOTATION,
            classes = UserController.class)  
            )
        
     *写具体的包扫描
     @ComponentScan({"com.yang.service","com.yang.dao"})

     

代码

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的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程