web.xml配置详解系列二

session-config

  1. 定义:
    1. <session-config><session-timeout>20</session-timeout></session-config>  
  2. 作用:用于定义整个WEB站点session的有效期限,单位是分钟。

mime-mapping

  1. 定义:
    1. <mime-mapping>  
    2. <extension>扩展名称(如doc)</extension>  
    3. <mime-type>MIME类型如(application/vnd.ms-word)</mime-type>  
    4. </mime-mapping>  
  2. 作用:用于定义扩展名(extension)与MIME类型的对映。一个扩展名对映一个mime-mapping

welcome-file-list

  1. 定义:
    1. <welcome-file-list>  
    2. <welcome-file>login.jsp</welcome>  
    3. </welcome-file-list>  

  2. 作用:用来指定WEB应用首页名称。

error-page

  1. 定义:
    1. <error-page>  
    2. <error-code>错误代码(如404)</error-code>  
    3. <location>对应的页面(如/404.jsp)</location>  
    4. </error-page>  

    或者
    1. <error-page>  
    2. <exception-type>完整的Java异常类型(如java.lang.Exception)</exception-type>  
    3. <location>对应的页面(如/error.jsp)</location>  
    4. </error-page>  

  2. 作用:用来指定发生特定htm的error-code或发生特定Java异常时访问的Web页面
  1. 作用:主要用于设定JSP页面的相关配置。
  2. 常见定义:
    1. <jsp-config>  
    2. <taglib>  
    3. <taglib-uri>URI(定义TLD文件的URI,JSP页面的tablib命令可以经由此URI获取到TLD文件)</tablib-uri>  
    4. <taglib-location>  
    5. TLD文件所在的位置  
    6. </taglib-location>  
    7. </taglib>  
    8. <jsp-property-group/>  
    9. </jsp-config>  

  3. 示例:
    web.xml中
     
    1. <taglib>  
    2. <taglib-uri>http://displaytag.sf.net</taglib-uri>  
    3. <taglib-location>/WEB-INF/displaytag.tld</taglib-location>  
    4. </taglib>  
    5. <taglib>  
    6. <taglib-uri>http://displaytag.sf.net/el</taglib-uri>  
    7. <taglib-location>/WEB-INF/displaytag-el.tld</taglib-location>  
    8. </taglib>  

    JSP页面上
      
    1. <%@ taglib uri="http://displaytag.sf.net" prefix="display" %>  
    2. <display:table name="sessionScope.test" pagesize="15" defaultsort="1" sort="list"  defaultorder="descending" requestURI="" decorator="com.myapp.displaytag.UserTableDecorator">  
    3. <display:caption>  
    4. <font size="1">  
    5. 测试信息表头  
    6. </font>  
    7. </display:caption>  
    8. </display> 
 

定义:

  1. <servlet>  
  2.  <servlet-name>myservlet</servlet-name>  
  3.  <servlet-class>com.myapp.controller.MyFirstServlet</servlet-class>  
  4.  <init-param>  
  5.   <param-name>ServletInitParam</param-name>  
  6.   <param-value>你好</param-value>  
  7.  </init-param>  
  8.  <load-on-startup>0</load-on-startup>  
  9. </servlet>  
  10. <servlet-mapping>  
  11.  <servlet-name>myservlet</servlet-name>  
  12.  <url-pattern>/myFirstServlet.do</url-pattern>  
  13. </servlet-mapping>  

 resource-env-ref元素来指定对管理对象的servlet引用的声明,该对象与servlet环境中的资源相关联

  1. <resource-env-ref>  
  2. <resource-env-ref-name>资源名</resource-env-ref-name>  
  3. <resource-env-ref-type>查找资源时返回的资源类名</resource-env-ref-type>  
  4.   
  5. </resource-env-ref>  


<resource-env-ref-name>:资源的名称 相对于java:comp/env >>context

<resource-env-ref-type>:当web应用查找该资源的时候,返回的Java类名的全称

 

