24-Java-Spring框架(二)

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前端控制器组件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="3.0" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 7   <display-name></display-name>    
 8   <welcome-file-list>
 9     <welcome-file>Hello.jsp</welcome-file>
10   </welcome-file-list>
11   
12   <servlet>
13       <servlet-name>springmvc</servlet-name>
14       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
15       
16       <init-param>
17           <param-name>contextConfigLocation</param-name>
18           <param-value>classpath:applicationContext.xml</param-value>
19       </init-param>
20       
21       <load-on-startup>1</load-on-startup>
22   </servlet>
23   
24   <servlet-mapping>
25       <servlet-name>spring</servlet-name>
26       <!-- 拦截以.do结尾的请求 -->
27       <url-pattern>*.do</url-pattern>
28   </servlet-mapping>
29 </web-app>

      第二步:开发SpringMVC

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

         (2)编写Controller组件

 1 package com.springmvc.Controller;
 2 
 3 import javax.servlet.http.HttpServletRequest;
 4 import javax.servlet.http.HttpServletResponse;
 5 
 6 import org.springframework.web.servlet.ModelAndView;
 7 import org.springframework.web.servlet.mvc.Controller;
 8 
 9 //XML配置
10 //首先创建ControllerClass 实现Controller接口返回ModelAndView
11 public class ControllerClass implements Controller{
12     
13     //该方法是约定的处理请求的方法,请求进入Controller对象后自动调用该方法
14     @Override
15     public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception {
16         System.out.println("进入Controller组件");
17         
18         
19         //接下来往JSP页面传递一个消息并跳转到Hello.jsp页面
20         ModelAndView mav = new ModelAndView();
21         mav.setViewName("Hello");//指定视图名称
22         mav.getModel().put("msg","I am happy!");
23         return mav;
24     }
25 }

         (3)JSP或html页面

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     <title>My JSP 'Hello.jsp' starting page</title>
12     <meta http-equiv="pragma" content="no-cache">
13     <meta http-equiv="cache-control" content="no-cache">
14     <meta http-equiv="expires" content="0">    
15     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
16     <meta http-equiv="description" content="This is my page">
17 
18   </head>
19   
20   <body>
21         <h1>hi,Spring</h1>
22         <h1>${msg }</h1>
23   </body>
24 </html>

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

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

         (6)在applicationContext.xml中配置ViewResolver组件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:aop="http://www.springframework.org/schema/aop"
 6        xmlns:tx="http://www.springframework.org/schema/tx"
 7        xmlns:jdbc="http://www.springframework.org/schema/jdbc"
 8        xmlns:jee="http://www.springframework.org/schema/jee"
 9        xmlns:mvc="http://www.springframework.org/schema/mvc"
10        xmlns:util="http://www.springframework.org/schema/util"
11        xsi:schemaLocation="
12             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
13             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
14             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
15             http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
16             http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
17             http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
18             http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
19             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
20         
21         <!-- 配置Contorller组件 -->
22         <bean id="Controller" class="com.springmvc.Controller.ControllerClass"></bean>
23         
24         <!-- 配置HandlerMapping组件 -->
25         <bean id="simpleurlhandlermapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
26             <property name="mappings">
27                 <props>
28                     <prop key="/hello.do">Controller</prop>
29                 </props>
30             </property>
31         </bean>
32         
33         <!-- 配置ViewResolver视图解析器 -->
34         <!-- 完整的页面路径:前缀+视图名称+后缀 -->
35         <bean id="ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
36             <!-- 前缀 -->
37             <property name="prefix" value="/"></property>
39             <!-- 后缀 -->
40             <property name="suffix" value=".jsp"></property>
41         </bean>
42 </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组件

 1 package com.springmvc.Controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.servlet.ModelAndView;
 6 
 7 @Controller//此处相当于继承了Controller接口的作用
 8 public class ControllerClass {
 9     
10     @RequestMapping("/hello.do")//此处等价于applicationContext.xml中的配置HandlerMapping组件
11     public ModelAndView hello(){
12         System.out.println("进入Controller组件");
13         
14         //接下来往JSP页面传递一个消息并跳转到Hello.jsp页面
15         ModelAndView mav = new ModelAndView();
16         mav.setViewName("Hello");
17         mav.getModel().put("msg", "I am pretty good");
18         return mav;
19     }
20 }

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

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

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

         (6)在applicationContext.xml中配置ViewResolver组件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:aop="http://www.springframework.org/schema/aop"
 6        xmlns:tx="http://www.springframework.org/schema/tx"
 7        xmlns:jdbc="http://www.springframework.org/schema/jdbc"
 8        xmlns:jee="http://www.springframework.org/schema/jee"
 9        xmlns:mvc="http://www.springframework.org/schema/mvc"
10        xmlns:util="http://www.springframework.org/schema/util"
11        xsi:schemaLocation="
12             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
13             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
14             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
15             http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
16             http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
17             http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
18             http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
19             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
20         
21         <!-- 配置Handler简配 -->
22         <mvc:annotation-driven/>
23         
24         <!-- 配置组件扫描 -->
25         <context:component-scan base-package="com.springmvc"/>
26         
27         <!-- 配置ViewResolver视图解析器 -->
28         <!-- 完整的页面路径:前缀+视图名称+后缀 -->
29         <bean id="ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
30             <!-- 前缀 -->
31             <property name="prefix" value="/"></property>
32             <!-- 后缀 -->
33             <property name="suffix" value=".jsp"></property>
34         </bean>
35 </beans>

       第三步:测试运行

           

 

posted @ 2020-05-05 03:44  我只是一个码农  阅读(581)  评论(0编辑  收藏  举报