SpringMvc之第一回(框架了解和第一个SpringMvc程序)
来源:互联网 发布:windows密钥能用几次 编辑:程序博客网 时间:2024/05/16 10:20
一、Spring mvc介绍
Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还可以是 Struts 这样的 Web 框架。Spring web mvc和Struts2都属于表现层的框架,它是Spring框架的一部分,我们可以从Spring的整体结构中看得出来:二、Web mvc 结构示意图
说明:
1、 用户发起request请求至控制器(Controller)控制接收用户请求的数据,委托给模型进行处理
2、 控制器通过模型(Model)处理数据并得到处理结果模型通常是指业务逻辑
3、 控制器将模型数据在视图(View)中展示web中模型无法将数据直接在视图上显示,需要通过控制器完成。如果在C/S应用中模型是可以将数据在视图中展示的。
4、 控制器将视图response响应给用户通过视图展示给用户要的数据或处理结果。
三、Spring mvc 架构
1.流程说明
1、 用户发送请求至前端控制器DispatcherServlet
2、 DispatcherServlet收到请求调用HandlerMapping处理器映射器。
3、 处理器映射器找到具体的处理器,生成处理器对象及处理器拦截器(如果有则生成)一并返回给DispatcherServlet。
4、 DispatcherServlet调用HandlerAdapter处理器适配器
5、 HandlerAdapter经过适配调用具体的处理器(Controller,也叫后端控制器)。
6、 Controller执行完成返回ModelAndView
7、 HandlerAdapter将controller执行结果ModelAndView返回给DispatcherServlet
8、 DispatcherServlet将ModelAndView传给ViewReslover视图解析器
9、 ViewReslover解析后返回具体View
10、 DispatcherServlet根据View进行渲染视图(即将模型数据填充至视图中)。
11、 DispatcherServlet响应用户
2.组件说明
以下组件通常使用框架提供实现(也就是一般企业中实际开发能满足大部分需求):
DispatcherServlet:作为前端控制器,整个流程控制的中心,控制其它组件执行,统一调度,降低组件之间的耦合性,提高每个组件的扩展性。
HandlerMapping:通过扩展处理器映射器实现不同的映射方式,例如:配置文件方式,实现接口方式,注解方式等。
HandlAdapter:通过扩展处理器适配器,支持更多类型的处理器。
ViewResolver:通过扩展视图解析器,支持更多类型的视图解析,例如:jsp、freemarker、pdf、excel等。
下边两个组件通常情况下需要开发:
Handler:处理器,即后端控制器用controller表示。
View:视图,即展示给用户的界面,视图中通常需要标签语言展示模型数据。
三、SpringMvc for HelloWorld 程序
测试开发环境为:win7 + Eclipse + Tomcat71.创建项目
后面两个步骤,直接用默认的就好了。 建好的项目如下:2.加入SpringMvc 相关 Jar包
我会在文章结尾处将博客所用jar放出一个百度云链接,方便大家下载。 选中全部,copy到项目中/springmvc_blog_001/WebContent/WEB-INF/lib 下。3.编写Web.xml 配置控制器
<!--?xml version=
"1.0"
encoding=
"UTF-8"
?-->
<web-app id=
"WebApp_ID"
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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>
<display-name>springmvc_blog_001</display-name>
<!-- 配置SpringMvc 前端控制器 -->
<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-servlet.xml</param-value>
</init-param>
<load-on-startup>
1
</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
load-on-startup:表示servlet随服务启动;
url-pattern:*.action的请交给DispatcherServlet处理。
contextConfigLocation:指定springmvc配置的加载位置,如果不指定则默认加载WEB-INF/[DispatcherServlet 的Servlet 名字]-servlet.xml。
4.编写SpringMvc配置文件
在WEB-INF下创建springmvc-servlet.xml文件1).配置处理器映射器
在springmvc-servlet.xml文件配置如下:<!--?xml version=
"1.0"
encoding=
"UTF-8"
?-->
<beans xmlns=
"http://www.springframework.org/schema/beans"
xmlns:aop=
"http://www.springframework.org/schema/aop"
xmlns:context=
"http://www.springframework.org/schema/context"
xmlns:mvc=
"http://www.springframework.org/schema/mvc"
xmlns:tx=
"http://www.springframework.org/schema/tx"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation="http://www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-3.1.xsd
http:
//www.springframework.org/schema/mvc
http:
//www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http:
//www.springframework.org/schema/context
http:
//www.springframework.org/schema/context/spring-context-3.1.xsd
http:
//www.springframework.org/schema/aop
http:
//www.springframework.org/schema/aop/spring-aop-3.1.xsd
http:
//www.springframework.org/schema/tx
http:
//www.springframework.org/schema/tx/spring-tx-3.1.xsd ">
<!-- HandlerMapping -->
<bean
class
=
"org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"
></bean></beans>
2).配置处理器适配器
说明:SimpleControllerHandlerAdapter:即简单控制器处理适配器,所有实现了org.springframework.web.servlet.mvc.Controller 接口的Bean作为Springmvc的后端控制器。
3).配置视图解析器
<bean
class
=
"org.springframework.web.servlet.view.InternalResourceViewResolver"
></bean>
InternalResourceViewResolver:支持JSP视图解析viewClass:JstlView表示JSP模板页面需要使用JSTL标签库,所以classpath中必须包含jstl的相关jar 包;prefix 和suffix:查找视图页面的前缀和后缀,最终视图的址为:前缀+逻辑视图名+后缀,逻辑视图名需要在controller中返回ModelAndView指定,比如逻辑视图名为hello,则最终返回的jsp视图地址 “WEB-INF/jsp/hello.jsp”.这里为了第一个程序简单起见,省略部分配置。
4).完整的配置是这样的
<!--?xml version=
"1.0"
encoding=
"UTF-8"
?-->
<beans xmlns=
"http://www.springframework.org/schema/beans"
xmlns:aop=
"http://www.springframework.org/schema/aop"
xmlns:context=
"http://www.springframework.org/schema/context"
xmlns:mvc=
"http://www.springframework.org/schema/mvc"
xmlns:tx=
"http://www.springframework.org/schema/tx"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation="http://www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-3.1.xsd
http:
//www.springframework.org/schema/mvc
http:
//www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http:
//www.springframework.org/schema/context
http:
//www.springframework.org/schema/context/spring-context-3.1.xsd
http:
//www.springframework.org/schema/aop
http:
//www.springframework.org/schema/aop/spring-aop-3.1.xsd
http:
//www.springframework.org/schema/tx
http:
//www.springframework.org/schema/tx/spring-tx-3.1.xsd ">
<!--
1
.BeanNameUrlHandlerMapping:表示将定义的Bean名字作为请求的url,需要将编写的controller在spring容器中进行配置,且指定bean的name为请求的url,且必须以.action结尾。
-->
<bean
class
=
"org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"
>
<!--
2
.SimpleControllerHandlerAdapter:即简单控制器处理适配器,所有实现了org.springframework.web.servlet.mvc.Controller 接口的Bean作为Springmvc的后端控制器。
-->
<bean
class
=
"org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"
>
<!--
3
.InternalResourceViewResolver:支持JSP视图解析
viewClass:JstlView表示JSP模板页面需要使用JSTL标签库,所以classpath中必须包含jstl的相关jar 包;
prefix 和suffix:查找视图页面的前缀和后缀,最终视图的址为:
前缀+逻辑视图名+后缀,逻辑视图名需要在controller中返回ModelAndView指定,比如逻辑视图名为hello,则最终返回的jsp视图地址 “WEB-INF/jsp/hello.jsp”
-->
<bean
class
=
"org.springframework.web.servlet.view.InternalResourceViewResolver"
>
</bean></bean></bean></beans>
5. 开发后端控制器(Controller)
package
com.billstudy.springmvc.controller;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.springframework.web.servlet.ModelAndView;
import
org.springframework.web.servlet.mvc.Controller;
/**
* HelloWorld 控制器
* @author Bill
* @since V1.0 2014/01/14
*/
public
class
HelloWorldController
implements
Controller {
@Override
public
ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response)
throws
Exception {
ModelAndView modelAndView =
new
ModelAndView();
// 存值
modelAndView.addObject(
"testMessage"
,
"hi,第一个SpringMvc程序!"
);
// 设置跳转的页面(视图:View)
modelAndView.setViewName(
"WEB-INF/helloworld.jsp"
);
return
modelAndView;
}
}
6. 在springmvb-servlet.xml中加入自己写的HelloWorldController配置
<!-- 配置写好的后端控制器 -->
<bean
class
=
"com.billstudy.springmvc.controller.HelloWorldController"
name=
"/helloworld.action"
></bean>
<span style=
"white-space:pre"
> </span>
7.编写jsp页面
在WEB-INF下面创建helloworld.jsp文件 好了,现在部署到Tomcat测试一把! 好的,没有问题! 如果大家在启动项目时候出现,springmvc-servlet.xml找不到错误,请到Tomcat发布目录中看看spring-servlet.xml位置是否在classes中,可能因为Eclipse和Tomcat配置的不同,位置也不一样。可以通过红色代码来配置对应的位置。<init-param>
<param-name>contextConfigLocation</param-name>
<span style=
"color:#ff0000;"
><param-value>classpath:springmvc-servlet.xml</param-value></span>
</init-param>
所有源码+jar包百度云地址:点击打开链接
- 和大彪一起来学习-SpringMvc之第一回(框架了解和第一个SpringMvc程序)
- SpringMvc之第一回(框架了解和第一个SpringMvc程序)
- 第一个SpringMVC程序
- 第一个SpringMVC程序
- springmvc第一个程序
- SpringMVC入门第一个程序
- 第一个SpringMVC+Hibernate框架搭建之路
- 我的SSH之旅 (2) : 第一个SpringMVC程序
- springmvc框架简介,第一个demo
- SpringMVC 入门------ 第一个SpringMVC框架的JavaWeb项目
- SpringMVC -- 第一个简单的程序
- 第一个配置SpringMVC,HelloWorld程序
- 开发第一个SPringMVC的程序
- 了解springmvc框架
- SpringMVC与struts2区别和第一个springMVC应用
- SpringMVC与struts2区别和第一个springMVC应用
- 学习SpringMVC——配置文件和第一个SpringMVC应用
- 第一个springMVC
- JS中bind方法与函数柯里化
- uploadify上传后预览Demo
- 中心召开个贷率目标任务推进会
- Onvif——生成源码框架
- 学习Java(一)
- SpringMvc之第一回(框架了解和第一个SpringMvc程序)
- LCD驱动
- Top 10 Free Source Code Editors – Reviewed
- 最长回文字符串
- NestedScrollView 以及ScrollView中 布局自动wrap_parent问题解决
- 23 服务的启动Demo2
- 大素数模板
- [刷题]算法竞赛入门经典(第2版) 5-10/UVa1597 - Searching the Web
- SurfaceView学习