建立步骤:

先在meta-inf目录下新建一个Context.xml文件配置如下:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <Context>  
  3. <!--对于javax.sql.DataSource资源类型 Tomcat容器提供了默认factory org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory -->  
  4. <Resource name="jdbc/mssql" auth="Container"  
  5.           type="javax.sql.DataSource" driverClassName="net.sourceforge.jtds.jdbc.Driver"  
  6.           url="jdbc:jtds:sqlserver://127.0.0.1:1433;DatabaseName=spring3mvc"  
  7.           username="sa" password="wlyoa_)*#!" >  
  8. </Resource>  
  9. <Resource name="bean/MyBeanFactory" auth="Container"  
  10.           type="com.myapp.domain.MyBean"  factory="org.apache.naming.factory.BeanFactory" projectName="我的测试项目">  
  11. </Resource>  
  12. </Context>  

在web.xml中添加资源引用

  1. <!--定义外部资源管理对象 在Web容器目录的conf\Catalina\localhost\工程名.xml文件中(可见示例文件:WebContext.xml) -->  
  2.  <resource-env-ref>  
  3.   <resource-env-ref-name>bean/MyBeanFactory</resource-env-ref-name>  
  4.   <resource-env-ref-type>com.myapp.domain.MyBean</resource-env-ref-type>  
  5.  </resource-env-ref>  
  6.  <resource-env-ref>  
  7.   <resource-env-ref-name>jdbc/mssql</resource-env-ref-name>  
  8.   <resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>  
  9.  </resource-env-ref>  


