SpringWebMVC是基于ServletAPI构建的原始Web框架,包含在Spring框架中。SpringMVC是一种轻量级Web框架,使用MVC架构模式。本文将讲解SpringMVC框架的基本处理流程和简易SpringMVC程序开发。
基本处理流程
如图所示,我将SpringMVC框架的基本请求流程大致13步骤,包括:
1.从浏览器发送请求
2.DispatcherServlet接受浏览器请求并交给处理器映射器HandlerMapping
如:
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
3.根据请求的URL查找相应的Handler
<bean id="/test" class="com.test.controller.TestController"/>
4.返回HandlerExecutionChain给DispatcherServlet
5.请求适配器执行Handler,如:
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
6.执行Handler (Handler查找Controller)
7.执行Controller
public class TestController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg","test");
modelAndView.setViewName("test");
return modelAndView;
}
}
8.Controller返回modelAndView给HandlerAdpter
9.HandlerAdpter返回modelAndView给DispatcherServlet
10.将modelAndView传给视图解析器ViewResolver
<bean id="InternalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
11.将View的逻辑名,即View的路径返回给DispatcherServlet
12.将步骤7中携带返回的Model数据传入View中并渲染界面
13.将视图响应给浏览器
SpringMVC Helloworld开发
这里我们就使用注解开发
编写web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<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>/</url-pattern>
</servlet-mapping>
</web-app>
首先注册一个DispatcherServlet,其初始参数init-param的contextConfigLocation配置为springmvc配置文件的路径,如classpath:springmvc-servlet.xml
再将servlet映射到/
注:
/ :匹配所有请求不包括jsp
/* :匹配所有请求包括jsp
配置SpringMVC配置文件
<?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: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/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:annotation-config/>
<context:component-scan base-package="com.test.controller"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<bean id="InternalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
其中
<context:component-scan base-package="com.test.controller"/>
为扫描Controller所在的包的路径
编写Controller
@Controller
public class TestController {
@RequestMapping("/test")
public String test(Model model){
model.addAttribute("msg","hello");
return "test";
}
}
@RequestMapping("/test") 为请求的URL地址,返回值为View的名称,会通过视图解析器
<bean id="InternalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
拼接成完整路径返回给DispatcherServlet
测试
最后写个jsp测试一下,DispatcherServlet会携带Model的数据渲染界面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
${msg}
</body>
</html>
一个简单的SpringMVC HelloWorld程序就编写完成了