1.解析SpringMVC的请求
每当用户在Web浏览器中点击链接或者提交表单的时候请求就将开始工作了。
SpringMVC所有的请求都会通过一个前段控制器Servlet。前段控制器是常用的web应用程序模式,在这里一个但是实例的servlet将请求委托给应用程序的其他组件来执行实际的处理。在SpringMVC中,DispatcherServlet就是前段控制器。
控制器在完成逻辑处理后,通常会产生一些信息,这些西悉尼需要返回给用户并在浏览器上显示。这些信息被称为模型,
控制器所做的最后一件事就是将模型数据打包,并且表示出用于渲染输出的视图名称。它接下来会将请求连同模型和视图名称发送回DispatchServlet
2.springmvc环境搭建
<servlet>
<servlet-name>user</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/org/sh/xml/user-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>user</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
通过将DispatcherServlet映射到/声明了他会作为默认的饿servlet并且会处理所有的请求,包括对静态资源的请求
Spring的MVC命名空间包含了一个新的<mvc:resource>他会处理静态资源的请求。只需要在Spring 配置文件中对其进行配置。
2.配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.qunar.controller"></context:component-scan>
<mvc:annotation-driven/>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
<mvc:annotation-driven/>
启动SpringMVC的注解功能
<context:component-scan base-package="com.qunar.controller"></context:component-scan>
自动扫描包
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
当DispatcherServlet要求InternalResourceViewResolver 解析视图的时候,他将获取一个逻辑视图名称,添加/WEB-INF/jsp/前缀 和“.jsp”后缀。得到的结果就是渲染输出的jsp路径,在内部InternalResourceViewResolver 接下来会将这个路径传递给VIEW对象,VIEW对象将请求传递给JSP所以,当HomeController返回home作为逻辑试图名称的时候,他最终将被解析成
/WEB-INF/jsp/home.jsp 路劲
3.Spring 应用上下文
DispatcherServlet会根据一个XML文件来加载其中Spring应用上下文,而这个文件基于他的<Servlet-name>属性来确定。
ContextLoaderListener是一个servlet监听器,除了DispatcherServlet创建的应用上下文以外,他能够加载其他的配置文件到一个Spring应用上下文中,为了是使用它,需要在ContextLoaderListener,需要在web.xml文件中添加如下的<listener>声明:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
如果,没有指定要加载哪些配置文件。上下文加载器就会在/WEB-INFO/applicationContext.xml这个Spring配置文件。但是这个文件本省没有做到将应用上下文分为多个片段。所以我们需要重写默认实现。
为了给ContextLoaderListener指定一个或多个Spring配置文件,需要在Servlet上下文中配置ContextConfigLocation参数:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/org/sh/xml/applicationContext.xml
</param-value>
</context-param>
contextConfigLocation参数指定了一个路径的列表,除非特别声明,路劲是相对于应用根目录的,但是我们的Spring配置被分成了多个XML文件,并分散在WEB应用程序的多个JAR文件,所以对其中的一些我们添加了classPath:前缀,使得他们能够以资源的方式在应用程序中的类路径中加载,而其他的文件则添加了web应用几个程序的本地路径。
未完待续
4.在Spring中获取json数据
1)加入如下依赖
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
</dependency>
2)编写Controller
@RequestMapping(value="/json",method=RequestMethod.GET)
@ResponseBody
public List<User> getUserJson(){
List<User> users = null;
try {
users = userMapper. selectUsers();
} catch (Exception e) {
e.printStackTrace();
}
return users;
}
3)打开mvc注解功能
<mvc:annotation-driven/>