SpringWeb MVC 详细讲解及开发案例

来源:互联网 发布:网络编辑员招聘启事 编辑:程序博客网 时间:2024/05/23 12:03

一、SpringWeb MVC 知识点讲解

1、MVC 模式简介

M-Model 模型
模型(Model)是负责业务逻辑。包含两层:业务数据和业务处理逻辑。
比如:实体类、DAO、Service都属于模型层。
V-View 视图
视图(View)是负责显示界面。属于视图的组件是不包含业务逻辑和控制逻辑的JSP.
C-Controller 控制器
控制器是模型层M和视图层V之间的桥梁,用于控制流程。

2、什么是Spring Web MVC

Spring Web MVC是Spring框架一个非常重要的功能模块。实现了MVC结构,便于简单、快速开发MVC结构的Web程序。Srping Web MVC 提供的API 封装了Web开发总常用的功能,简化了Web过程。

3、Spring Web MVC的核心组件

Spring Web MVC提供了M、V、C相关的主要实现组件,具体如下:

  • DispatcherServlet(控制器,请求入口)
  • HandlerMapping (控制器,请求派发)
  • Controller(控制器,请求处理流程)
  • ModelAndView(模型,封装业务处理结果和视图)
  • ViewResolver(视图,视图显示处理器)

4、Spirng Web Mvc 处理流程

Springmvc流程图

  • 浏览器向Spring发出请求,请求交给前端控制器DispatcherServlet处理
  • 控制器通过HandlerMapping组件根据请求找到响应的Controller处理
  • 执行Controller组件约定方法处理请求,在约定方法调用模型组件完成业务处理。约定方法可以返回一个ModelAndView对象,封装了处理结果数据和视图名称信息
  • 控制器接收ModelAndView之后,调用ViewResolver组件,定位View(JSP)并传递数据信息,生成响应界面结果

二、Spring Web MVC开发案例

1、Spring Web MVC开发步骤

  • 1.1创建Web工程,导入Spring Web MVC相关开发包
  • 1.2在src下添加Spring的XML配置文件
    名称可以自定义,例如Spring-mvc.xml,配置如下内容:
<!--指定扫描类路径--><context:component-scan base-package="SpringMvcTest"/>    <!--配置HandlerMapping -->    <mvc:annotation-driven/>    <!--配置视图名称解析器 ViewResolver组件-->    <bean id="view" class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <!--前缀-->        <property name="prefix" value="/WEB-INF/"/>        <!-- 后缀 -->        <property name="suffix" value=".jsp"/>    </bean>
  • 1.3在web.xml中配置DispatcherServlet前端控制器组件
    DIspatcherServlet组件在spring mvc中已经提供,只需要配置即可。
    配置DispatcherServlet时,同时指定XML配置文件
    DispatcherServlet控制器配置如下:
  <servlet>    <servlet-name>springmvc</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath:spring-mvc.xml</param-value>    </init-param>    <load-on-startup>1</load-on-startup>  </servlet>
  • 1.4编写Controller组件
  • 1.5编写ModelAndView组件

2、具体案例

1、导包
导入如下jar包:
jar包图
2、在web.xml中配置DispatcherServlet主控制器并初始化SpringMVC配置文件
1、配置DispatcherServlet前置控制器
2、初始化SpringMVC的配置文件
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"         version="3.1">  <!--配置DispatcherServlet-->  <servlet>    <servlet-name>springmvc</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <!--初始化SpringMVC的配置文件-->    <init-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath:spring-mvc.xml</param-value>    </init-param>    <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>springmvc</servlet-name>    <url-pattern>/</url-pattern>  </servlet-mapping></web-app>
3、配置SpringMVC的配置文件
1、在src下添加spring-mvc.xml配置文件
2、指定扫描类路径
3、配置HandlerMapping
4、配置视图名称解析器 ViewResolver组件
<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"       xmlns:tx="http://www.springframework.org/schema/tx"       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"       xmlns:util="http://www.springframework.org/schema/util"       xmlns:jpa="http://www.springframework.org/schema/data/jpa"       xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">    <!--指定扫描类路径-->    <context:component-scan base-package="SpringMvcTest"/>    <!--配置HandlerMapping -->    <mvc:annotation-driven/>    <!--配置视图名称解析器 ViewResolver组件-->    <bean id="view" class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <!--前缀-->        <property name="prefix" value="/WEB-INF/"/>        <!-- 后缀 -->        <property name="suffix" value=".jsp"/>    </bean></beans>
4、在WEB-INF下创建hello.jsp文件,如下
<html><body><h2>Hello SpringMVC!</h2></body></html>
5、编写controller
@Controllerpublic class SpringMvcController {    @RequestMapping("/index")    public String Hello() {        return "hello";    }
0 0