1、web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 用於初始化Spring容器的Listener --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 載入spring設定檔 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext-*.xml</param-value> </context-param> <!-- encodingFilter是為了處理國際化,交由Spring處理,設定為UTF-8 --> <filter> <filter-name>encodingFilter</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> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- struts2 是struts的filter,這個定義就將可以將請求交給struts過濾 --> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>struts-cleanup</filter-name> <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class> </filter> <filter-mapping> <filter-name>struts-cleanup</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 定義預設返回頁,如輸入http://127.0.0.1/那麼根目錄下的index.html或者其他檔案就被請求 --> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- session逾時定義,單位為分鐘 --> <session-config> <session-timeout>30</session-timeout> </session-config></web-app>
2、struts.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.i18n.encoding" value="UTF-8" /> <constant name="struts.ui.theme" value="simple" /> <package name="struts2" extends="struts-default"> <!-- 在這裡添加Action定義 --> <!-- ImageCode --> <action name="randomImageCode" class="randomImageCode"> <result type="stream"> <param name="contentType">image/jpeg</param> <param name="inputName">inputStream</param> </result> </action> </package></struts>
3、hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory> <!-- JDBC驅動程式 --> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </property> <!-- 串連資料庫的URL --> <property name="connection.url"> jdbc:oracle:thin:@192.168.100.69:1521:orcl </property> <property name="connection.useUnicode">true</property> <property name="connection.characterEncoding">UTF-8</property> <!-- 串連的登入名稱 --> <property name="connection.username">frscs</property> <!-- 登入密碼 --> <property name="connection.password">frscs</property> <!-- C3P0串連池設定 --> <property name="hibernate.connection.provider_class"> org.hibernate.connection.C3P0ConnectionProvider </property> <property name="hibernate.c3p0.max_size">20</property> <property name="hibernate.c3p0.min_size">5</property> <!-- 擷取串連的等待時間 --> <property name="hibernate.c3p0.timeout">120</property> <!-- 可以被緩衝的PreparedStatement最大數目。設定適量可大大提高Hibernate效能 --> <property name="hibernate.c3p0.max_statements">200</property> <!-- 每隔120s測試連接是否可以正常使用 --> <property name="hibernate.c3p0.idle_test_period">120</property> <!-- 串連池滿了以後,增加的數量 --> <property name="hibernate.c3p0.acquire_increment">2</property> <!-- 動態更新 --> <property name="hibernate.dynamic-update">true</property> <!-- 是否將運行期產生的SQL輸出到日誌以供調試 --> <property name="show_sql">true</property> <!-- 指定串連的語言 --> <property name="dialect">org.hibernate.dialect.OracleDialect</property> <!-- 映射資源 --> <mapping resource="com/frscs/pojo/Person.hbm.xml" /> <mapping resource="com/frscs/pojo/Encodetype.hbm.xml" /> </session-factory></hibernate-configuration>
4、Person.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.bean"> <class name="Person" table="person"> <cache usage="read-write" region="cn.xwuxin.bean.Person"/> <id name="id"> <generator class="native"/> </id> <property name="name" length="10" not-null="true"/> </class> </hibernate-mapping>
5、applicationContext.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" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <!-- 配置交易管理員Bean,因為使用Hibernate持久化技術,故使用HibernateTranscationManager 交易管理員 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <!--為交易管理員注入SessionFactory 引用 --> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 配置事務攔截器Bean --> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <!-- 事務攔截器bean需要依賴注入一個交易管理員 --> <property name="transactionManager" ref="transactionManager" /> <property name="transactionAttributes"> <!-- 下面定義事務傳播屬性--> <props> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!-- 定義BeanNameAutoProxyCreator 後處理器 ,BeanNameAutoProxyCreator為名字匹配字串或者萬用字元的bean自動建立AOP代理。--> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <!-- 指定對滿足哪些bean name的bean自動產生業務代理 --> <property name="beanNames"> <!-- 下面是所有需要自動建立事務代理的bean--> <list> <value>*Service</value> </list> <!-- 此處可增加其他需要自動建立事務代理的bean--> </property> <!-- 下面定義BeanNameAutoProxyCreator所需的事務攔截器--> <property name="interceptorNames"> <list> <!-- 此處可增加其他新的Interceptor --> <value>transactionInterceptor</value> </list> </property> </bean> </beans>