java培训之SpringMVC源码解析

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

commons-logging-1.1.3.jar

spring-web-4.0.0.RELEASE.jar

spring-webmvc-4.0.0.RELEASE.jar

  • 配置文件xml

<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:springmvc.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>

  • 配置文件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.springmvc”/>

 

<!– 配置视图解析器 –>

<bean id=”internalResourceViewResolver”

??? class=”org.springframework.web.servlet.view.InternalResourceViewResolver”>

<property name=”prefix” value=”/WEB-INF/views/”/>

<property name=”suffix” value=”.jsp”/>

</bean>

 

</beans>

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>

3 Debug实验

四月 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/>,测试,会去查找目标资源
  • 测试,依?#29615;?#29983;错误,这?#20445;?#38656;要配置:<mvc:annotation-driven/>,否则,?#25104;?#35299;析不好使。

Java培训

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.

java培训

  • 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.

java培训

java培训机构

java培训

5 ?#31995;?/h3>

java培训


上一篇:
下一篇:

关于我们
名师团队
新闻动态
合作企业
资源下载
视频下载
资料下载
工具下载
加入我们
招聘岗位
岗位介绍
招贤纳师
联系我们
电话:010-56253825
邮箱:[email protected]
地址:北京市昌平区宏福科技园综?#19979;?层(北京校区)

 深圳市宝安区西部硅谷大厦B座C区一层(深圳校区)

上海市松江区谷阳北路166号大江商厦6层(上海校区)

轩辕传奇通天塔
排三复式投注玩法 浙江11选五开奖结果走势图 阿拉德之怒mg版怎么找不到了 时时走势图老时时360 湖北12333APP 百人牛牛透视辅助 时时彩五星综合分布图 ipad在线棋牌游戏 香港皇家彩库开奖号码 足球竞猜