session-config
- 定义:
- <session-config><session-timeout>20</session-timeout></session-config>
- 作用:用于定义整个WEB站点session的有效期限,单位是分钟。
mime-mapping
- 定义:
- <mime-mapping>
- <extension>扩展名称(如doc)</extension>
- <mime-type>MIME类型如(application/vnd.ms-word)</mime-type>
- </mime-mapping>
- 作用:用于定义扩展名(extension)与MIME类型的对映。一个扩展名对映一个mime-mapping
welcome-file-list
- 定义:
- <welcome-file-list>
- <welcome-file>login.jsp</welcome>
- </welcome-file-list>
- 作用:用来指定WEB应用首页名称。
error-page
- 定义:
- <error-page>
- <error-code>错误代码(如404)</error-code>
- <location>对应的页面(如/404.jsp)</location>
- </error-page>
或者
- <error-page>
- <exception-type>完整的Java异常类型(如java.lang.Exception)</exception-type>
- <location>对应的页面(如/error.jsp)</location>
- </error-page>
- 作用:用来指定发生特定htm的error-code或发生特定Java异常时访问的Web页面
- 作用:主要用于设定JSP页面的相关配置。
- 常见定义:
- <jsp-config>
- <taglib>
- <taglib-uri>URI(定义TLD文件的URI,JSP页面的tablib命令可以经由此URI获取到TLD文件)</tablib-uri>
- <taglib-location>
- TLD文件所在的位置
- </taglib-location>
- </taglib>
- <jsp-property-group/>
- </jsp-config>
- 示例:
web.xml中
- <taglib>
- <taglib-uri>http://displaytag.sf.net</taglib-uri>
- <taglib-location>/WEB-INF/displaytag.tld</taglib-location>
- </taglib>
- <taglib>
- <taglib-uri>http://displaytag.sf.net/el</taglib-uri>
- <taglib-location>/WEB-INF/displaytag-el.tld</taglib-location>
- </taglib>
JSP页面上
- <%@ taglib uri="http://displaytag.sf.net" prefix="display" %>
- <display:table name="sessionScope.test" pagesize="15" defaultsort="1" sort="list" defaultorder="descending" requestURI="" decorator="com.myapp.displaytag.UserTableDecorator">
- <display:caption>
- <font size="1">
- 测试信息表头
- </font>
- </display:caption>
- </display>
定义:
- <servlet>
- <servlet-name>myservlet</servlet-name>
- <servlet-class>com.myapp.controller.MyFirstServlet</servlet-class>
- <init-param>
- <param-name>ServletInitParam</param-name>
- <param-value>你好</param-value>
- </init-param>
- <load-on-startup>0</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>myservlet</servlet-name>
- <url-pattern>/myFirstServlet.do</url-pattern>
- </servlet-mapping>
resource-env-ref元素来指定对管理对象的servlet引用的声明,该对象与servlet环境中的资源相关联
- <resource-env-ref>
- <resource-env-ref-name>资源名</resource-env-ref-name>
- <resource-env-ref-type>查找资源时返回的资源类名</resource-env-ref-type>
- </resource-env-ref>
<resource-env-ref-name>:资源的名称 相对于java:comp/env >>context
<resource-env-ref-type>:当web应用查找该资源的时候,返回的Java类名的全称
建立步骤:
先在meta-inf目录下新建一个Context.xml文件配置如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <Context>
- <!--对于javax.sql.DataSource资源类型 Tomcat容器提供了默认factory org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory -->
- <Resource name="jdbc/mssql" auth="Container"
- type="javax.sql.DataSource" driverClassName="net.sourceforge.jtds.jdbc.Driver"
- url="jdbc:jtds:sqlserver://127.0.0.1:1433;DatabaseName=spring3mvc"
- username="sa" password="wlyoa_)*#!" >
- </Resource>
- <Resource name="bean/MyBeanFactory" auth="Container"
- type="com.myapp.domain.MyBean" factory="org.apache.naming.factory.BeanFactory" projectName="我的测试项目">
- </Resource>
- </Context>
在web.xml中添加资源引用
- <!--定义外部资源管理对象 在Web容器目录的conf\Catalina\localhost\工程名.xml文件中(可见示例文件:WebContext.xml) -->
- <resource-env-ref>
- <resource-env-ref-name>bean/MyBeanFactory</resource-env-ref-name>
- <resource-env-ref-type>com.myapp.domain.MyBean</resource-env-ref-type>
- </resource-env-ref>
- <resource-env-ref>
- <resource-env-ref-name>jdbc/mssql</resource-env-ref-name>
- <resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
- </resource-env-ref>
在Servlet类中可通过如下方式查找或使用资源
- /**
- *
- */
- package com.myapp.controller;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.naming.Context;
- import javax.naming.InitialContext;
- import javax.naming.NamingException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.sql.DataSource;
- import com.myapp.domain.MyBean;
- /**
- * @author louisliao
- *我的第一个Servlet
- */
- public class MyFirstServlet extends HttpServlet {
- private String servletInitParam="";
- /**
- *
- */
- public MyFirstServlet() {
- // TODO Auto-generated constructor stub
- }
- public void init(){
- servletInitParam=this.getInitParameter("ServletInitParam");
- }
- public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
- testDataSourceJNDI();
- testBeanSourceJNDI();
- String parame1=getServletContext().getInitParameter("MyContextParam");
- String projectName=getServletContext().getInitParameter("ProjectName");
- System.out.println("上下文初始化参数1:"+parame1);
- System.out.println("上下文初始化参数2:"+projectName);
- PrintWriter writer=response.getWriter();
- writer.println("<html><head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>");
- writer.println(projectName);
- writer.println("</title></head>");
- writer.println("<body>");
- writer.println(servletInitParam+":"+servletInitParam+"<br/>");
- writer.println("上下文初始化参数1:"+parame1+"<br/>");
- writer.println("上下文初始化参数2:"+projectName+"<br/>");
- writer.println("</body></html>");
- }
- public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
- doGet(request, response);
- }
- public void destroy() {
- super.destroy();
- System.out.println("我是destroy()方法!用来进行销毁实例的工作");
- }
- /**
- * Java-Bean资源读取测试
- */
- private void testBeanSourceJNDI()
- {
- try {
- System.err.println("testBeanSourceJNDI");
- Context context=new InitialContext();
- Context envContext=(Context)context.lookup("java:/comp/env");
- MyBean mBean=(MyBean)envContext.lookup("bean/MyBeanFactory");
- System.out.println(mBean.getHello()+":"+mBean.getProjectName());
- } catch (Exception e) {
- // TODO: handle exception
- }
- }
- /**
- * DataSource资源测试
- */
- private void testDataSourceJNDI()
- {
- try {
- System.err.println("DataSourceJNDI");
- Connection connection=null;
- ResultSet result = null;
- Statement stmt = null;
- Context context=new InitialContext();
- Context envContext=(Context)context.lookup("java:/comp/env");
- DataSource dataSource=(DataSource)envContext.lookup("jdbc/mssql");
- try {
- connection=dataSource.getConnection();
- stmt=connection.createStatement();
- result=stmt.executeQuery("select * from Author");
- while (result.next()) {
- System.out.println(result.getString("name"));
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- finally{
- result.close();
- stmt.close();
- connection.close();
- }
- } catch (Exception e) {
- // TODO: handle exception
- }
- }
- }
- /**
- *
- */
- package com.myapp.domain;
- /**
- * @author louisliao
- *
- */
- public class MyBean {
- private String projectName;
- private String hello="欢迎使用";
- public String getProjectName() {
- return projectName;
- }
- public void setProjectName(String projectName) {
- this.projectName = projectName;
- }
- public String getHello() {
- return hello;
- }
- public void setHello(String hello) {
- this.hello = hello;
- }
- }
- servlet-class:可以是<code class="java plain">org.apache.struts.action.ActionServlet或继承该类</code>
- <servlet>
- <servlet-name>action</servlet-name>
- <servlet-class>
- com.weboa.util.web.EbusinessActionServlet
- </servlet-class>
- <init-param>
- <param-name>config</param-name>
- <param-value>/WEB-INF/struts-config.xml</param-value>
- </init-param>
- <!-- module configurations -->
- <init-param>
- <param-name>debug</param-name>
- <param-value>2</param-value>
- </init-param>
- <init-param>
- <param-name>detail</param-name>
- <param-value>2</param-value>
- </init-param>
- <load-on-startup>2</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>action</servlet-name>
- <url-pattern>*.htm</url-pattern>
- </servlet-mapping>
- <!-- Struts Tag Library Descriptors -->
- <taglib>
- <taglib-uri>struts-bean</taglib-uri>
- <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
- </taglib>
- <taglib>
- <taglib-uri>struts-html</taglib-uri>
- <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
- </taglib>
- <taglib>
- <taglib-uri>struts-nested</taglib-uri>
- <taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>
- </taglib>
- <taglib>
- <taglib-uri>struts-logic</taglib-uri>
- <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
- </taglib>
- <taglib>
- <taglib-uri>struts-tiles</taglib-uri>
- <taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>
- </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配置文件位置
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- /WEB-INF/spring-dao-bean.xml,/WEB-INF/spring-resources.xml,
- /WEB-INF/spring-service-bean.xml
- </param-value>
- </context-param>
定义Spring监听器加载Spring
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
配置Struts
- <servlet>
- <servlet-name>action</servlet-name>
- <servlet-class>
- com.weboa.util.web.EbusinessActionServlet
- </servlet-class>
- <init-param>
- <param-name>config</param-name>
- <param-value>/WEB-INF/struts-config.xml</param-value>
- </init-param>
- <!-- module configurations -->
- <init-param>
- <param-name>debug</param-name>
- <param-value>2</param-value>
- </init-param>
- <init-param>
- <param-name>detail</param-name>
- <param-value>2</param-value>
- </init-param>
- <load-on-startup>2</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>action</servlet-name>
- <url-pattern>*.htm</url-pattern>
- </servlet-mapping>
定义:spring-dao-bean.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:jee="http://www.springframework.org/schema/jee"
- xmlns:util="http://www.springframework.org/schema/util"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd" >
- <bean id="transactionManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource" />
- </bean>
- <bean id="sqlMapClient"
- class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
- <property name="configLocation">
- <value>
- classpath:/com/ibatis/sql-map-config.xml
- </value>
- </property>
- <property name="dataSource" ref="dataSource" />
- </bean>
- </beans>
定义:spring-resources.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <!-- 数据库连接 -->
- <bean id="propertyConfigurer"
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="location">
- <value>/WEB-INF/classes/jdbc.properties</value>
- </property>
- </bean>
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="${jdbc.driver}"/>
- <property name="url" value="${jdbc.url}"/>
- <property name="username" value="${jdbc.user}"/>
- <property name="password" value="${jdbc.password}"/>
- <property name="maxActive" value="${jdbc.maxActive}"/>
- <property name="maxIdle" value="${jdbc.maxIdle}"/>
- <property name="maxWait" value="${jdbc.maxWait}"/>
- <property name="defaultAutoCommit" value="${jdbc.defaultAutoCommit}"/>
- <property name="removeAbandoned" value="${jdbc.removeAbandoned}"/>
- <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}"/>
- </bean>
- </beans>
定义Spring-service-bean.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <bean id="txProxyTemplate" abstract="true"
- class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
- <property name="transactionManager" ref="transactionManager" />
- <property name="transactionAttributes">
- <props>
- <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
- <prop key="remove*">PROPAGATION_REQUIRED,-Exception</prop>
- <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
- <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
- <prop key="updateFolder">PROPAGATION_REQUIRED, timeout_30</prop>
- <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
- </props>
- </property>
- </bean>
- <bean id="userCache"
- class="org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache">
- <property name="cache">
- <bean
- class="org.springframework.cache.ehcache.EhCacheFactoryBean">
- <property name="cacheManager">
- <bean
- class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" />
- </property>
- <property name="cacheName" value="userCache" />
- </bean>
- </property>
- </bean>
- <!-- *********************2009.4.24添加,得到acegi中保存的用户信息******************* -->
- <bean id="sessionRegistry" class="org.acegisecurity.concurrent.SessionRegistryImpl"/>
- <!-- *****************************个人事务************************** -->
- <!-- 日程安排 -->
- <bean id="arrangementService" parent="txProxyTemplate">
- <property name="target">
- <bean
- class="com.weboa.personalService.service.impl.ArrangementServiceImpl">
- <property name="arrangementDAO" ref="arrangementDAO" />
- </bean>
- </property>
- </bean>
- </beans>
定义jdbc.properties
- #jdbc\u6570\u636e\u5e93\u8fde\u63a5\u914d\u7f6e\u4fe1\u606f
- jdbc.driver=net.sourceforge.jtds.jdbc.Driver
- jdbc.url=jdbc\:jtds\:sqlserver\://127.0.0.1\:1433;DatabaseName\=wlynew
- jdbc.user=sa
- jdbc.password=wlyoa_)*\#\!
- jdbc.maxActive=100
- jdbc.maxIdle=30
- jdbc.maxWait=1000
- jdbc.defaultAutoCommit=false
- jdbc.removeAbandoned=true
- jdbc.removeAbandonedTimeout=60
以上是基于Spring+Struts+Ibatis的配置