一、什么是Spring MVC?

         1.Spring MVC 是目前最主流的MVC框架之一。

         2.Spring MVC 通过MVC的注解,让POJO处理请求。

         3.控制器无需实现任何接口。

         4.是Spring提供的一个基于MVC设计模式优秀WEB开发框架,本质上相当于Servlet。

 

二、Spring MVC的环境搭建步骤(idea)

          1.创建项目

              (1) 点击file 新建一个项目  这里的Project 相当于 eclipse的工作空间。module相当于eclipse的项目。按需要新建

  Spring MVC的Hello

(2)弹出的框中 勾选 Spirng SpringMVC  WEB Application。然后点击next

 (这里会有下载jar包 ,或者导入本地的jar包)

Spring MVC的Hello

                 相应的jar包下载链接 :链接: https://pan.baidu.com/s/1Z7_rRJxoINWS7ZQFc7Bi5Q 提取码: dxnt

           (3)   我选择的是本地,然后选择jar包的文件。命名。下一步。得到如下目录结构

                    Spring MVC的Hello

 

二、配置文件。

          (1)配置tomcat (我已经配置过一个。)

                             Spring MVC的HelloSpring MVC的HelloSpring MVC的HelloSpring MVC的Hello

                

              (2)修改web.xml

                                            Spring MVC的Hello

        (3)修改 dispater-servlet.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp"/>
    </bean>
    <!-- 包的自动扫描-->
    <context:component-scan base-package="hello"/>
    <!--Spring mvc 的自动扫描-->
    <mvc:annotation-driven/>
</beans>

          (4)新建包和类 以及 jsp文件

        Spring MVC的Hello

            (5)类中内容修改

                    

package hello;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class hello {
    @RequestMapping("/hello.html")
     public String hello(){
        System.out.println("学框架还是Spring MVC好");
        return "index";
    }
}

              (6) jsp文件修改

                Spring MVC的Hello

三、项目运行 

                 1.启动tomcat 

                  2.浏览器输入 : localhost:8080/hello.html

Spring MVC的Hello

                       3.控制台相应输出

 Spring MVC的Hello

 

四、总结

         M :Model (模型)

         V:View     (视图)

         C:Controller(控制器)

Spring mvc 基于请求驱动的Web框架,并且也使用了前端控制器模式来进行分析,再根据请求映射规则分发给相应的页面控制器(处理器)

          1.用户发送请求到前端控制器(DispatcherServlet)然后根据请求信息来决定选择哪个页面控制器来处理,并把请求转发,

           2.页面控制器接收到请求后,进行业务处理,处理完毕后返回一个ModelAndView(模型数据和逻辑视图名称)

          3.前端控制器收回控制权,跟据返回的逻辑视图名称,选择相应的视图。并把数据传入将视图渲染

          4.将相应结果返回给用户。                        

                                                                                          --
                                                                                                     积土而为山,积水而为海。

 

相关文章:

  • 2021-07-02
  • 2022-02-12
  • 2021-06-02
  • 2021-12-26
  • 2021-09-26
  • 2021-12-14
猜你喜欢
  • 2021-12-05
  • 2021-12-03
  • 2021-10-07
  • 2021-12-02
  • 2021-04-24
  • 2022-12-23
相关资源
相似解决方案