1.SpringMVC运行原理(执行过程)
( 1 )客户端(浏览器)发送请求,直接请求到DispatcherServlet。
继承关系:DispatcherServlet--继承-->FrameworkServlet-->HttpServletBean--->HttpServlet--->GenericServlet
(2)DispatcherServlet根据请求信息调用HandlerMapping,解析请求对应的Handler。
(3)解析到对应的Handler后,开始由HandlerAdapter适配器处理。
(4)HandlerAdapter会根据Handler来调用真正的处理器开处理请求,并处理相应的业务逻辑。
(5)处理器处理完业务后,会返回一个ModelAndView对象,Model是返回的数据对象,View是个逻辑上的View。
(6)ViewResolver会根据逻辑View查找实际的View。
(7)DispaterServlet把返回的Model传给View。
(8)通过View返回给请求者(浏览器)
2、 SpringMVC配置式开发
需求:用户提交一个请求,服务端处理器接收到请求后,给出一条信息,在相应页面显示该条信息
1导入jar包
https://files.cnblogs.com/files/QW-lzm/SpringMVC----.jar.rar
2配置web.xml,注册SpringMVC前端控制器(中央调度器)
3编写SpringMVC后端控制器
4编写springmvc配置文件,注册后端控制器(注意id写法格式)
配置文件的约束:
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
5编写跳转资源页面
把设置的数据通过addObject响应到前端,并通过setViewName来跳转到指定页面 a.jsp
package com.springmvc.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
//用来封装跳转页面及数据的模型视图对象
ModelAndView m =new ModelAndView();
//携带数据流转到调转页面
m.addObject("uname","张三");//session可以携带数据流转
//指定要跳转的页面
m.setViewName("a.jsp");
return m;
}
}
4.web.xml中urlpattern配置问题
通过浏览器访问/abc 来跳转访问MyContorller类(上面那个类)
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--相当于sevlet中给当前类提供访问的url-partten;浏览器可以通过访问/abc来访问MyController类-->
<bean id="/abc" class="com.springmvc.controller.MyController"/>
<!--放行静态资源-->
<!--/images/**表示放行/images/目录下及其子目录下的所有静态资源-->
<mvc:resources mapping="/images/**" location="/images/"/>
</beans>
6跳转到a.jsp并把后端响应的数据在前端显示
通过${uname}
<html>
<head>
<base href="<%=basePath%>">
<title>Title</title>
</head>
<body>
<h2>第一个springmvc程序</h2>
hello springmvc!!--${uname} <br/>
<img src="images/aa.jpg" alt="找不到图片...">
</body>
</html>