您的位置:首页 > 编程语言 > Java开发

白话Spring(中级篇)---前端控制器

2016-03-18 22:55 239 查看
[一知半解,就是给自己挖坑]

在前面helloworld工程中,我们已经展示了基本的MVC结构及使用方法。接下来,我们更加深入的学习一下SpringMVC中的前端控制器的内容。即

DispatcherServlet的详细介绍。

--------------------------------------------------------------------------------------------------------------------------------------
1.概念回顾。

DispatcherServlet:是web工程中用户访问的入口。是分派调度各个请求的指挥官。是流程控制的核心!请参考前文官网示例图来理解。

2.helloworld工程中的配置方式:【注:此配置在web.xml中,详情见前文工程实例】
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
解释:
<servlet-name>:其值可以自定义,但是需要主要的是要与下方的<servlet-mapping>中的<servlet-name>对应,保持一致。
<servlet-class>:这里配置的就是我们的前端控制器所属的位置,由包名+类名组成。
<init-param>:表示初始化的参数,这里如果不配置的话会采用默认的配置方式,为了保证程序正确性,这里我们手动的配置了上下文关系配置的名称与配置文件的路径。
<url-pattern>:此处配置的前端控制器需要拦截的请求的地址形式。常见的配置为:*.do,*.htm等。但是,特别特别特别注意“/*”表示拦截所有的请求,这时我们将不会在程序中捕获任何请求,因为所有的请求都被前端控制器给拦截下来了。
另外,还有一个常见的配置项:<load-on-startup>1</load-on-startup>中间的数字表示启动顺序,数字越小表示启动级别越高。注意其最小值为0.当是一个负数时或者没有指定时,则指示容器在该servlet被选择时才加载。
3.上下关系配置:
在集成web的环境下,一般我们会采用如下的配置方式单独的设置用户的上下文关系。其作用是加载Service层,dao层的服务。这里我们给出一个经典的配置。供读者参考:
首先,在web.xml中:【注:下面配置文件只为说明配置方式, 不代表完整的web.xml配置】
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/applicationContext.xml,
classpath*:/applicationContext-*.xml,
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

contextConfigLocation:表示用于加载Spring的Bean的配置文件;注意:此名称固定

listener:监听加载bean的实现类,此处也是固定,默认使用webapplicationContext
注意:此处的上下文环境文件可以配置多份,来满足我们不同框架下的需求。

接着,配置spring-mvc.xml文件,具体内容如下:【注:下面配置文件只为说明配置方式, 不代表完整的spring-mvc.xml配置】
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 自动扫描且只扫描@Controller -->
<context:component-scan base-package="com.java.ingo" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"></property>
</bean>

</beans>


然后,配置applicationContext.xml文件,具体内容如下:【注:下面配置文件只为说明配置方式, 不代表完整的spring-mvc.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd" default-lazy-init="true">

<description>Spring公共配置</description>

<!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 -->
<context:component-scan base-package="com.yonyou.ingo">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>

<!-- 使用annotation定义事务 -->
<aop:aspectj-autoproxy/>
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

<!--省略部门内容-->
</beans>


4.前端控制器中的上文加载顺序。
第一步:项目启动时,容器加载web.xml
第二步:加载上下文配置即ContextLoaderListener,此处会将引入的配置文件逐一进行加载。
第三步:初始化MVC上下文,即加载注解,视图解析器,web组件等。
由此发现,我们推荐的做法是将上下文配置的位置尽量的放置在web.xml顶部,防止意外的错误。并且,一旦上下文配置文件加载之后便能够在全局范围内被使用。
另外,从上面的示例配置,也可以看出,前端控制器只负责controller层的配置解析。其他的配置交给上下文来配置管理。
------------------------------------------------------------------------------------------------------------------------------------
至此,白话Spring(中级篇)---前端控制器结束

备注:
我们在中级篇仅对前端控制器中的通用配置,加载顺序,进行简单讲解,对于源码的解读,将会放在高级篇中进行详细叙述,敬请期待!

参考资料:

参考资料:

Spring官网

其他博文:http://jinnianshilongnian.iteye.com/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: