您的位置:首页 > 其它

SSM整合之web.xml的相关配置

2018-02-16 09:02 351 查看

SSM整合之web.xml的相关配置

一、注册ServletContext监听器

<listener>
    <listener-class>

            org.springframework.web.context.ContextLoaderListener

   </listener-class>

</listener>
ps:为了在ServletContext初始化的时候创建Spring容器,就需要使用监听器,接口ServletContextListener对ServletContext进行监听,在web.xml中注册该监听器。Spring为该监听器接口定义了一个实现类,它完成了两个很重要的工作:创建容器对象,并将容器对象放入到了ServletContext的属性空间中,保证了整个应用中Spring容器的唯一性

二、指定Spring配置文件的位置及名称

        <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:resources/spring-*.xml</param-value>
</context-param>

ps:ContextLoaderListener在对Spring容器进行创建时,需要加载Spring配置文件。其默认的Spring配置文件位置与名称为:WEB-INF/applicationContext.xml。但一般会将该配置文件放置于项目的classpath下,即src下,所以需要在web.xml中对Spring配置文件的位置及名称进行指定。

三、注册字符集过滤器

    <filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>

</filter>

        <filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

ps:注册字符集过滤器,用于解决请求参数中携带中文时产生乱码问题

四、注册Springmvc中央调度器

        <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:resources/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>

</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
ps:配置中央调度器时需要注意,Springmvc的配置文件名要与其他Spring配置文件名称格式相同。这样在上一步中指定所有Spring配置文件时,就将Springmvc的配置文件也包含进来,这样,就使得Spring容器在初始化时,将Springmvc配置文件中注册Bean也进行创建

c337
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: