1: web.xml学名叫部署描述符文件,是在Servlet规范中定义的,是web应用的配置文件。
2: web.xml对大小写敏感,对其中元素的先后顺序也敏感
3: 常见的几个元素:
3.1: welcome-file-list
<welcome-file-list>
<welcome-file>login.htm</welcome-file>
<welcome-file>login.html</welcome-file>
<welcome-file>login.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
welcome-file用来指定首页文件名称
welcome-file用来指定首页文件名称.我们可以用welcome-file指定几个首页,而服务器会依照设定的顺序来找首页.
3.2: display-name
<display-name>XXXX平台</display-name>
定义应用名称。
3.3 error-page
error-page元素包含三个子元素error-code,exception-type和location.将错误代码(Error Code)或异常(Exception)的种类对应到web应用资源路径.
<error-page>
<error-code>500</error-code>
<location>/errorDisp.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/errorDisp.jsp</location>
</error-page>
3.4 context-param
context-param 元素用来设定web应用的环境参数(context),它包含两个子元素:
param-name和param-value.
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webApp.intergratedPlatform</param-value>
</context-param>
//配置日志
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
3.5
filter元素用来设定web应用的过滤器,它的两个主要子元素filter-name和filter-class用来定义Filter所对应的class
filter-mapping 元素的两个主要子元素filter-name和url-pattern.用来定义Filter所对应的URL.
<filter>
<filter-name>expiresFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>expiresFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.6
servlet元素的两个主要子元素servlet-name和servlet-class用来定义servlet所对应的class
servlet-mapping元素包含两个子元素servlet-name和url-pattern.用来定义servlet所对应URL.
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
以上参考自https://blog.csdn.net/heaveneleven/article/details/37911531
spring配置文件在web.xml中被加载 如:
//配置前端控制器
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
spring配置文件内部可以引用其他的Spring配置文件,如:
<!--关联Spring的配置文件-->
<import resource="classpath:Application.xml"/>
一般将spring配置文件分为两个mvc.xml 和applicationContext.xml (也可以自定义,如:spring-servlet.xml与applicationContext.xml)
1: mvc.xml中的主要内容:
1.1: 注册拦截器
<mvc:interceptors>
<mvc:interceptor>
<!--对哪些请求做拦截-->
<mvc:mapping path="/**"/>
<!--跳过哪些请求-->
<mvc:exclude-mapping path="/login"/>
<!--拦截器对象-->
<bean class="com_fm.web.interceptor.CheckLoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
~~~~~~~~~~~~~该内容有待更新 ~~~~~~~~~
2: applicationContext.xml中的主要内容:
2.1: 创建sqlSessionFactory对象:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载mybatis的配置文件 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
2.2: 扫描mapper:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定扫描的包名 如果扫描多个包,每个包中间使用半角逗号分隔 -->
<property name="basePackage" value="com.jm.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
2.3: 事务管理
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="import*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="append*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception" />
</tx:attributes>
</tx:advice>
2.4: 切面设置
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* com.jm.serviceDao..*(..))" />
<aop:advisor pointcut-ref="transactionPointcut"
advice-ref="transactionAdvice" />
</aop:config>