Spring MVC简介 2.5 Spring MVC执行的流程
package org.fkit.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; /** * HelloController是一个实现Controller接口的控制器, * 可以处理一个单一的请求动作 */ public class HelloController implements Controller{ private static final Log logger = LogFactory .getLog(HelloController.class); /** * handleRequest是Controller接口必须实现的方法 * 该方法的参数是对应请求的HttpServletRequest和HttpServletResponse * 该方法必须返回一个包含视图名或视图路径和模型的ModelAndView对象。 */ public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub logger.info("handleRequest 被调用"); // 创建准备返回的ModelAndView对象,该对象通常包含了返回视图名、模型的名称以及模型对象 ModelAndView mv = new ModelAndView(); // 添加模型数据,可以是任意的POJO对象 mv.addObject("message", "Hello World!"); //设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面 mv.setViewName("/WEB-INF/content/welcome.jsp"); //返回ModelAndView对象。 return mv; } }
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <!-- 定义Spring MVC的前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 让Spring MVC的前端控制器拦截所有请求 --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd"> <!-- 配置Handle,映射"/hello"请求 --> <bean name="/hello" class="org.fkit.controller.HelloController"></bean> <!-- 处理映射器将bean的name作为url进行查找,需要在配置Handle时指定name(即url) --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- SimpleControllerHandlerAdapter是一个处理器适配器,所有处理适配器都要实现 HandlerAdapter接口 --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"> </bean> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean> </beans>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>欢迎页面</title> </head> <body> <!-- 页面可以访问Controller传递出来的message --> ${requestScope.message}</body> </html>
示例:基于注解的控制器
package org.fkit.controller; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; /** * HelloController是一个基于注解的控制器, * 可以同时处理多个请求动作,并且无须实现任何接口。 * org.springframework.stereotype.Controller注解用于指示该类是一个控制器 * @author zhongzh * */ @Controller public class HelloController { private static final Log logger = LogFactory.getLog(HelloController.class); /** * org.springframework.web.bind.annotation.RequestMapping注解 * 用来映射请求的URL和请求的方法等。本例用来映射"/hello" * hello只是一个普通方法。 * 该方法返回一个包含视图名或视图名和模型的ModelAndView对象。 */ @RequestMapping(value="/hello") public ModelAndView hello() { logger.info("hello方法 被调用"); // 创建准备返回的ModelAndView对象,该对象通常包含了返回视图名、模型的名称以及模型对象 ModelAndView mv = new ModelAndView(); //添加模型数据,可以是任意的POJO对象 mv.addObject("message", "Hello World!"); // 设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面 mv.setViewName("/WEB-INF/content/welcome.jsp"); // 返回ModelAndView对象。 return mv; } }
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> --> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <!-- 定义Spring MVC的前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 让Spring MVC的前端控制器拦截所有请求--> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <!-- spring可以自动去扫描base-pack 下面的包或者子包下面的Java文件, 如果扫描到有Spring的相关注解的类,则把这些类注册为Spring的bean --> <context:component-scan base-package="org.fkit.controller"></context:component-scan> <!-- 配置annotation类型的处理映射器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> </bean> <!-- 配置annotation类型的处理器适配器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> </bean> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean> </beans>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>welcome</title> </head> <body> <!-- 页面可以访问Controller传递出来的message--> ${requestScope.message} </body> </html>