Spring框架的了解SpringIOC的部分内容请阅读23-Java-Spring框架(一)

SpringAOP的了解运用及原理等部分内容请阅读25-Java-Spring框架(三)

三、Spring Web MVC(Model View Controller)

  1.SpringMVC了解

      Spring提供了一个Web MVC框架,便于开发MVC结构的Java Web程序。Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。

    Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的Spring

    MVC框架或集成其他MVC开发框架。

      通过策略接口,Spring 框架是高度可配置的,而且包含多种视图技术,例如 JavaServer Pages(JSP)技术、Velocity、Tiles、iText和POI。Spring MVC 框架

    并不知道使用的视图,所以不会强迫开发者只使用 JSP 技术。Spring MVC 分离了控制器、模型对象、过滤器以及处理程序对象的角色,这种分离让它们更容易进行

    定制。

         SpringMVC框架中提供了一个控制器(DispatcherServlet),负责接收客服端的请求,然后将请求分发到不同的处理器进行业务请求,最后由控制器完成转发动作。

  2.SpringMVC相关组件

      (1)DispatcherServlet(前端控制器,请求入口)

      (2)HandlerMapping(控制器,请求派发)

      (3)Controller(控制器,请求处理流程)

      (4)ModelAndView(模型,封装处理结果和视图)

      (5)ViewResolver(视图,视图显示处理器)

  3.SpringMVC请求流程:SpringMVC是通过传统的Servlet来实现对框架源代码的封装以及整个流程的控制

              

      (1)浏览器向服务器(tomcat)发送http请求,web服务器对http请求进行解析,解析后如果URL地址匹配了DispatcherSerlvet的映射路径(serlvet-mapping),

        web容器就会将请求交给DispatcherServlet来处理。

      (2)DispatcherServet接收到请求后,再次对URL进行解析,得到URI,然后调用相应的方法得到HandlerMapping,再根据URI调用这个对象相对应的方法得

        到Handler,此时并不会操作它,需要调用HandlerAdapter对Hander进行调用以及控制。

      (3)DispatcherServlet根据得到的Handler对象选择合适的HandlerAdapter创建实例,执行拦截器中的preHander()方法

      (4)Handler执行完毕后返回一个ModeAndView对象给DispatcherServlet。

      (5)这个ModeAndView只是一个逻辑视图,并不是真正的视图,DispatcherServlet通过ViewResolver视图解析器将逻辑视图转化成一个真正的视图

      (6)DispatcherServlet通过Model将ModeAndView中得到的数据解析后渲染视图,将得到的最终的视图通过http响应返回客服端。

  4.SpringMVC的运用一(通过XML配置的方式)

      第一步:搭建SpringMVC环境

         (1)创建web项目

         (2)添加SpringIOC和web MVC相关jar包(想要如下jar包的网友评论留言私聊)

            ioc相关jar包:

              aopalliance-1.0.jar

              aspectjweaver-1.5.3.jar

              spring-aop-4.1.6.RELEASE.jar

              spring-aspects-4.1.6.RELEASE.jar

            webmvc相关jar包:

              spring-web-4.1.6.RELEASE.jar

              spring-webmvc-4.1.6.RELEASE.jar

         (3)添加Spring配置文件applicationContext.xml(也可以用注解的方式代替此步骤)

         (4)在web.xml中配置DispatcherServlet前端控制器组件

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>Hello.jsp</welcome-file>
</welcome-file-list> <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:applicationContext.xml</param-value>
</init-param> <load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>spring</servlet-name>
<!-- 拦截以.do结尾的请求 -->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

      第二步:开发SpringMVC

         (1)设计请求到响应的处理流程

         (2)编写Controller组件

 package com.springmvc.Controller;

 import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller; //XML配置
//首先创建ControllerClass 实现Controller接口返回ModelAndView
public class ControllerClass implements Controller{ //该方法是约定的处理请求的方法,请求进入Controller对象后自动调用该方法
@Override
public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception {
System.out.println("进入Controller组件"); //接下来往JSP页面传递一个消息并跳转到Hello.jsp页面
ModelAndView mav = new ModelAndView();
mav.setViewName("Hello");//指定视图名称
mav.getModel().put("msg","I am happy!");
return mav;
}
}

         (3)JSP或html页面

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Hello.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page"> </head> <body>
<h1>hi,Spring</h1>
<h1>${msg }</h1>
</body>
</html>

         (4)在applicationContext.xml中配置Controller组件(也可以用注解的方式代替此步骤)

         (5)在applicationContext.xml中配置HandlerMapping组件(也可以用注解的方式代替此步骤)

         (6)在applicationContext.xml中配置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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置Contorller组件 -->
