MyEclipse Spring 学习总结三 SpringMVC
一、SpringMVC原理
1.Springmvc 框架介绍
1)Spring 框架停工了构建Web应用程序的全功能MVC模块。Springmvc包含多种视图技术,例如JSP,Volocity,Tiles,iText和POI。Spring MVC框架并不知道使用的视图,所以不会强迫只使用JSP技术
SpringMVC分离了控制器,模型对象,分配器已经处理程序的角色。
2)Springmvc 框架主要由DispatcherServlet,处理器映射,处理器(控制器),试图解析器,试图组成。
2.SpringMVC接口解释
(1)DispatcherServlet接口
Spring提供的前端控制器,所有的请求都经过它统一分发。DispatcherServlet是整个Spring MVC的核心。它负责接收HTTP请求,组织协调Spring MVC的各个部分。
主要工作有以下三个
a)截获符合特定格式的URL请求
b)初始化DispatcherServlet上下文对应的WebApplicationContext,并将其业务层,持久化曾的WebApplicationContext建立关联
c)初始化Spring MVC的各个组成组件,并装配到DispatcherServlet中。
(2)HandlerMappping接口
能够完成客户请求道Controller的映射
(3)Controller接口:
需要为并发用户处理请求,因此实现Controller接口时,必须保证线程安全,并可重用。
从宏观考虑,DispatcherServlet是整个Web应用的控制器,从微观考虑,Controller是单个Http请求的控制器。而ModelAndView是Http请求过程中返回的模型(Model)和视图(View)
(4)ViewResolver接口:
视图解析器ViewResolver在Web应用中查找View对象,从而将相应的结果渲染给用户
3. SpringMVC原理图
(1)HTTP请求: 客户端将请求提交到DispatcherServlet
(2)寻找处理器: 由DispatcherServlet控制器查询一个或多个HandlerMapping, 找到处理请求的Control
(3)调用处理器: DispatcherServlet将请求提交到Controller
(4)(5)调用业务处理和返回结果: Control调用调用业务逻辑处理后,返回ModelAndView
(6)(7) 处理试图映射并返回模型: DispatcherServlet查询一个或多个ViewResolver试图解析器,找到ModelAndView指定的视图
(8)Http响应: 视图负责将结果显示到客户端
二、项目结果如下:
web.xml文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<web-app version=
"3.0"
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_3_0.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-
class
>
org.springframework.web.servlet.DispatcherServlet
</servlet-
class
>
<load-on-startup>
1
</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
|
HelloWeb-servlet.xml文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<beans
xmlns=
"http://www.springframework.org/schema/beans"
xmlns:context=
"http://www.springframework.org/schema/context"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:p=
"http://www.springframework.org/schema/p"
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">
<context:component-scan base-
package
=
"com.example"
/>
<bean
class
=
"org.springframework.web.servlet.view.InternalResourceViewResolver"
>
<property name=
"prefix"
value=
"/WEB-INF/jsp/"
/>
<property name=
"suffix"
value=
".jsp"
/>
</bean>
</beans>
|
HelloControl.java 文件
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestMethod;
import
org.springframework.ui.ModelMap;
@Controller
@RequestMapping
(
"/hello"
)
public
class
HelloController{
@RequestMapping
(method = RequestMethod.GET)
public
String printHello(ModelMap model) {
model.addAttribute(
"message"
,
"Hello Spring MVC Framework!"
);
return
"hello"
;
}
}
|
hello.jsp文件
1
2
3
4
5
6
7
8
9
|
<%@ page contentType=
"text/html; charset=UTF-8"
%>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
|
输出结果如下:
本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/p/5179930.html,如需转载请自行联系原作者