SringMVC概述&案例开发

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

优点:使用简单,开发便捷(相比于Servlet) 灵活性强
 

使用Servlet技术开发web程序流程

1.创建web工程(Maven结构)

2.设置tomcat服务器,加载web工程(tomcat插件)

3.导入坐标(Servlet)

4.定义处理请求的功能类(Userservlet)5.设置请求映射(配置映射关系)
 

使用SpringMVC技术开发web程序流程

1.创建web工程(Maven结构)

2.设置tomcat服务器,加载web工程(tomcat插件)

3.导入坐标(SpringMVC+Servlet)

4.定义处理请求的功能类(UserController)

5.设置请求映射(配置映射关系)
 



开始写案例

第一步 新建一个maven工程,将main test文件都导好之后,导入坐标和tomcat插件,刷新

<plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <port>80</port>
          <path>/</path>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <packaging>war</packaging>

 <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>
  

第二步 写包我的是分别写了config,controller包,然后将他们放在一个All包下

controller中写有UserController

package All.controller;

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

/**
 * 这个必须得是受SpringMVC管理的bean
 */
@Controller
public class UserController {
    //设置一个处理器对应的方法,/save是它的名称
    @RequestMapping("/save")
    public  void save(){

    }
}

config包写有SpringMvcConfig和ServletContainersInitConfig

ServletContainersInitConfig

package All.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 {
    protected WebApplicationContext createServletApplicationContext() {
        AnnotationConfigWebApplicationContext ctx=new AnnotationConfigWebApplicationContext();
        ctx.register(SpringMvcConfig.class);//加载SpringMVC对应的容器对象
        return ctx;
    }

    protected String[] getServletMappings() {
        return new String[]{"/"};//这个的意思是所有的请求都归SpringMVC处理
    }

    protected WebApplicationContext createRootApplicationContext() {
        return null;
    }
}

补充 后续上面可以这样写

public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

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

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

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

SpringMvcConfig

package All.config;


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

@Configuration
@ComponentScan("All.controller")
public class SpringMvcConfig {
}

第三步 创建tomcat服务器

第四步 运行

 

启动服务器初始化过程
1.服务器启动,执行ServletContainersInitconfig类,初始化web容器
2.执行createServletApplicationContext方法,创建了WebApplicationContext对象

3.加载SpringMvcConfig
4.执行@Componentscan加载对应的bean
5.加载Usercontroller,每个@RequestMapping的名称对应一个具体的方法

6.执行getServletMappings方法,定义所有的请求都通过SpringMVC

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这个人是谁呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值