<bean id="Controller" class="com.springmvc.Controller.ControllerClass"></bean> <!-- 配置HandlerMapping组件 -->
<bean id="simpleurlhandlermapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.do">Controller</prop>
</props>
</property>
</bean> <!-- 配置ViewResolver视图解析器 -->
<!-- 完整的页面路径:前缀+视图名称+后缀 -->
<bean id="ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

      第三步:测试运行

          

  5.SpringMVC的运用二(通过注解的方式)

      第一步:搭建SpringMVC环境

         (1)创建web项目

         (2)添加SpringIOC、AOP和web MVC相关jar包(想要如下jar包的网友评论留言私聊)

            ioc相关jar包:

              aopalliance-1.0.jar

              aspectjweaver-1.5.3.jar

              spring-aop-4.1.6.RELEASE.jar

              spring-aspects-4.1.6.RELEASE.jar

            aop相关jar包:

              aopalliance-1.0.jar

              aspectjweaver-1.5.3.jar

              spring-aop-4.1.6.RELEASE.jar

              spring-aspects-4.1.6.RELEASE.jar

            webmvc相关jar包:

              spring-web-4.1.6.RELEASE.jar

              spring-webmvc-4.1.6.RELEASE.jar

         (3)添加Spring配置文件applicationContext.xml(也可以用注解的方式代替此步骤)

         (4)在web.xml中配置DispatcherServlet前端控制器组件(同运用一的web.xml)

      第二步:开发SpringMVC

         (1)设计请求到响应的处理流程

         (2)编写Controller组件

 package com.springmvc.Controller;

 import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller//此处相当于继承了Controller接口的作用
public class ControllerClass { @RequestMapping("/hello.do")//此处等价于applicationContext.xml中的配置HandlerMapping组件
public ModelAndView hello(){
System.out.println("进入Controller组件"); //接下来往JSP页面传递一个消息并跳转到Hello.jsp页面
ModelAndView mav = new ModelAndView();
mav.setViewName("Hello");
mav.getModel().put("msg", "I am pretty good");
return mav;
}
}

         (3)JSP或html页面(同运用一的Hello.jsp)

         (4)在applicationContext.xml中配置handler组件

         (5)在applicationContext.xml中配置组件扫描

         (6)在applicationContext.xml中配置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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置Handler简配 -->
<mvc:annotation-driven/> <!-- 配置组件扫描 -->
<context:component-scan base-package="com.springmvc"/> <!-- 配置ViewResolver视图解析器 -->
<!-- 完整的页面路径:前缀+视图名称+后缀 -->
<bean id="ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

       第三步:测试运行

           

