SpringMVC框架
11.3.2 完成HelloWorld
- 页面链接
<a href="helloworld">Hello World</a> |
- 控制器方法
package com.atguigu.springmvc.handler;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;
@Controller public class HelloWorldHandler { @RequestMapping("/helloworld") public String testHello(){ System.out.println("Hello,SpringMVC..."); return "success"; } } |
- 成功页面:/views/success.jsp
<h3>Success Page</h3> |
11.3.3 Debug实验
- 正常流程,运行出结果
- 没有配置<mvc:default-servlet-handler/>,测试,直接报404
- http://localhost:8080/SpringMVC_09_WorkFlow/helloworld2
四月 20, 2016 11:53:19 上午 org.springframework.web.servlet.PageNotFound noHandlerFound 警告: No mapping found for HTTP request with URI [/SpringMVC_09_WorkFlow/helloworld2] in DispatcherServlet with name 'springDispatcherServlet' |
四月 20, 2016 11:54:16 上午 org.springframework.web.servlet.PageNotFound noHandlerFound 警告: No mapping found for HTTP request with URI [/SpringMVC_09_WorkFlow/test.html] in DispatcherServlet with name 'springDispatcherServlet' |
- 配置<mvc:default-servlet-handler/>,测试,会去查找目标资源
- 测试,依然发生错误,这时,需要配置:<mvc:annotation-driven/>,否则,映射解析不好使。
11.3.4 Debug流程分析
- HandlerExecutionChain mappedHandler;包含了拦截器和处理器方法;
DispatcherServlet L902 916
org.springframework.web.servlet.HandlerExecutionChain Handler execution chain, consisting of handler object and any handler interceptors. Returned by HandlerMapping's HandlerMapping.getHandler method. |
- HandlerMapping
org.springframework.web.servlet.HandlerMapping Interface to be implemented by objects that define a mapping between requests and handler objects. This class can be implemented by application developers, although this is not necessary, as org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping and org.springframework.web.servlet.handler.SimpleUrlHandlerMapping are included in the framework. The former is the default if no HandlerMapping bean is registered in the application context. HandlerMapping implementations can support mapped interceptors but do not have to. A handler will always be wrapped in a HandlerExecutionChain instance, optionally accompanied by some HandlerInterceptor instances. The DispatcherServlet will first call each HandlerInterceptor's preHandle method in the given order, finally invoking the handler itself if all preHandle methods have returned true. The ability to parameterize this mapping is a powerful and unusual capability of this MVC framework. For example, it is possible to write a custom mapping based on session state, cookie state or many other variables. No other MVC framework seems to be equally flexible. Note: Implementations can implement the org.springframework.core.Ordered interface to be able to specify a sorting order and thus a priority for getting applied by DispatcherServlet. Non-Ordered instances get treated as lowest priority. |
- 没有配置<mvc:default-servlet-handler/>,<mvc:annotation-driven/>,发送一个不存在资源的请求路径,mappedHandler为null
- http://localhost:8080/SpringMVC_09_WorkFlow/helloworld2
- 配置<mvc:default-servlet-handler/>,<mvc:annotation-driven/>,发送一个不存在资源的请求路径
- http://localhost:8080/SpringMVC_09_WorkFlow/helloworld2
- mappedHandler不为null,原因是当循环simpleUrlHandlerMapping时,当做静态资源处理