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

web.xml中配置spring监听,日志记录,编码集等,spring的mvc

2014-01-13 22:30 555 查看
<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>

             classpath:applicationContext-*.xml

          </param-value>

</context-param>

<listener>

    <listener-class>

      org.springframework.web.context.ContextLoaderListener

     </listener-class>

</listener>

contextConfigLocation指定了配置文件的路径。如果不配置默认的路径是在WEB-INFO下面。通配符可以加载所有匹配的文件

ContextLoaderListener监听器的作用在web容器启动时候可以加载applicationContext的相关配置信息,他实现了ServletContextListener接口。在

web容器启动的时候就可以执行他实现的方法。ContextLoaderListener implements ContextServletListener实现它里面的contextInitialized方法

public void contextInitialized (ServletContextEvent event) {

              this.contextLoader = createContextLoader();

              this.contextLoader.initWebApplicationContext(event.getServletContext()); 

public ContextLoader createContextLoader(){

     return new ContextLoader():

}

 }此方法里面通过contextLoader对象初始化web应用。此方法是spring容器的启动入口

配置web应用的编码集

<filter>

   <filter-name>encodingFilter</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>   过滤器的匹配 URL

   <filter-name>encodingFilter</filter-name>

   <url-pattern>/*</url-pattern>

 </filter-mapping>

还可以配置日志文件

设置日志的跟踪:

① 指定 Log4J 配置文件的地址

<context-param>    

 <param-name>log4jConfigLocation</param-name> 

 <param-value>/WEB-INF/log4j.properties</param-value>  

</context-param> 



② 使用该监听器初始化 Log4J 日志引擎 

<listener> 

    <listener-class> org.springframework.web.util.Log4jConfigListener</listener-class>  

</listener> 

 

 

 

 

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