SpringMVC01(SpringMVC的简介+入门案例)

一、大纲

二、springMVC的开发流程

三、SpringMVC的操作"思路"

3.1、对SpringMVC(n+1)代码块

图片中的"config包"下的内容一个springMVC只要配置一次,"controller包"、"service包"、"dao包"下是需要写流程的

3.2 代码块

"只需要配置一次的"Config/ServletContainersInitConfig
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
    //加载springmvc配置类
    protected WebApplicationContext createServletApplicationContext() {
        //初始化WebApplicationContext对象
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        //加载指定配置类:自己写的"接收Bean"的Config
        ctx.register(SpringMvcConfig.class);
        return ctx;
    }

    //设置由springmvc控制器处理的请求映射路径
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    //加载spring配置类
    protected WebApplicationContext createRootApplicationContext() {
        return null;
    }
}
"只需要配置一次的"Config/SpringMvcConfig
@Configuration
//@ComponentScan("com.itheima.springbootmvc.controller"),写的是:你需要Bean/实体化的controller包的路径
@ComponentScan("com.itheima.springbootmvc.controller")
public class SpringMvcConfig {
}
n部分的:controller/UserController
@Controller
@ResponseBody //这个注解:是把retune的内容就全部输出,没有转化页面的形式
public class UserController {
    @RequestMapping("/save")
    public String save(){
       return "{info:SpringMVC_save}";
    }
}
SpringMVC的入门案例pox.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.7-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.itheima</groupId>
    <artifactId>springbootMVC</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springbootMVC</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- 配置Tomcat插件 -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <!-- 配置Tomcat监听端口 -->
                    <port>8080</port>
                    <!-- 配置项目的访问路径(Application Context) -->
                    <path>/</path>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

posted on 2022-12-01 22:49  陈嘻嘻-  阅读(22)  评论(0编辑  收藏  举报

导航