先说一下springMVC的工作机制

1.springmvc把 所有的请求都提交给DispatcherServlet,它会委托应用系统的其他模块负责对请求进行真正的处理工作。

2.DispatcherServlet查 询一个或多个HandlerMapping,找到处理请求的Controller.

3.DispatcherServlet把 请求提交到目标Controller

4.Controller进 行业务逻辑处理后,会返回一个ModelAndView

5.Dispathcher查 询一个或多个ViewResolver视图解析器,找到ModelAndView对 象指定的视图对象

6.视 图对象负责渲染返回给客户端。

<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!--设置访问名  就是访问对应的名称的时候就是用项目名+/test/helloworld (这里针对的是实现Controller 接口覆写handleRequest方法的类)-->
<bean name="/test/helloworld" class="controller.HelloworldController"></bean>
<bean name="/test/my" class="controller.Mycontroller"></bean>

<!--指定配置方法解析器 bean="ParamMethodResolver" () -->
<bean name="/test/multi" class="controller.MultiController">
<property name="methodNameResolver">
<ref bean="ParamMethodResolver"></ref>
</property>
</bean>

<!-- 多配置:访问控制器多个方法j解析配置 method请求的方法名称-->
<bean name="ParamMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="method"></property>
</bean>

<!-- 视图解析器  在对应的类中 return new ModelAndView("/multi","method",name); 会跳转到对应multi.jsp文件下,没有在WebRoot下的需加上包名--> 
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

<!-- MVC标签 表示 静态资源 访问不经过springMVC框架 -->
<mvc:resources location="/img/" mapping="/img/**"/>

springMVC 3.0以后引入注解方式配置简化了很多(上面的配置都可以不用了)

<!-- 注解扫描包 com.controller这个包下面的包使用注解有效 -->
<context:component-scan base-package="com.controller"></context:comp1onent-scan>
<!-- 开启注解驱动 -->
<mvc:annotation-driven/>

<!-- 视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

</beans>

上面spring3.0以前配置实例对应的类
public class Mycontroller implements Controller {

public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("------------hello world--------");
String name="屌丝";
Map<String,Object> map=new HashMap();
map.put("map1", "haha");
map.put("map2", "xixi");
map.put("map3", "heihei");

//相当于request中保存的name属性
return new ModelAndView("/welcome","map",map);
}

}

//一个controller控制器中写多个方法
public class MultiController extends MultiActionController {
//method1
public ModelAndView add(HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("------MultiController add----------");
String name="add";
return new ModelAndView("/multi","method",name);
}

//method2
public ModelAndView update(HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("------MultiController update----------");
String name="update";
return new ModelAndView("/multi","method",name);
}
}

上面spring3.0以后配置实例对应的类

@Controller
@RequestMapping("/test")//访问的总包(根目录)
public class Mycontller4 extends MultiActionController {

@RequestMapping("/youHua")//访问方法名
public ModelAndView youHua(){
System.out.println("Mycontller4——youHua。。优化注解");
String mulit="Mycontller4——youHua。。优化注解";
return new ModelAndView("/annotation","mulit",mulit);
}
/*method=RequestMethod.GET
* 只接收get提交方法的请求方试,不写这一句那么就不区分提交方式
*/
@RequestMapping("/tiao",method=RequestMethod.GET)
public String tiao(HttpServletRequest request){
System.out.println("Mycontller4——tiao。。优化注解");
String mulit="Mycontller4——youHua。。优化注解";
request.setAttribute("mulit", mulit);
return "/annotation";
}

web.xml文件中的配置

<!-- 这个是spring MVC的入口 一个servlet
spring-webmvc-3.2.0.RELEASE.jar 包下第一个
-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--
配置dispatcher的加载路劲
classpath*表示加载config包下面的所有文件
(如果这个配置文件放在lib文件下,springmvc启动后默认会自己去加载 ,下面的这一段就可以不用写了)
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/dispatcher-servlet.xml</param-value>
</init-param>-->
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

springMVC工作机制和框架搭建配置说明的更多相关文章

  1. (一)springmvc+spring+mybatis+maven框架搭建

    (一)springmvc+spring+mybatis+maven框架搭建 1.说明 工作之余,为了学习点东西.先搭建个框架. 以后要往里面加东西,比如rabbitMQ.redis.shiro等. 也 ...

  2. springmvc工作原理和环境搭建

