目录
1.Spring MVC组件与流程
2.Spring MVC实例
1.Spring MVC组件与流程
组件和流程图
当一个请求到来时,DispatcherServlet首先通过请求和事先解析好的HandlerMapping配置,找到对应的处理器(Handler),准备开始运行处理器和拦截器组成的执行链,运行处理器需要有适配器(HandlerAdapter),通过适配器就能运行对应的处理器和拦截器。在处理器返回模型和视图给DispatcherServlet后,DispatcherServlet就会把对应的视图信息传递给视图解析器(ViewResolver)
具体的流程图
2.Spring MVC实例
文件结构
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<!--配置Spring IOC配置文件路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!--配置ContextLoaderListener用以初始化Spring IOC容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置DispatcherServlet-->
<servlet>
<!--需要配置一个/WEB-INF/dispatcher-servlet.xml文件-->
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--使得Dispatcher在服务器启动的时候就初始化-->
<load-on-startup>2</load-on-startup>
</servlet>
<!--Servlet拦截配置-->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
- 系统变量contextConfigLocation配置会告诉Spring MVC其Spring IOC配置文件在哪里,默认值是/WEB-INF/applicationContext.xml
- ContextLoaderListener实现了接口ServletContextListener, 其作用是可以在整个Web工程前后加入自定义代码,所以可以在Web工程初始化之前,它先完成对Spring IOC容器的初始化
- 配置DIspatcherSerlvet,首先是配置了servlet-name为dispatcher,意味着需要一个/WEB-INF/dispatcher-servlet.xml文件(注意,servlet-name和文件名的对应关系)与之对应,并且设置了服务器启动时就初始化它
- 配置DispatcherServlet拦截器以后缀"do"结尾的请求,以"do"结尾的请求都会被它拦截
applicationContext.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--使用注解驱动-->
<context:annotation-config/>
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--数据库连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${db.driverClassName}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
<!--集成MyBatis-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--数据库-->
<property name="dataSource" ref="dataSource"/>
<!--MyBatis全局配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--Mapper映射器配置路径-->
<property name="mapperLocations" value="classpath*:mappers/*Mapper.xml"/>
</bean>
<!--配置数据库事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启基于事务的注解-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--采用自动扫描方式创建mapper bean-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.whc.mapper"/>
<property name="annotationClass" value="org.springframework.stereotype.Repository"/>
</bean>
</beans>
dispatcher-servlet.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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--定义扫描装载的包-->
<context:component-scan base-package="cn.whc"/>
<!--使用注解驱动Spring MVC-->
<mvc:annotation-driven/>
<!--定义视图解析器-->
<!--找到Web工程/WEB-INF/JSP文件夹,且文件结尾为jsp的文件作为映射-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
RoleController
@Controller
@RequestMapping("/role")
public class RoleController {
@Autowired
private RoleService roleService = null;
@RequestMapping(value = "/getRole", method = RequestMethod.GET)
public ModelAndView getRole(@RequestParam("id") Long id) {
Role role = roleService.getRole(id);
ModelAndView mv = new ModelAndView();
mv.addObject("role", role);
// 指定视图类型
mv.setView(new MappingJackson2JsonView());
return mv;
}
}
注意: 要添加jackson-databind这个jar包,才能显示JSON数据
效果: