SpringMVC札集(01)——SpringMVC入门完整详细示例(上)
自定义View系列教程00–推翻自己和过往,重学自定义View
自定义View系列教程01–常用工具介绍
自定义View系列教程02–onMeasure源码详尽分析
自定义View系列教程03–onLayout源码详尽分析
自定义View系列教程04–Draw源码分析及其实践
自定义View系列教程05–示例分析
自定义View系列教程06–详解View的Touch事件处理
自定义View系列教程07–详解ViewGroup分发Touch事件
自定义View系列教程08–滑动冲突的产生及其处理
探索Android软键盘的疑难杂症
深入探讨Android异步精髓Handler
详解Android主流框架不可或缺的基石
站在源码的肩膀上全解Scroller工作机制
Android多分辨率适配框架(1)— 核心基础
Android多分辨率适配框架(2)— 原理剖析
Android多分辨率适配框架(3)— 使用指南
几年前写过一些后台,当时见得最多的就是SSH(Spring + Struts + Hibernate)。现在重新回到这里的时候发现大家谈论得更多不再是SSH的使用及其源码剖析;反而是关于它的弊端,比如Hibernate的过度设计和Struts的安全漏洞。在一些中小项目中,人们更多的去选用SSM(Spring+SpringMVC+MyBatis)或者使用SpringMVC+MyBatis的组合。嗯哼,既然时代的潮流不可阻挡,我又何必螳臂当车;现要拾起后台的开发技能,那就跳过Struts和Hibernate直接从SpringMVC作为切入点吧。
MVC
在讨论SpringMVC之前,我们先来看看MVC
关于MVC无需多说;一图胜千言,或许就是这个理。
SpringMVC
关于SpringMVC的执行流程,请参见上图;小结如下:
- 01 浏览器向服务器发起请求
- 02 DispatcherServlet接收到请求后利用处理器映射器HandlerMapping寻找Handler
- 03 HandlerMapping返回一个包含Handler和拦截器的HandlerExecutionChain给DispatcherServlet
- 04 DispatcherServlet调用处理器适配器HandlerAdapter执行Handler
- 05 HandlerAdapter执行Handler(Controller)得到ModelAndView
- 06 Handler返回ModelAndView至HandlerAdapter
- 07 HandlerAdapter返回ModelAndView至DispatcherServlet
- 08 DispatcherServlet调用视图解析器ViewResolver
- 09 ViewResolver解析出ModelAndView中的View返回至DispatcherServlet
- 10 DispatcherServlet将View与ModelAndView中的Model结合进行视图渲染
- 11 试图渲染结果返回至DispatcherServlet
- 12 DispatcherServlet应答浏览器
SpringMVC在MVC的大框架下做了一些调整:C和V基本不变,分别负责调度和视图渲染;但是把M拆开了,拆成了处理器映射器HandlerMapping,处理器适配器HandlerAdapter,视图解析器ViewResolver。这些组件有什么用呢?该怎么使用呢?请看如下示例。
SpringMVC入门示例
按照国际惯例,我们来写一个SpringMVC版本的Hello world.
第一步:准备jar包
第二步:配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<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:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
在web.xml文件中配置DispatcherServlet。嗯哼,发现了没有,它和我们之前配置servlet是非常的类似。
第三步:实现Controller
/**
* @author 原创作者:谷哥的小弟
* @blog 博客地址:http://blog.csdn.net/lfdfhl
* @time 创建时间:2017年7月22日 下午4:57:04
* @info 描述信息:自定义Controller
*/
package cn.com;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class MyController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception {
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("helloSpringMVC", "欢迎您学习SpringMVC");
modelAndView.setViewName("test");
return modelAndView;
}
}
此处实现org.springframework.web.servlet.mvc.Controller接口并在handleRequest()方法中返回一个ModelAndView至DispatcherServlet;在这利用addObject()方法给View设置数据,数据的key是helloSpringMVC;我们会在稍后的步骤中取出该值。
请注意第19行代码:
modelAndView.setViewName(“test”);
在此设置的View的名字为test,表示响应的页面。这就有点奇怪了,不是应该是一个包含了test的url么,比如xxx/ooo/xxx/test.jsp么?别急,这里仅仅是一个逻辑视图,它所对应的物理视图的完整路径会在springmvc.xml中进行配置的。
第四步:配置springmvc.xml
<?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: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.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 处理器映射器 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- 处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!-- 自定义Controller -->
<bean id="myController" name="/welcome.do" class="cn.com.MyController"></bean>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
在该springmvc.xml中配置了处理器映射器HandlerMapping,处理器适配器HandlerAdapter,视图解析器ViewResolver以及我们的Controller。其中,此处配置的HandlerMapping和HandlerAdapter均是SpringMVC的默认配置。其实,此处的HandlerMapping只要瞅瞅它的名字就很好理解它的作用:BeanNameUrlHandlerMapping——根据bean的name属性所设置的url去执行对应的Controller;正如此例:
<bean id="myController" name="/welcome.do" class="cn.com.MyController"></bean>
在Controller的配置为其设置name属性为/welcome.do,当用户在浏览器中输入http://xxx.ooo.xxx/welcome.do时BeanNameUrlHandlerMapping就会判断它所映射到的Controller是MyController,于是将该Controller返回至DispatcherServlet;再由SimpleControllerHandlerAdapter执行该Controller。至于,ViewResolver的配置,请注意前缀和后缀的配置;将它们与Controller中的逻辑视图一起组拼起来构成一个物理视图,比如:/WEB-INF/jsps/test.jsp;它就是用户最终看到的View。
第五步:编写jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>SpringMVC_HelloWorld</title>
<style type="text/css">
p {
font-size: 40px;
font-family: 宋体;
color: red;
background-color: pink;
}
</style>
</head>
<body>
<p>${helloSpringMVC}</p>
<p>原创作者:谷哥的小弟</p>
<p>博客地址:http://blog.csdn.net/lfdfhl</p>
</body>
</html>
在此编译一个非常简单的jsp页面,请注意第16行代码:
${helloSpringMVC}
获取第三步传递过来的数据!
第六步:部署验证
将项目部署至Tomcat,然后在浏览器中输入
回车后,即可见test.jsp
最后,附上本示例的项目结构:
梳理小结
好了,代码写完了,测试也OK了。现在结合执行流程图和该示例再来梳理一下SpringMVC:
- 第一步:浏览器发起请求
- 第二步:DispatcherServlet接收到请求后利用HandlerMapping寻找Handler
- 第三步:HandlerAdapter执行Handler并返回ModelAndView至DispatcherServlet
- 第四步:DispatcherServlet进行视图渲染并将结果返回给浏览器
在该篇文章中展示了一个完整的SpringMVC入门示例。其实,比代码更重要的是原理。比如,自己梳理清楚了框架的执行流程,那么我们写起代码来思路也会清晰很多。
SpringMVC札集(01)——SpringMVC入门完整详细示例(上)的更多相关文章
- SpringMVC札集(03)——基于注解的SpringMVC入门完整详细示例
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- SpringMVC札集(02)——SpringMVC入门完整详细示例(下)
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- SpringMVC札集(05)——SpringMVC参数回显
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- SpringMVC札集(04)——SpringMVC传递参数
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- SpringMVC札集(10)——SSM框架整合
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- SpringMVC札集(09)——拦截器
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- SpringMVC札集(08)——文件上传
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- SpringMVC札集(07)——JSON数据
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- SpringMVC札集(06)——转发和重定向
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
随机推荐
- 吴超老师课程---Hadoop的伪分布安装
1.1 设置ip地址 执行命令 service network restart 验证: ifconfig1.2 关闭防火墙 执行命令 service ip ...
- UML类图几种关系的总结(转载 http://blog.csdn.net/tianhai110/article/details/6339565 )
http://blog.csdn.net/tianhai110/article/details/6339565
- akka框架地址
http://doc.akka.io/docs/akka/2.2.3/AkkaJava.pdf
- seven habits of highly effective people 高效能人士的七个习惯
习惯的模型 : dependent 依赖 -- independent 独立自主 --interdependent 互相依赖 1: be proactive 主动积极 what you can ...
- 参考MongoRepository,为接口生成bean实现注入
首先弄个注解,给代码个入口,这个就是mongo的@EnableMongoRepositories了. @Target(ElementType.TYPE) @Retention(RetentionPol ...
- Windows 下c获取文件目录
由于要插数据库,构建sql语句,需要文件名,在网上找了半天,无奈都是Linux下的专用函数,伤心,,还有那个下载URL ,还木搞好,就要走啦,心焦哇 #include<iostream> ...
- 【JavaScript】canvas实现一个小游戏
参考: 1.image onload事件:http://www.runoob.com/jsref/event-img-onload.html(赞) 2.canvas的drawImage无法显示图像:h ...
- 如何编写自己的虚拟DOM
要构建自己的虚拟DOM,需要知道两件事.你甚至不需要深入 React 的源代码或者深入任何其他虚拟DOM实现的源代码,因为它们是如此庞大和复杂--但实际上,虚拟DOM的主要部分只需不到50行代码. 有 ...
- redis_入门网址
redis中文网: http://www.redis.cn/ 可以 试用 以及 下载 redis百度百科:http://baike.baidu.com/link?url=MEkE5MpGAOfJ7ci ...
- linux根分区满了如何处理,查找大文件方法
一:如果linux根分区使用量达到100%,会造成如下现象: root不能登录 系统不能正常启动 二:通过命令查找根分区内的大文件 du -sh /* 2>/dev/null | sort -h ...