    SpringMVC工作原理     上面的是springMVC的工作原理图: 1.客户端发出一个http请求给web服务器,web服务器对http请求进行解析,如果匹配DispatcherServle ...

  3. springmvc+mybatis+maven项目框架搭建

    项目的目录

  4. SSM框架搭建web服务器实现登录功能(Spring+SpringMVC+Mybatis)

    初学java EE,虽然知道使用框架会使开发更加便捷高效,但是对于初学者来说,感到使用框架比较迷惑,尤其是各种jar包的引用.各种框架的配置.注解的使用等等. 最好的学习方法就是实践,于是下载了一个现 ...

  5. springMVC,spring,mybatis全注解搭建框架--第一步,让框架跑起来

    自己从事java开发工作也有一年多了,自己却没有亲手搭建一个完整的框架.于是今天自己动手搭建一个,过程中遇到一些问题,倒腾了大半天终于搞定了. 现在给大家分享一下过程,自己也记录下来,以后学习参考使用 ...

  6. MyBatis+SpringMVC 框架搭建小结

    前言:最近再写一款视频播放器的后台,踩了很多坑,在此总结. 设计顺序: 前提:搭建配置完好的Spring-MyBatis项目 1.流程分析,数据库设计(看似无用,真正做起来真的需要这个东西帮忙整理下思 ...

  7. 手把手教你使用VUE+SpringMVC+Spring+Mybatis+Maven构建属于你自己的电商系统之vue后台前端框架搭建——猿实战01

            猿实战是一个原创系列文章,通过实战的方式,采用前后端分离的技术结合SpringMVC Spring Mybatis,手把手教你撸一个完整的电商系统,跟着教程走下来,变身猿人找到工作不是 ...

  8. 基于SpringMVC下的Rest服务框架搭建【1、集成Swagger】

    基于SpringMVC下的Rest服务框架搭建[1.集成Swagger] 1.需求背景 SpringMVC本身就可以开发出基于rest风格的服务,通过简单的配置,即可快速开发出一个可供客户端调用的re ...

  9. Unity 游戏框架搭建 (五) 简易消息机制

    什么是消息机制? 23333333,让我先笑一会. 为什么用消息机制?   三个字,解!!!!耦!!!!合!!!!. 我的框架中的消息机制用例: 1.接收者 ``` using UnityEngine ...

随机推荐

  1. Android LayoutInflater详解(转)

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  2. MySQL的中文编码问题

    创建表格时,怎么让表格显示中文?注意:不区分大小写 mysql> ALTER TABLE 表格的名字 CONVERT TO CHARACTER SET UTF8; 怎么让默认的数据库支持中文字符 ...

  3. 生成n位随机字符串

    --1.借助newid() Go --创建视图(因为在函数中无法直接使用newid()) create view vnewid as select newid() N'MacoId'; go --创建 ...

  4. 自定义ActionBar

    /** * 1.创建ActionBar对象getSupportActionBar() * 2.布置自己的ActionBar布局(在res/layout) * 3.把自定义的ActionBar布局加载到 ...

  5. 苹果推送(APNs)ios push小结

    把app删除后就推送不成功了,可以看出deviceToken应该是设备+app来一起识别的,重新安装后仍然为同一个 简介 推送服务APNs(Apple Push Notification servic ...

  6. Http协议提要

    HTTP协议提要 简单来说,HTTP就是一个基于应用层的通信规范:双方要进行通信,大家就要遵守一个规范---HTTP协议.HTTP协议从WWW服务器超文本到本地浏览器 ,可以使浏览器更加高效.HTTP ...

  7. (转)qsort和sort

    1.qsort函数: 原 型: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *,const void *)) ...

  8. express-20 REST API和JSON

    简介 "Web服务"是一个通用术语,指任何可以通过HTTP访问的应用程序编程界面(API); 我们的重点是提供"REST风格"的服务,与其交互要更直接得多. R ...

  9. 【面试题】HWL

    1.编程题 设计数据结构,模拟浏览器功能: BACK FORWARD VISIT QUIT Visit 未指定具体网址时,返回主页 http://www.xueersi.com 2.指出下列程序的错误 ...

  10. WPF在DLL中读取Resource的方法

    WPF是个用户控件,被WinForm调用.而WinForm是在一个DLL类库中被调用.试了很多方法,都无法将Resource中的图读进程序.用下面的方法总算实现了.   /根据图片的名称,从资源中找到 ...