Spring MVC的Controller用于处理用户的请求。Controller相当于Struts 1里的Action,他们的实现机制、运行原理都类似

Controller是个接口,一般直接继承AbstrcatController,并实现handleRequestInternal方法。handleRequestInternal方法相当于Struts 1的execute方法

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class CatController extends AbstractController{
      private ICatService catService;
      //setter、getter略

      protected ModelAndView handleRequestInternal(HttpServletRequestrequest,HttpServletResponse response) throws Exception{
             String action =request.getParameter("action");
             if("list".equals(action)){
                    return this.list(request,response);
             }
      }

      protected ModelAndView list(HttpServletRequestrequest,HttpServletResponse response) throws Exception{
             List<Cat> catList =catService.listCat();
             request.setAttribute("catList", catList);
             return new ModelAndView("cat/listCat");
      }

}

Spring MVC没有内置数据的封装,开发者可以自己封装数据转换代码

Spring MVC独特的地方在于view层的处理上。handleRequestInternal返回ModelAndView对象,可以看做是对JSP对象的封装。ModelAndIView直接接受JSP页面的路径。例如参数"cat/listCat",只是JSP路径的一部分,实际完整的路径是"WEB-INF/jsp/cat/catList.jsp",路径前后的部分是配置在配置文件中的

除了制定JSP路径,ModelAndView还可以直接传递Model对象到View层,而不用事先放到request中,例如new ModelAndView("cat/listCat","cat",cat),如果传递多个参数,可以使用Map,如

Map map = newHashMap();
map.put("cat",cat);
map.put("catList",catList);
return new ModelAndView("cat/listCat",map);

一般使用一个独立的xml文件如spring-action.xml专门配置web相关的组件

<?xml version= "1.0" encoding="UTF-8"?>
<!DCTYPEbeans PUBLIC "-//SPRING//DTD BEAN//EN"
  "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
      <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
             <property name="prefix">
                    <value>/WEB-INF/jsp/</value><!--  JSP前缀-->
             </property>
             <property name="suffix">
                    <value>.jsp</value>                 <!--  JSP后缀-->
             </property>

      <!-- 配置URL Mapping-->
      <bean id="urlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandleMapping">
             <property name="mappings">
                    <props><!—Controller的URL会被配置成"cat.mvc"-->
                           <prop key="cat.mvc">catController</prop>
                    <props>
             </property>
      </bean>
      <bean id="catController" class="com.clf.spring.CatController">
             <property name="catService" ref="catService"></property>
      </bean>
</beans>

web.xml配置
<context-param><!--  Spring配置文件的位置-->
      <param-name>contextConfigLocation</param-name>
      <param-value>
             /WEB-INF/classes/applicationContext.xml,
             /WEB-INF/classes/spring-action.xml
      </param-value>
</context-param>

<listener><!--  使用Listener加载Spring配置文件-->
      <listener-class>
             org.springframework.web.context.ContextLoaderListener
      </listener-class>
</listener>

<servlet><!--  spring分发器-->
      <servlet-name>spring</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>/WEB-INF/classes/spring-action.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup><!--  启动时加载-->
</servlet>

<servlet-mapping>
      <servlet-name> spring</servlet-name>
      <url>*.mvc</url>
</servlet-mapping>

如果一个Controller要处理多个业务逻辑,可以使用MultiActionController,相当于Struts 1中的DispatchAction,能根据某个参数将不同的请求分发到不同的方法上

import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class CatController extends AbstractController{
      private ICatService catService;
      //setter、getter略

      protected ModelAndView add(HttpServletRequestrequest,HttpServletResponse response) throws Exception{
             ……
             return new ModelAndView("cat/addCat");
      }

      protected ModelAndView list(HttpServletRequestrequest,HttpServletResponse response) throws Exception{
             List<Cat> catList =catService.listCat();
             request.setAttribute("catList", catList);
             return new ModelAndView("cat/listCat");
      }

}

配置到spring-action.xml

<bean id="paraMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
      <property name="paramName">
             <value>action</value><!--  配置分发参数-->
      </property>
      <property name="defaultMethodName">
             <value>list</value><!--  配置默认的执行方法-->
      </property>
</bean>

<bean id="urlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandleMapping">
             <property name="mappings">
                    <props>
                           <prop key="cat.mvc">catController</prop><!--  访问"cat.mvc"则交给catController处理-->
                           <prop key="catMulti.mvc">catMultiController</prop><!--  访问"catMulti.mvc"则交给catMultiController处理-->
                    <props>
             </property>
      </bean>

      <bean id="catController" class="com.clf.spring.CatMultiController">
             <property name="catService" ref="catService"></property>
      </bean>

      <bean id="catMultiController" class="com.clf.spring.CatController">
             <property name="catService" ref="catService"></property>
      </bean>