24-Java-Spring框架(二)的更多相关文章

  1. 基于java spring框架开发部标1078视频监控平台精华文章索引

    部标1078视频监控平台,是一个庞杂的工程,涵盖了多层协议,部标jt808,jt809,jt1078,苏标Adas协议等,多个平台功能标准,部标796标准,部标1077标准和苏标主动安全标准,视频方面 ...

  2. 《Java Spring框架》SpringXML配置详解

    Spring框架作为Bean的管理容器,其最经典最基础的Bean配置方式就是纯XML配置,这样做使得结构清晰明了,适合大型项目使用.Spring的XML配置虽然很繁琐,而且存在简洁的注解方式,但读懂X ...

  3. [Java]Spring框架

    在这里学习Spring框架: >>spring&struts框架学习 >>spring >>Java回顾之Spring基础 >>IBM Java ...

  4. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:SSM(Spring+Spring MVC+MyBatis)框架整合搭建详细步骤

    因为 Spring MVC 是 Spring 框架中的一个子模块,所以 Spring 与 SpringMVC 之间不存在整合的问题.实际上,SSM 框架的整合只涉及 Spring 与 MyBatis ...

  5. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:SSH框架(Struts2+Spring+Hibernate)搭建整合详细步骤

    在实际项目的开发中,为了充分利用各个框架的优点,通常都会把 Spring 与其他框架整合在一起使用. 整合就是将不同的框架放在一个项目中,共同使用它们的技术,发挥它们的优点,并形成互补.一般而言,在进 ...

  6. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring使用AspectJ开发AOP基于XML和基于Annotation

    AspectJ 是一个基于 Java 语言的 AOP 框架,它扩展了 Java 语言.Spring 2.0 以后,新增了对 AspectJ 方式的支持,新版本的 Spring 框架,建议使用 Aspe ...

  7. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring JDK动态代理

    JDK 动态代理是通过 JDK 中的 java.lang.reflect.Proxy 类实现的.下面通过具体的案例演示 JDK 动态代理的使用. 1. 创建项目 在 MyEclipse 中创建一个名称 ...

  8. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring DI(依赖注入)的实现方式属性注入和构造注入

    依赖注入(Dependency Injection,DI)和控制反转含义相同,它们是从两个角度描述的同一个概念. 当某个 Java 实例需要另一个 Java 实例时,传统的方法是由调用者创建被调用者的 ...

  9. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring IoC容器BeanFactory和ApplicationContext

    IoC 是指在程序开发中,实例的创建不再由调用者管理,而是由 Spring 容器创建.Spring 容器会负责控制程序之间的关系,而不是由程序代码直接控制,因此,控制权由程序代码转移到了 Spring ...

  10. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring体系结构详解

    Spring 框架采用分层架构,根据不同的功能被划分成了多个模块,这些模块大体可分为 Data Access/Integration.Web.AOP.Aspects.Messaging.Instrum ...

随机推荐

  1. 30.3 Collections 集合体系的工具类

    /* * Collections: * 面试题:Collection和Collections有什么区别? * Collection是集合体系的最顶层,包含了集合体系的共性 * Collections是 ...

  2. 《MySQL实战45讲》学习笔记4——MySQL中InnoDB的索引

    索引是在存储引擎层实现的,且在 MySQL 不同存储引擎中的实现也不同,本篇文章介绍的是 MySQL 的 InnoDB 的索引. 下文将以这张表为例开展. # 创建一个主键为 id 的表,表中有字段 ...

  3. 一个spring 基本知识的微博(怎么加载多个xml、多个property文件、aop配置、监视器)

    http://blog.sina.com.cn/s/blog_61c5866d0100ev44.html

  4. AJ学IOS 之微博项目实战(6)导航控制器NavigationController 的滑动回退功能实现

    AJ分享,必须精品 一:效果 第二篇里面写了怎样自定义navigation实现自定义的导航控制器左右按钮样式,但是当我们自己实现后,系统自带的向右边滑动来实现回退的功能就不能用了. 这里主要实现滑动回 ...

  5. Laravel 5.8 RCE 分析

    原帖地址 : https://xz.aliyun.com/t/6059 Laravel 代码审计 环境搭建 composer create-project --prefer-dist laravel/ ...

  6. 容易忽略的CSS3属性

    flex布局模型 1. flex-direction: row |  row-reverse | column | column-reverse flex-direction 属性决定主轴的方向(即项 ...

  7. ubuntu16.04-交叉编译opencv3.4.6

    0.前言 在要移植opecv和SeetaFaceEngine-master到ARM板子上运行的所有步骤之前,有几点需要注意的: 查看板子运行的Kernel版本 交叉编译工具链的gcc版本,关键就是工具 ...

  8. orcale 多列转一行显示

    强大的数据库有个自带函数wm_concat() wm_concat()这个函数放的是需要汇总的列 select wm_concat(name) name  from tablename

  9. ES6中对数值的扩展

    上一节和大家学习了字符串的扩展,这一节轮到了数值,我们一起来学习数值的扩展. 剧透一下,这一节并不会很烧脑,都是介绍类的知识讲解,理解性的东西不多,初学者们也可以愉快地看完. 传统的写法 在介绍数值的 ...

  10. JavaScript之浅谈内存空间

    JavaScript之浅谈内存空间 JavaScipt 内存自动回收机制 在JavaScript中,最独特的一个特点就是拥有自动的垃圾回收机制(周期性执行),这也就意味者,前端开发人员能够专注于业余, ...