DispatcherServlet 控制器入口 负责分发请求
HandlerMapping 负责根据请求 找到对应的控制器
Controller 真正处理请求的控制器
ModelAndView 封装数据信息和视图信息的
ViewResolver 视图处理器 通过处理找到对应的页面
2.实现Spring MVC 的步骤
2.1 建立一个 动态web 项目 在WEB-INF 下 建立一个 hello.jsp
导入jar包(mvc ioc) 拷贝Spring 配置文件到 src 下
2.2 在web.xml 中 配置 DispatcherServlet
关联Spring 的配置文件
2.3 在Spring 的配置文件中 配置HandlerMapping 接口对应的实现类
SimpleUrlHandlerMapping 指定请求路径和 对应的处理控制器的
对应关系
2.4 写一个 控制器类 实现 Controller 接口 完成返回 ModelAndView
赋值hello.jsp 对应的视图信息
2.5 在Spring 容器中创建 控制器对象 并关联请求和控制器的对象关系
2.6 在Spring 配置文件 配置 视图处理器接口 ViewResolver 对应的
实现类 InternalResourceViewResolver。
##配置文件配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework/schema/beans"
xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
xmlns:context="http://www.springframework/schema/context"
xmlns:jdbc="http://www.springframework/schema/jdbc"
xmlns:jee="http://www.springframework/schema/jee"
xmlns:tx="http://www.springframework/schema/tx"
xmlns:aop="http://www.springframework/schema/aop"
xmlns:mvc="http://www.springframework/schema/mvc"
xmlns:util="http://www.springframework/schema/util"
xmlns:jpa="http://www.springframework/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework/schema/beans http://www.springframework/schema/beans/spring-beans-4.1.xsd
http://www.springframework/schema/context http://www.springframework/schema/context/spring-context-4.1.xsd
http://www.springframework/schema/jdbc http://www.springframework/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework/schema/jee http://www.springframework/schema/jee/spring-jee-4.1.xsd
http://www.springframework/schema/tx http://www.springframework/schema/tx/spring-tx-4.1.xsd
http://www.springframework/schema/data/jpa http://www.springframework/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework/schema/aop http://www.springframework/schema/aop/spring-aop-4.1.xsd
http://www.springframework/schema/mvc http://www.springframework/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework/schema/util http://www.springframework/schema/util/spring-util-4.1.xsd">
<!-- 配置HandlerMapping -->
<bean id="handlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<!-- 建立请求 和 控制器的对应关系 key 请求路径 hello 是控制器id -->
<property name="mappings">
<props>
<prop key="/hello.do" >hello</prop>
</props>
</property>
</bean>
<!-- 配置控制器对象 -->
<bean id="hello" class="com.xdl.controller.HelloController"></bean>
<!-- 配置视图处理器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
更多推荐
Spring MVC五大核心组件及实现Spring MVC 的步骤
发布评论