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

Spring MVC入门教程(二)

2015-08-16 19:42 691 查看
第一篇文章宏观讲了Spring MVC概念,以及分享了一个快速入门的例子

这篇文章主要来谈谈Spring MVC的配置文件。

首先来谈谈web.xml: web项目启动时自动加载到内存中的信息,比如服务器配置参数,<listener>监听器,<filter>过滤器,<servlet>等。再如,如果在项目中使用了spring框架,则必须定义ContextLoaderListener,那么在启动Web容器时,会自动装配Spring
applicationContext.xml的配置信息。这里贴出一个典型的web.xml文件,注释写的很详细:

<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
<display-name>Spring MVC</display-name>

<!-- 这里是指定 Spring配置文件:contextConfigLocation的位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/configs/spring/applicationContext*.xml</param-value>
</context-param>

<!-- 启动Spring容器需要的类文件 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<!-- DispatcherServlet, Spring MVC的核心 -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- DispatcherServlet对应的上下文配置, 默认为/WEB-INF/$servlet-name$-servlet.xml
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 指定Spring MVC的配置文件的具体位置,这一参数可以不指定,那就是默认设置,上面有说 -->
<param-value>/WEB-INF/configs/spring/mvc-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<!-- "/"写法,mvc-dispatcher拦截所有的请求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app></span>


再来说下Spring MVC配置文件mvc-dispatcher-servlet.xml:web项目启动时,在启动相应的servlet时会配置Spring
MVC。一个典型的Spring MVC的配置文件如下:

<pre name="code" class="html"><span style="font-size:14px;"><?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">

<!-- DispatcherServlet使用, 提供其相关的Spring MVC配置 -->

<!-- 启用Spring基于annotation的DI-->
<context:annotation-config />

<!-- DispatcherServlet上下文, 只管理@Controller类型的bean, 忽略其他型的bean, 如@Service -->
<context:component-scan base-package="com.xidian.mvcdemo">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<!-- HandlerMapping, 无需配置, Spring MVC可以默认启动。 DefaultAnnotationHandlerMapping
annotation-driven HandlerMapping -->

<!-- 扩充了注解功能,可以将请求参数绑定到控制器参数,比如可以在类中的某个方法上加上,进一步指定url,极大提高开发效率 -->
<mvc:annotation-driven />

<!-- 静态资源处理, css, js, imgs等,为web项目映射具体的资源路径-->
<mvc:resources mapping="/resources/**" location="/resources/" />

<!-- 配置ViewResolver,视图解析器,还可以有多个,只要附上相应的order即可 ,实际运行时会次序加载-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsps/" />
<property name="suffix" value=".jsp" />
</bean>

<!-- Spring mvc 提供为了实现文件的上下传 -->
<!--200*1024*1024即200M resolveLazily属性启用是为了推迟文件解析,以便捕获文件大小异常 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="209715200" />
<property name="defaultEncoding" value="UTF-8" />
<property name="resolveLazily" value="true" />
</bean>

</beans></span>



最后来说下Spring容器的配置文件applicationContext.xml:这里没太多涉及,具体的配置细节可以看《Spring入门教程(一)》,在后面的Mybatis系列文章中会结合来做一个简单的demo,Spring
MVC入门系列就是三篇文章,还有一篇文章会具体说些开发中常用的标签的注意事项和一些Spring MVC开发思想。


<span style="font-size:14px;"><?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.springframewor                          k.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">

<!-- 启用Spring基于annotation的DI-->
<context:annotation-config />

<context:component-scan base-package="com.xidian.mvcdemo">
<!-- 提示Spring不需要管理com.xidian.mvc.demo下Controller属性修饰的对象 -->
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>

</beans></span>


你看会了吗?欢迎讨论http://blog.csdn.net/code_7
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: