web.xml配置文件

在web.xml里配置需要加载的spring配置文件,如果要装入多个配置文件,在param-value标记中用逗号作分隔符即可:

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

ContextLoaderListener会去加载context-param中指定的配置文件:

<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

IntrospectorCleanupListener它主要负责处理由 JavaBean Introspector 功能而引起的缓存泄露,是Introspector 缓存清除监听器:

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

RequestContextFilter实现ServletRequestListener监听器接口,该监听器监听HTTP请求事件,Web服务器接收的每次请求都会通知该监听器。通过配置RequestContextFilter,Spring容器与Web容器结合的更加密切:

<listener>
		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

filter过滤器,filter-name标签指定此过滤器的名字,filter-class标签指定此过滤器指向的类,此类必须实现javax.servlet.Filter接口。
filter-mapping标签用来关联一个或多个servlet或jsp页面。注意无论有多少filter-mapping,他们的filter-name必须与前面的名字一致:

<filter>
	<filter-name>basepathFilter</filter-name>
	<filter-class>com.common.filter.BasePathFilter</filter-class>
</filter>
<filter-mapping>
	<filter-name>basepathFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

filter过滤器,解决中文乱码用spring经典编码过滤器org.springframework.web.filter.CharacterEncodingFilter:

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

DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,而且负责职责的分派,而且与Spring IoC容器无缝集成,从而可以获得Spring的所有好处。
init-param:Spring Web MVC框架将加载“classpath:spring-mvc.xml”来进行初始化上下文。
load-on-startup:表示启动容器时初始化该Servlet。
url-pattern:表示哪些请求交给Spring Web MVC处理

**

<servlet>  
    <servlet-name>chapter2</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>  
    <init-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:spring-mvc.xml</param-value>  
    </init-param>  
</servlet>
<servlet-mapping>  
    <servlet-name>chapter2</servlet-name>  
    <url-pattern>/</url-pattern>  
</servlet-mapping>  

设置session超时。Session超时理解为:浏览器和服务器之间创建了一个Session,由于客户端长时间(休眠时间)没有与服务器交互,服务器将此Session销毁,客户端再一次与服务器交互时之前的Session就不存在了。

<session-config>
		<session-timeout>15</session-timeout>
</session-config>

welcome-file-list是一个配置在web.xml中的一个欢迎页,用于当用户在url中输入工程名称或者输入web容器url(如http://localhost:8080/)时直接跳转的页面

<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

web.xml中有两种配置error-page的方法,一是通过错误码来配置,500错误(即服务器内部错误)时,跳转到错误处理页面error.jsp:

<error-page>
	<error-code>404</error-code>
	<location>/404.jsp</location>
</error-page>

二是通过异常的类型来配置,java.lang.NullException(即空指针异常)时,跳转到错误处理页面error.jsp:

<error-page>  
        <exception-type>java.lang.NullException</exception-type>  
        <location>/error.jsp</location>  
</error-page> 

在web.xml配置指定jsp标签库,tld标签库更新,jsp里面代码不用任何改变,这样就降低了代码和tld文件的耦合程度,便于维护和迁移。

<taglib>
<taglib-uri>/WEB-INF/tlds/c.tld</taglib-uri>
<taglib-location>/WEB-INF/tlds/c.tld</taglib-location>
</taglib>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值