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. Python常见数据结构-Set集合

    集合基本特点 集合是无序的,且集合内无重复值. 集合不支持索引和切片 集合常见操作及方法 s1 = {1,2,3} s2 = {2,3,4} s1.add(4) #.add()方法添加一个元素 s1. ...

  2. CSS 布局水平 & 垂直对齐

    元素居中对齐 margin: auto; 文本居中对齐 text-align: center; 图片居中对齐 要让图片居中对齐, 可以使用 margin: auto; 并将它放到 块 元素中 左右对齐 ...

  3. .Net Core结合AspNetCoreRateLimit实现限流

    前言 相信使用过WebApiThrottle的童鞋对AspNetCoreRateLimit应该不陌生,AspNetCoreRateLimit是一个ASP.NET Core速率限制的解决方案,旨在控制客 ...

  4. char类型及ASCII码之间比较

    在JAVA中,char类型可以直接运算,char在ASCII等字符编码表中有对应的数值对char类型字符运行时,直接当做ASCII表对应的整数来对待 参考 https://blog.csdn.net/ ...

  5. termux上安装lxml失败

    想试试在手机上随时可以接收到各个平台上最新的消息,于是打算在qpython上写个python的爬虫,安装bs4后,在安装lxml时出现了问题. qpython失败后,改用termux试一下,结果出现了 ...

  6. Java成长第三集--基础重点详细说明

    接上篇文章,继续阐述相关的重点基础知识,话不多说! 一.Java中equals()和“==”区别 1.对于8种基础数据类型,使用“=="比较值是否相等: 2.对于复合数据类型(类),使用eq ...

  7. Gallery实现图片拖动切换

    Gallery中文意思为画廊,通过Gallery能够实现用手指在屏幕上滑动实现图片的拖动.效果如下: 上面,为了学习了解,只用了android默认的Icon图片. 主程序中创建了一个继承自BaseAd ...

  8. 【Java】【常用类】 Arrays工具类 源码学习

    虽然在数组的随笔中有说过,但实际上应该仔细深入一下源码进行分析 源码没有想象中的高大上,代码终究还是写给人看的,可读性大于执行性 最小阵列排序:1 乘 2的13次方 =  8192 学识浅薄,暂时还不 ...

  9. python白帽子/黑客/实战编程教程

    Python搜索爬虫抓取超高清视频教程_第一期Python搜索爬虫抓取超高清视频教程_第二期Python搜索爬虫抓取视频教程_第三期Python搜索爬虫抓取视频教程_第四期Python搜索引擎爬虫抓取 ...

  10. 【错误】python百分号冲突not enough arguments for format string

    query = "SELECT * FROM devices WHERE devices.`id` LIKE '%{}%'".format("f2333") d ...