Spring之MVC模块的更多相关文章

  1. Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework中的spring web MVC模块

    spring framework中的spring web MVC模块 1.概述 spring web mvc是spring框架中的一个模块 spring web mvc实现了web的MVC架构模式,可 ...

  2. Spring REST实践之Spring Web MVC

    Spring概要 Spring Framework提供了依赖注入模型和面向切面编程,简化了基础型代码的编写工作以及更好的能够与其它框架和技术整合起来.Spring Framework由data acc ...

  3. Spring 4 MVC+Hibernate 4+MySQL+Maven使用注解集成实例

    Spring 4 MVC+Hibernate 4+MySQL+Maven使用注解集成实例 转自:通过注解的方式集成Spring 4 MVC+Hibernate 4+MySQL+Maven,开发项目样例 ...

  4. spring的MVC执行原理

    spring的MVC执行原理 1.spring mvc将所有的请求都提交给DispatcherServlet,它会委托应用系统的其他模块负责对请求 进行真正的处理工作. 2.DispatcherSer ...

  5. Spring Web MVC(一)

    [toc] 概述 Spring的web框架围绕DispatcherServlet设计. DispatcherServlet的作用是将请求分发到不同的处理器. Spring的web框架包括可配置的处理器 ...

  6. 淘淘商城之spring web mvc架构

    一.什么是springmvc springmvc是spring框架的一个模块,springmvc和spring无需通过中间整合层进行整合: springmvc是一个基于mvc的web框架   二.mv ...

  7. 1.Spring——七大主要模块

    Spring有七大功能模块,分别是Spring Core,AOP,ORM,DAO,MVC,WEB,Content. 下面分别简单介绍: 1.Spring Core Core模块是Spring的核心类库 ...

  8. 一头扎进Spring之---------Spring七大核心模块

    Spring七大核心模块 核心容器(Spring Core) 核心容器提供Spring框架的基本功能.Spring以bean的方式组织和管理Java应用中的各个组件及其关系.Spring使用BeanF ...

  9. Spring官方文档翻译——15.1 介绍Spring Web MVC框架

    Part V. The Web 文档的这一部分介绍了Spring框架对展现层的支持(尤其是基于web的展现层) Spring拥有自己的web框架--Spring Web MVC.在前两章中会有介绍. ...

随机推荐

  1. springboot全局异常处理

    @Slf4j@ControllerAdvicepublic class RestExceptionHandler extends ResponseEntityExceptionHandler { @E ...

  2. angularjs中使用 <input type="file">标签实现一次最多上传5张图片

    前期准备: 1.angular.js 2.bootstrap.css 具体如何实现呢?请看下面代码哈哈哈. 在angular项目中,如果要给<input type="file" ...

  3. 词向量:part 1 WordNet、SoW、BoW、TF-IDF、Hash Trick、共现矩阵、SVD

    1.基于知识的表征 如WordNet(图1-1),包含同义词集(synonym sets)和上位词(hypernyms,is a关系). 存在的问题: 作为资源来说是好的,但是它失去了词间的细微差别, ...

  4. [HNOI 2009]有趣的数列

    Description 我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件: (1)它是从1到2n共2n个整数的一个排列{ai}: (2)所有的奇数项满足a1<a3<…&l ...

  5. BZOJ 3938 Robot

    Description 小q有n只机器人,一开始他把机器人放在了一条数轴上,第i只机器人在ai的位置上静止,而自己站在原点.在这 之后小q会执行一些操作,他想要命令一个机器人向左或者向右移动x格.但是 ...

  6. ●BZOJ 3529 [Sdoi2014]数表

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3529 题解: 莫比乌斯反演. 按题目的意思,令$f(i)$表示i的所有约数的和,就是要求: ...

  7. UVALive - 3026:Period

    用KMP里面的next数组即可,原理就是next数组的原理 #include<cstdio> #include<cstdlib> #include<algorithm&g ...

  8. udacity/CarND-Path-Planning-Project 工程详细配置过程——吐血整理

    本人原创,转载请注明地址 学习udacity/CarND-Path-Planning-Project 工程过程 1.首先登陆 jeremy-shannon/CarND-Path-Planning-Pr ...

  9. python2.7入门---变量类型

      这篇文章呢,主要是用来记录python中的变量类型学习内容的.接下来就来看一下变量类型,那么什么是变量呢.变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间.基于变量的数据类型,解 ...

  10. GC机制

    java虚拟机中的垃圾回收机制是,一个类,当该对象没有更多的应用指向它时,就会被垃圾回收器给回收,从而释放资源.该机制不可以程序员手动调用去回收某个对象,系统自动会去调用,当然程序员可以建议垃圾回收器 ...