在Servlet类中可通过如下方式查找或使用资源

   

  1. /** 
  2.  *  
  3.  */  
  4. package com.myapp.controller;  
  5. import java.io.IOException;  
  6. import java.io.PrintWriter;  
  7. import java.sql.Connection;  
  8. import java.sql.ResultSet;  
  9. import java.sql.SQLException;  
  10. import java.sql.Statement;  
  11.   
  12. import javax.naming.Context;  
  13. import javax.naming.InitialContext;  
  14. import javax.naming.NamingException;  
  15. import javax.servlet.ServletException;  
  16. import javax.servlet.http.HttpServlet;  
  17. import javax.servlet.http.HttpServletRequest;  
  18. import javax.servlet.http.HttpServletResponse;  
  19. import javax.sql.DataSource;  
  20.   
  21. import com.myapp.domain.MyBean;  
  22.   
  23. /** 
  24.  * @author louisliao 
  25.  *我的第一个Servlet 
  26.  */  
  27. public class MyFirstServlet extends HttpServlet {  
  28.   
  29.     private String servletInitParam="";  
  30.     /** 
  31.      *  
  32.      */  
  33.     public MyFirstServlet() {  
  34.         // TODO Auto-generated constructor stub  
  35.     }  
  36.   
  37.     public void init(){  
  38.         servletInitParam=this.getInitParameter("ServletInitParam");  
  39.     }  
  40.     public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {  
  41.           
  42.         testDataSourceJNDI();  
  43.         testBeanSourceJNDI();  
  44.         String parame1=getServletContext().getInitParameter("MyContextParam");  
  45.         String projectName=getServletContext().getInitParameter("ProjectName");  
  46.         System.out.println("上下文初始化参数1:"+parame1);  
  47.         System.out.println("上下文初始化参数2:"+projectName);  
  48.         PrintWriter writer=response.getWriter();  
  49.         writer.println("<html><head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>");  
  50.         writer.println(projectName);  
  51.         writer.println("</title></head>");  
  52.         writer.println("<body>");  
  53.         writer.println(servletInitParam+":"+servletInitParam+"<br/>");  
  54.         writer.println("上下文初始化参数1:"+parame1+"<br/>");  
  55.         writer.println("上下文初始化参数2:"+projectName+"<br/>");  
  56.         writer.println("</body></html>");  
  57.     }  
  58.     public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {  
  59.         doGet(request, response);  
  60.     }  
  61.     public void destroy() {  
  62.         super.destroy();  
  63.         System.out.println("我是destroy()方法!用来进行销毁实例的工作");  
  64.     }  
  65.     /** 
  66.      * Java-Bean资源读取测试 
  67.      */  
  68.     private void testBeanSourceJNDI()  
  69.     {  
  70.         try {  
  71.             System.err.println("testBeanSourceJNDI");  
  72.             Context context=new InitialContext();  
  73.             Context envContext=(Context)context.lookup("java:/comp/env");  
  74.             MyBean mBean=(MyBean)envContext.lookup("bean/MyBeanFactory");  
  75.             System.out.println(mBean.getHello()+":"+mBean.getProjectName());  
  76.               
  77.         } catch (Exception e) {  
  78.             // TODO: handle exception  
  79.         }  
  80.     }  
  81.     /** 
  82.      * DataSource资源测试 
  83.      */  
  84.     private void testDataSourceJNDI()   
  85.     {  
  86.         try {  
  87.               
  88.         System.err.println("DataSourceJNDI");  
  89.         Connection connection=null;  
  90.         ResultSet result = null;  
  91.         Statement stmt = null;  
  92.         Context context=new InitialContext();  
  93.         Context envContext=(Context)context.lookup("java:/comp/env");  
  94.         DataSource dataSource=(DataSource)envContext.lookup("jdbc/mssql");  
  95.         try {  
  96.             connection=dataSource.getConnection();  
  97.             stmt=connection.createStatement();  
  98.             result=stmt.executeQuery("select * from Author");  
  99.             while (result.next()) {  
  100.                 System.out.println(result.getString("name"));  
  101.                   
  102.             }  
  103.         } catch (SQLException e) {  
  104.             // TODO Auto-generated catch block  
  105.             e.printStackTrace();  
  106.         }  
  107.         finally{  
  108.             result.close();  
  109.             stmt.close();  
  110.             connection.close();  
  111.         }  
  112.         } catch (Exception e) {  
  113.             // TODO: handle exception  
  114.         }  
  115.     }  
  116. }  


 


 

  1. /** 
  2.  *  
  3.  */  
  4. package com.myapp.domain;  
  5.   
  6. /** 
  7.  * @author louisliao 
  8.  * 
  9.  */  
  10. public class MyBean {  
  11. private String projectName;  
  12. private String hello="欢迎使用";  
  13. public String getProjectName() {  
  14.     return projectName;  
  15. }  
  16. public void setProjectName(String projectName) {  
  17.     this.projectName = projectName;  
  18. }  
  19. public String getHello() {  
  20.     return hello;  
  21. }  
  22. public void setHello(String hello) {  
  23.     this.hello = hello;  
  24. }  
  25.   
  26. }  
  1. servlet-class:可以是<code class="java plain">org.apache.struts.action.ActionServlet或继承该类</code>  
  1.     <servlet>  
  2.         <servlet-name>action</servlet-name>  
  3.         <servlet-class>  
  4.             com.weboa.util.web.EbusinessActionServlet  
  5.         </servlet-class>  
  6.         <init-param>  
  7.             <param-name>config</param-name>  
  8.             <param-value>/WEB-INF/struts-config.xml</param-value>  
  9.         </init-param>  
  10.         <!-- module configurations -->  
  11.         <init-param>  
  12.             <param-name>debug</param-name>  
  13.             <param-value>2</param-value>  
  14.         </init-param>  
  15.         <init-param>  
  16.             <param-name>detail</param-name>  
  17.             <param-value>2</param-value>  
  18.         </init-param>  
  19.         <load-on-startup>2</load-on-startup>  
  20.     </servlet>  
  21.   
  22.     <servlet-mapping>  
  23.         <servlet-name>action</servlet-name>  
  24.         <url-pattern>*.htm</url-pattern>  
  25.     </servlet-mapping>  
  26. <!-- Struts Tag Library Descriptors -->        
  27.   
  28.      <taglib>        
  29.   
  30.         <taglib-uri>struts-bean</taglib-uri>        
  31.   
  32.         <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>        
  33.   
  34.      </taglib>        
  35.   
  36.      <taglib>        
  37.   
  38.          <taglib-uri>struts-html</taglib-uri>        
  39.   
  40.        <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>        
  41.   
  42.      </taglib>        
  43.   
  44.     <taglib>        
  45.   
  46.     <taglib-uri>struts-nested</taglib-uri>        
  47.   
  48.      <taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>        
  49.   
  50.      </taglib>        
  51.   
  52.    <taglib>        
  53.   
  54.         <taglib-uri>struts-logic</taglib-uri>        
  55.   
  56.          <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>        
  57.   
  58.     </taglib>       
  59.      <taglib>        
  60.   
  61.         <taglib-uri>struts-tiles</taglib-uri>        
  62.   
  63.         <taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>        
  64.   
  65.     </taglib>       


