SpringMVC-01 简述
MVC的流程
SSM
SSM:SpringMvc(完成数据的封装,页面的跳转的逻辑)
Spring()
Mybatis(持久化框架。ORM Object Relative Mapping 对象关系映射)
springMVC流程
graph TD
A[客户] -->| | B(控制器 Servlet)
B --> C( HandlerMapping)
C --> D[Controller1]
C --> E[Controller1]
C -->F[Controller1]
D -->G[ModelAndView]
E -->G[ModelAndView]
F -->G[ModelAndView]
G -->H[ ResoluteView]
H -->I( 客户)
写一个简单的springMVC的demo
1.引入springmvc的相关jar
**2.在web.xml中配置DispatcherServlet
<servlet>
<servlet-name>springMVC-annotation</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 将springMVC-annotation-servlet放在创建的resource文件中配置的路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC-annotation-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMVC-annotation</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
**3.配置springMVC-annotion的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 包扫描 -->
<context:component-scan base-package="com.hw.lb.controller.annotation"></context:component-scan>
<!-- 启动注解器 -->
<mvc:annotation-driven/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
SpringMVC的流程
1.客户发出请求。例如 http://localhost:8080/SpringMVC-01/my.do
2.到达web.xml文件中DispatcherServlet.查看是否符合url的要求
3.DispatcherServlet就会查询springmvc的配置文件。(找HandlerMapping)
根据bean的名称查找相应的controller
4.找到我的MyController类。执行该类中handleRequestInternal方法。
5.根据返回的modelAndView在找springmvc配置文件中视图解析器。
6.视图解析器吧viewName和prefix以及suffix做一个拼接,把拼接的页面展示给客户