1.SpringMVC简介
Spring MVC框架是有一个MVC框架,通过实现Model-View-Controller模式来很好地将数据、业务与展现进行分离。
它的设计是围绕DispatcherServlet展开的,DispatcherServlet负责将请求派发到特定的handler。通过可配置的handler
mappings来处理请求并且转到对应的视图
2.SpringMVC运行原理
- 客户端(浏览器)发送请求,直接请求到DispatcherServlet
- DispatcherServlet根据请求信息调用HandlerMapping,解析请求对应的Handler
- 解析到对应的Handler后,开始由HandlerAdapter适配器处理
- HandlerAdapter会根据Handler来调用真正的处理器开处理请求,并处理相应的业务逻辑
- 处理器处理完业务后,会返回一个ModelAndView对象,Model是返回的数据对象,View是个逻辑上的View
- ViewResolver会根据逻辑View查找实际的View
- DispaterServlet把返回的Model传给View
- 通过View返回给请求者(浏览器)
3.springmvc的第一个HelloWorld
3.1 导入jar包
- commons-logging-1.1.1.jar
- spring-aop-4.0.0.RELEASE.jar
- spring-beans-4.0.0.RELEASE.jar
- spring-context-4.0.0.RELEASE.jar
- spring-core-4.0.0.RELEASE.jar
- spring-expression-4.0.0.RELEASE.jar
- spring-web-4.0.0.RELEASE.jar
- spring-webmvc-4.0.0.RELEASE.jar
3.2 配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>springmvc_1_helloWorld</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置 DispatcherServlet --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置 DispatcherServlet 的一个初始化参数: 配置 SpringMVC 配置文件的位置和名称 --> <!-- 实际上也可以不通过 contextConfigLocation 来配置 SpringMVC 的配置文件, 而使用默认的. 默认的配置文件为: /WEB-INF/<servlet-name>-servlet.xml --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
注意:servlet-mapping表示拦截的模式,这里是“/”,表示对于所有的请求的拦截,包括静态资源如html, js, jpg
3.3 配置 spring-mvc.xml
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.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.yyx.springmvc.controller"></context:component-scan> <!-- 配置视图解析器 :如何把 handlers 方法返回值解析为实际的物理视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
3.4 Handle处理器
package com.springmvc.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @RequestMapping("/springmvc") @Controller() public class HelloWorldController { private static final String SUCCESS = "success"; /** * 1.使用@RequestMapping注解来映射请求的URL 2. 返回值会通过视图解析器解析为实际的物理视图, 对于 * InternalResourceViewResolver 视图解析器, 会做如下的解析: 通过 prefix + returnVal + 后缀 * 这样的方式得到实际的物理视图, 然会做转发操作/WEB-INF/views/success.jsp * * @return */ @RequestMapping("/helloworld") public String sayHelloWorld() { System.out.println("Hello World!"); return SUCCESS; } }
3.5 初始页和目标页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="springmvc/helloworld">Hello World!</a> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h3>Success Page</h3> </body> </html>