Spring MVC 框架:第十二章:运行原理,腾讯 Java 面试题
④将模型数据保存到请求域
所在 API:org.springframework.web.servlet.view.AbstractView 源码位置:374 行
⑤转发
所在 API:org.springframework.web.servlet.view.InternalResourceView 源码位置:209 行
10.调用拦截器的 afterCompletion 方法
所在 API:org.springframework.web.servlet.DispatcherServlet 源码位置:1030 行
第三节 annotation 相关
我们在前面的操作中发现,使用了 mvc:default-servlet-handler 和 mvc:view-controller 后必须使用 mvc:annotation-driven。那么这是为什么呢?关键原因是他们加载使用的 HandlerMapping 不同。
1.三个都没有使用时有效的 HandlerMapping
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
其中 DefaultAnnotationHandlerMapping 负责把所有 handler 类中的 handler 方法收集起来。
2.增加了 mvc:default-servlet-handler 或 mvc:view-controller 后有效的 HandlerMapping
org.springframework.web.servl
et.handler.BeanNameUrlHandlerMapping
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping
很明显,DefaultAnnotationHandlerMapping 没了,而 SimpleUrlHandlerMapping 只能映射静态资源。所以我们通过 @RequestMapping 映射的 handler 方法无效了。
3.再增加了 mvc:annotation-driven 后
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping
加入了 mvc:annotation-driven 后最关键的是增加了 RequestMappingHandlerMapping,从而可以映射我们的 handler 方法。
案例:
index.jsp:
<a href="${pageContext.request.contextPath }/test/work/flow">Test work flow</a>
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_2_5.xsd"
id="WebApp_ID" version="2.5">
<!-- The front controller of this Spring Web application, responsible for
handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<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>
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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
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-4.0.xsd">
<context:component-scan base-package="com.atguigu.spring.mvc.handlers"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:mapping path="/**"/>
<bean class="com.atguigu.spring.mvc.interceptor.WorkFlowInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
interceptor
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class WorkFlowInterceptor implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("my after completion");
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
System.out.println("my post handle");
}
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
System.out.println("my pre handle");
return true;
}
}
handlers
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class WorkFlowHandler {
@RequestMapping("/test/work/flow")
public String myHandle(Model model) {
System.err.println("my handler method");
model.addAttribute("message", "i love you!!!");
return "target";
}
评论