SpringMVC学习笔记(1)-----所需的配置文件

来源:互联网 发布:网络广告公司价格表 编辑:程序博客网 时间:2024/06/08 18:42

配置DispatcherServlet

在WEB-INF中的web.xml中,加入如下配置:
(0)Maven生成的工程目录中的web.xml比较老,建议将头部替换成2.4版本以上。

<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">

(1)添加Spring的配置文件,存放目录在<param-value>中给出。

    <!-- Spring应用上下文,理解层次化的ApplicationContext -->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>/WEB-INF/configs/spring/applicationContext*.xml</param-value>    </context-param>

(2)配置Servlet的名字以及Servlet的类(DispatcherServlet)
(3)SpringMVC默认的配置文件是WebContent/WEB-INF/[servlet-name]-servlet.xml。
若不想使用默认配置文件,有两种方法。
第一种方法:<servlet>标签中通过<init-param>子标签配置SpringMVC的xml文件路径。
第二种方法:可以通过在web.xml文件中添加servlet侦听器ContextLoaderListener来自定义此文件名和位置。

<!-------- DispatcherServlet definition goes here-----><context-param>   <param-name>contextConfigLocation</param-name>   <param-value>/WEB-INF/HelloWeb-servlet.xml</param-value></context-param><listener>   <listener-class>      org.springframework.web.context.ContextLoaderListener   </listener-class></listener>

(4)配置servlet-mapping的映射关系,使该servlet拦截指定的URL。

<display-name>Spring MVC Application</display-name>   <servlet>      <servlet-name>DispatcherServlet的名字</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>            <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>HelloWeb</servlet-name>      <url-pattern>*.jsp</url-pattern>   </servlet-mapping>

Spring的配置文件

就是上面写的/WEB-INF/configs/spring/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:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd">    <!-- Spring的注解支持 -->    <context:annotation-config />    <!-- Spring的自动扫描组件,除了Controller标记 -->    <context:component-scan base-package="com.shen.mvcdemo">        <context:exclude-filter type="annotation"            expression="org.springframework.stereotype.Controller" />    </context:component-scan></beans>

配置SpringMVC的xml文件

就是上面写的WEB-INF/configs/spring/mvc-dispatcher-servlet.xml文件。
(1)配置好相关的SpringMVC的命名空间。一般老夫使用复制粘贴。

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd">    <!-- 本配置供工名为mvc-dispatcher的DispatcherServlet使用,提供其相关的Spring MVC配置 -->    <!-- 启用Spring基于annotation的DI,使用户可以在Spring MVC中使用Spring的强大功能 。 -->    <!-- 激活@Required @AutoWire, JSR 250's @PostConsttruct,@PreDestory and @Resouce等标注 -->    <context:annotation-config />    <!-- 启动注释扫描功能,Spring的各种注释可以被解析为Bean,并加入搜索@Controller注释 -->    <context:component-scan base-package="com.shen.mvcdemo">        <context:include-filter type="annotation"expression="org.springframework.stereotype.Controller" />    </context:component-scan>    <!-- HandlerMapping,无需配置,Spring MVC可以默认启动 。 DefaultAnnotationHandlerMapping         annotation-driven HandlerMapping -->    <!-- 扩充了注解驱动,可以将请求参数绑定到控制器参数 -->    <mvc:annotation-driven />    <!-- 静态资源处理,css,js,imgs -->    <mvc:resources location="/resources/" mapping="/resources/**" />    <!-- 配置ViewResolver。 可以使用多个ViewResolver。 使用order属性排序。 InternalResourceViewResolver放在最后。 -->    <!-- 文件上传需要的 -->    <bean id="multipartResolver"        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">        <property name="maxUploadSize" value="10485760" />    </bean><!-- 定义用于解析视图名称的规则,放在最后,需要在/WEB-INF/目录下创建jsps文件夹,存放可解析的jsp文件 -->    <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"></property>    </bean></beans>

配置文件到此结束—待添加

原创粉丝点击