<servlet-name>指定servlet的名称
<servlet-class>指定servlet的类的名称
<init-param>用来定义参数,可有多个init-param。在servlet类中通过getInitParamenter(String name)方法访问初始化参数
<load-on-startup>指定web应用启动时,装载servlet的次序。当值为负数或未定义时Servlet容器将在web客户首次访问此servlet时加载它;当值为正数或未定义时Servlet容器先加载数值小的servlet
<url-pattern>指定servlet所对应的URL



指定Spring配置文件位置

  1. <context-param>  
  2.         <param-name>contextConfigLocation</param-name>  
  3.         <param-value>  
  4.             /WEB-INF/spring-dao-bean.xml,/WEB-INF/spring-resources.xml,  
  5.             /WEB-INF/spring-service-bean.xml  
  6.         </param-value>  
  7.     </context-param>  


定义Spring监听器加载Spring

 

  1. <listener>  
  2.     <listener-class>  
  3.         org.springframework.web.context.ContextLoaderListener  
  4.     </listener-class>  
  5. </listener>  


配置Struts

  1. <servlet>  
  2.         <servlet-name>action</servlet-name>  
  3.         <servlet-class>  
  4.             com.weboa.util.web.EbusinessActionServlet  
  5.         </servlet-class>  
  6.         <init-param>  
  7.             <param-name>config</param-name>  
  8.             <param-value>/WEB-INF/struts-config.xml</param-value>  
  9.         </init-param>  
  10.         <!-- module configurations -->  
  11.         <init-param>  
  12.             <param-name>debug</param-name>  
  13.             <param-value>2</param-value>  
  14.         </init-param>  
  15.         <init-param>  
  16.             <param-name>detail</param-name>  
  17.             <param-value>2</param-value>  
  18.         </init-param>  
  19.         <load-on-startup>2</load-on-startup>  
  20.     </servlet>  
  21.   
  22.     <servlet-mapping>  
  23.         <servlet-name>action</servlet-name>  
  24.         <url-pattern>*.htm</url-pattern>  
  25.     </servlet-mapping>  

