springMVC工作机制和框架搭建配置说明
先说一下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工作机制和框架搭建配置说明的更多相关文章
- (一)springmvc+spring+mybatis+maven框架搭建
(一)springmvc+spring+mybatis+maven框架搭建 1.说明 工作之余,为了学习点东西.先搭建个框架. 以后要往里面加东西,比如rabbitMQ.redis.shiro等. 也 ...
- springmvc工作原理和环境搭建
SpringMVC工作原理 上面的是springMVC的工作原理图: 1.客户端发出一个http请求给web服务器,web服务器对http请求进行解析,如果匹配DispatcherServle ...
- springmvc+mybatis+maven项目框架搭建
项目的目录
- SSM框架搭建web服务器实现登录功能(Spring+SpringMVC+Mybatis)
初学java EE,虽然知道使用框架会使开发更加便捷高效,但是对于初学者来说,感到使用框架比较迷惑,尤其是各种jar包的引用.各种框架的配置.注解的使用等等. 最好的学习方法就是实践,于是下载了一个现 ...
- springMVC,spring,mybatis全注解搭建框架--第一步,让框架跑起来
自己从事java开发工作也有一年多了,自己却没有亲手搭建一个完整的框架.于是今天自己动手搭建一个,过程中遇到一些问题,倒腾了大半天终于搞定了. 现在给大家分享一下过程,自己也记录下来,以后学习参考使用 ...
- MyBatis+SpringMVC 框架搭建小结
前言:最近再写一款视频播放器的后台,踩了很多坑,在此总结. 设计顺序: 前提:搭建配置完好的Spring-MyBatis项目 1.流程分析,数据库设计(看似无用,真正做起来真的需要这个东西帮忙整理下思 ...
- 手把手教你使用VUE+SpringMVC+Spring+Mybatis+Maven构建属于你自己的电商系统之vue后台前端框架搭建——猿实战01
猿实战是一个原创系列文章,通过实战的方式,采用前后端分离的技术结合SpringMVC Spring Mybatis,手把手教你撸一个完整的电商系统,跟着教程走下来,变身猿人找到工作不是 ...
- 基于SpringMVC下的Rest服务框架搭建【1、集成Swagger】
基于SpringMVC下的Rest服务框架搭建[1.集成Swagger] 1.需求背景 SpringMVC本身就可以开发出基于rest风格的服务,通过简单的配置,即可快速开发出一个可供客户端调用的re ...
- Unity 游戏框架搭建 (五) 简易消息机制
什么是消息机制? 23333333,让我先笑一会. 为什么用消息机制? 三个字,解!!!!耦!!!!合!!!!. 我的框架中的消息机制用例: 1.接收者 ``` using UnityEngine ...
随机推荐
- Database.com SOQL and SOSL Reference
如下是关于 SOQ L与 SOSL 的相关链接: http://docs.database.com/dbcom/en-us/db_sosl_soql/sforce_api_calls_soql.htm ...
- loj 1167(二分+最大流)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26881 思路:我们可以二分最大危险度,然后建图,由于每个休息点只能 ...
- 标签q
标记短的引用,默认是中文符号:双引号 <p>文字<q>段落中的引用</q>文字</p> 如果是在html里直接敲出引号,是这样的: <p>文 ...
- SQL数据库的基本语句
1.修改字段类型语句: alter table 表名 alter column 列名 类型 例如: alter table D alter column no char(15): 2.从其他地方插 ...
- LoadRunner常用事务判断
一.数据值比较 lr_start_transaction("终审") if (atoi(lr_eval_string("{MyOutputParm}"))==a ...
- node.js整理 06异步编程
回调 异步编程依托于回调来实现,但不能说使用了回调后程序就异步化了 function heavyCompute(n, callback) { var count = 0, i, j; for (i = ...
- node.js整理 05进程管理
简介 NodeJS可以感知和控制自身进程的运行环境和状态,也可以创建子进程并与其协同工作,这使得NodeJS可以把多个程序组合在一起共同完成某项工作,并在其中充当胶水和调度器的作用 常用API Pro ...
- SQL ISNULL 函数
sql 中 NULL 值的处理:微软的 ISNULL() 函数用于规定如何处理 NULL 值.NVL(), IFNULL() 和 COALESCE() 函数也可以达到相同的结果.语法ISNULL ( ...
- [bzoj2118]墨墨的等式【dijk+堆】
10/30的update:如果是冲着dijk的板子来的,建议看多校联考contest中第二场day2的T2,那边的写法比较优秀... --------------------------------- ...
- POJ3308 Paratroopers(最小割/二分图最小点权覆盖)
把入侵者看作边,每一行每一列都是点,选取某一行某一列都有费用,这样问题就是选总权最小的点集覆盖所有边,就是最小点权覆盖. 此外,题目的总花费是所有费用的乘积,这时有个技巧,就是取对数,把乘法变为加法运 ...