web.xml文件的加载和配置

一、web.xml文件是项目的核心配置文件,很多配置信息都是通过该文件配置、加载、初始化;
1. web.xml文件的加载与其在web.xml文件中的顺序无关;加载顺序为,ServletContext(context-param) -> listener - > filter ->servlet
2. spring 核心文件:applicationContext.xml文件。web.xml文件加载applicationContext.xml

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

3. spring-mvc.xml 文件是springMVC 核心文件。web.xml 加载spring-mvc.xml文件

<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:config/spring-mvc.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
    <async-supported>true</async-supported> 
</servlet>

4. struts2.xml文件,web.xml加载struts2.xml 文件;

<filter> 
    <filter-name>struts2</filter-name> 
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
    <init-param> <param-name>config</param-name> 
        <param-value>struts-default.xml,struts-BsfwtWeb-main.xml</param-value> 
    </init-param> 
</filter> 
<filter-mapping> 
   <filter-name>struts2</filter-name> 
   <url-pattern>*.do</url-pattern> 
</filter-mapping>

二、 applicationContext.xml文件作用:
1 主要是配置spring注解开启,spring依赖注入,数据库相关配置,数据源(如果有多个数据库,可以配置多个数据源)、配置和spring-mybatis.xml文件、配置事务等信息;
2. applicationContext.xml文件定位到xx.properties文件:如果是在/WEB-INF/目录下的话,该位置为: /WEB-INF/文件名.后缀

<context:property-placeholder location="classpath:jdbc.properties"/> 

三、 spring-mvc.xml文件:
1. web容器启动时,读取web.xml配置 文件,首先都去applicationContext.xml文件,其次是spring-mvc文件
2. spring-mvc.xml 文件:启动注解(controller注解)、扫描controller包注解、静态资源映射、试图解释、文件上传、返回消息json配置;

四、 web.xml文件中servlet的配置:
1. 编写对应的servelt类。

2. web.xml文件中的配置,配置servlet信息;

<servlet>
    <servlet-name>bondeServiceServlet</servlet-name> 
    <servlet-class>cn.com.servyou.bonde.gate.entrance.EntranceServlet</servlet-class> 
    <load-on-startup>2</load-on-startup> 
</servlet> 
   <--! servlet映射,和上同名,url-pattern 是访问路劲的后缀; -->
<servlet-mapping> 
    <servlet-name>bondeServiceServlet</servlet-name> 
    <url-pattern>/bondegate/bondeServiceServlet</url-pattern> 
</servlet-mapping>

3. 访问路劲:http://localhost/bondegate/bondeServiceServlet

五、 web.xml中的filter (过滤器):
1. web.xml中定义filter:

    <filter>
		<filter-name>EncodingFilterUtf-8</filter-name>
		<filter-class>
			cn.com.jdls.foundation.web.EncodingFilter
		</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>EncodingFilterUtf-8</filter-name>
		<url-pattern>*.ajax</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>EncodingFilterUtf-8</filter-name>
		<url-pattern>*.ado</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>EncodingFilterUtf-8</filter-name>
		<url-pattern>*.do</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>EncodingFilterUtf-8</filter-name>
		<url-pattern>/api/*</url-pattern>
	</filter-mapping>

2.    定义对应的类:类似于servlet的定义。
    
六、  总结:web.xml是项目的启动配置文件(非必须,但是其它的配置文件都是通过这个文件加载的)。若使用struts2框架,则加载struts2.xml配置文件通过filter(过滤器)实现加载;使用springMVC框架则通过servlet配置加载;spring.xml文件则通过配置监听器配置经行启动加载;最后mybatis.xml文件也是通过spring.xml文件进行加载(在spring.xml文件中引入mybatis配置文件),至此,web.xml 文件的加载问题结束;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值