定义:spring-dao-bean.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"   
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4. xmlns:aop="http://www.springframework.org/schema/aop"   
  5. xmlns:tx="http://www.springframework.org/schema/tx"   
  6. xmlns:jee="http://www.springframework.org/schema/jee"   
  7. xmlns:util="http://www.springframework.org/schema/util"   
  8. xsi:schemaLocation="   
  9.    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  10.    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
  11.    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   
  12.    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd" >  
  13.   
  14.     <bean id="transactionManager"  
  15.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  16.         <property name="dataSource" ref="dataSource" />  
  17.     </bean>  
  18.       
  19.     <bean id="sqlMapClient"  
  20.         class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">  
  21.         <property name="configLocation">  
  22.             <value>  
  23.                 classpath:/com/ibatis/sql-map-config.xml  
  24.             </value>  
  25.         </property>  
  26.         <property name="dataSource" ref="dataSource" />  
  27.     </bean>  
  28. </beans>  



 定义:spring-resources.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="  
  7.        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  
  9.        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
  10.     <!-- 数据库连接 -->  
  11.     <bean id="propertyConfigurer"   
  12.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  13.         <property name="location">  
  14.            <value>/WEB-INF/classes/jdbc.properties</value>  
  15.         </property>  
  16.     </bean>  
  17.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  18.         <property name="driverClassName" value="${jdbc.driver}"/>  
  19.         <property name="url" value="${jdbc.url}"/>  
  20.         <property name="username" value="${jdbc.user}"/>  
  21.         <property name="password" value="${jdbc.password}"/>  
  22.         <property name="maxActive" value="${jdbc.maxActive}"/>  
  23.         <property name="maxIdle" value="${jdbc.maxIdle}"/>  
  24.         <property name="maxWait" value="${jdbc.maxWait}"/>  
  25.         <property name="defaultAutoCommit" value="${jdbc.defaultAutoCommit}"/>  
  26.         <property name="removeAbandoned" value="${jdbc.removeAbandoned}"/>  
  27.         <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}"/>  
  28.     </bean>  
  29. </beans>  

定义Spring-service-bean.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"   
  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.         xmlns:context="http://www.springframework.org/schema/context"   
  5.         xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  7.         http://www.springframework.org/schema/context   
  8.         http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
  9.     <bean id="txProxyTemplate" abstract="true"  
  10.         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
  11.         <property name="transactionManager" ref="transactionManager" />  
  12.         <property name="transactionAttributes">  
  13.             <props>  
  14.                 <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>  
  15.                 <prop key="remove*">PROPAGATION_REQUIRED,-Exception</prop>  
  16.                 <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>  
  17.                 <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>  
  18.                 <prop key="updateFolder">PROPAGATION_REQUIRED, timeout_30</prop>  
  19.                 <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>  
  20.             </props>  
  21.         </property>  
  22.     </bean>  
  23.       
  24.     <bean id="userCache"  
  25.         class="org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache">  
  26.         <property name="cache">  
  27.             <bean  
  28.                 class="org.springframework.cache.ehcache.EhCacheFactoryBean">  
  29.                 <property name="cacheManager">  
  30.                     <bean  
  31.                         class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" />  
  32.                 </property>  
  33.                 <property name="cacheName" value="userCache" />  
  34.             </bean>  
  35.         </property>  
  36.     </bean>  
  37.     <!-- *********************2009.4.24添加,得到acegi中保存的用户信息******************* -->  
  38.     <bean id="sessionRegistry" class="org.acegisecurity.concurrent.SessionRegistryImpl"/>  
  39.       
  40.     <!-- *****************************个人事务************************** -->  
  41.     <!-- 日程安排 -->  
  42.     <bean id="arrangementService" parent="txProxyTemplate">  
  43.         <property name="target">  
  44.             <bean  
  45.                 class="com.weboa.personalService.service.impl.ArrangementServiceImpl">  
  46.                 <property name="arrangementDAO" ref="arrangementDAO" />  
  47.             </bean>  
  48.         </property>  
  49.     </bean>  
  50. </beans>  



 

 定义jdbc.properties

  1. #jdbc\u6570\u636e\u5e93\u8fde\u63a5\u914d\u7f6e\u4fe1\u606f  
  2. jdbc.driver=net.sourceforge.jtds.jdbc.Driver  
  3. jdbc.url=jdbc\:jtds\:sqlserver\://127.0.0.1\:1433;DatabaseName\=wlynew  
  4. jdbc.user=sa  
  5. jdbc.password=wlyoa_)*\#\!  
  6. jdbc.maxActive=100  
  7. jdbc.maxIdle=30  
  8. jdbc.maxWait=1000  
  9. jdbc.defaultAutoCommit=false  
  10. jdbc.removeAbandoned=true  
  11. jdbc.removeAbandonedTimeout=60  


以上是基于Spring+Struts+Ibatis的配置


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值