不多说,直接上代码。关于注释我尽量写详细点。
1、web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 5 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 6 <!-- ********************struts2******************** --> 7 <!-- struts2的过滤器 --> 8 <filter> 9 <filter-name>struts2</filter-name> 10 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 11 <!-- 配置struts2的配置文件路径 12 <init-param> 13 <param-name>config</param-name> 14 <param-value> 15 struts.xml 16 </param-value> 17 </init-param> 18 --> 19 </filter> 20 21 <filter-mapping> 22 <filter-name>struts2</filter-name> 23 <url-pattern>/*</url-pattern> 24 </filter-mapping> 25 26 <!-- ********************spring******************** --> 27 <!-- 配置spring的监听器,用以实现依赖注射 --> 28 <listener> 29 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 30 </listener> 31 32 <!-- 配置spring的配置文件的读取路径 --> 33 <context-param> 34 <param-name>contextConfigLocation</param-name> 35 <param-value>classpath:applicationContext.xml</param-value> 36 </context-param> 37 38 <!-- 配置spring的缓存清除监听器,可选 --> 39 <listener> 40 <listener-class> 41 org.springframework.web.util.IntrospectorCleanupListener</listener-class> 42 </listener> 43 44 <!-- 配置设置spring框架编码的过滤器,可选 --> 45 <filter> 46 <filter-name>encodingFilter</filter-name> 47 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 48 <init-param> 49 <param-name>encoding</param-name> 50 <param-value>UTF-8</param-value> 51 </init-param> 52 </filter> 53 54 <!-- ********************hibernate******************** --> 55 <!-- 配置hibernate的过滤器 56 <filter> 57 <filter-name>hibernateFilter</filter-name> 58 <filter-class> 59 org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> 60 </filter> 61 --> 62 63 <!-- 配置映射路径 64 <filter-mapping> 65 <filter-name>hibernateFilter</filter-name> 66 <url-pattern>*.do</url-pattern> 67 </filter-mapping> 68 --> 69 70 <!-- ********************other******************** --> 71 <!-- 配置session超时的时间 --> 72 <session-config> 73 <session-timeout>40</session-timeout> 74 </session-config> 75 76 <!-- 配置欢迎页面 --> 77 <display-name>AGUI</display-name> 78 <welcome-file-list> 79 <welcome-file>index.jsp</welcome-file> 80 </welcome-file-list> 81 </web-app>
2、applicationContext.xml(放置spring的配置文件,包括hibernate的配置文件)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE beans PUBLIC 3 "-//SPRING//DTD BEAN//EN" 4 "http://www.springframework.org/dtd/spring-beans.dtd"> 5 <beans> 6 <import resource="classpath:database.xml" /> 7 8 <bean id="userDao" class="com.ssh.daoimpl.UserDaoImpl"> 9 <property name="sessionFactory"> 10 <ref bean="sessionFactory"/> 11 </property> 12 </bean> 13 14 <bean id="userService" class="com.ssh.serviceimpl.UserServiceImpl"> 15 <property name="userDao" ref="userDao"></property> 16 </bean> 17 18 <bean id="regAction" class="com.ssh.action.RegAction"> 19 <property name="userService" ref="userService"></property> 20 </bean> 21 22 <bean id="listUserAction" class="com.ssh.action.ListUserAction"> 23 <property name="userService" ref="userService"></property> 24 </bean> 25 26 <bean id="deleteUserAction" class="com.ssh.action.DeleteUserAction"> 27 <property name="userService" ref="userService"></property> 28 </bean> 29 </beans>
3、database.xml(放置spring的配置文件,包括hibernate的配置文件)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE beans PUBLIC 3 "-//SPRING//DTD BEAN//EN" 4 "http://www.springframework.org/dtd/spring-beans.dtd"> 5 <beans> 6 <bean id="configBean" 7 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 8 <property name="location"> 9 <value>classpath:dataSource.properties</value> 10 </property> 11 </bean> 12 13 <!--C3P0 数据源--> 14 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> 15 <!-- 指定连接数据库的驱动 --> 16 <property name="driverClass" value="${dataSource.driverClassName}"/> 17 <!-- 指定连接数据库的URL --> 18 <property name="jdbcUrl" value="${dataSource.url}"/> 19 <!-- 指定连接数据库的用户名 --> 20 <property name="user" value="${dataSource.username}"/> 21 <!-- 指定连接数据库的密码 --> 22 <property name="password" value="${dataSource.password}"/> 23 <!-- 指定连接数据库连接池的最大连接数 --> 24 <property name="maxPoolSize" value="500"/> 25 <!-- 指定连接数据库连接池的最小连接数 --> 26 <property name="minPoolSize" value="10"/> 27 <!-- 指定连接数据库连接池的初始化连接数 --> 28 <property name="initialPoolSize" value="5"/> 29 <!-- 指定连接数据库连接池的连接的最大空闲时间 --> 30 <property name="maxIdleTime" value="60"/> 31 <!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量. 如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0--> 32 <property name="maxStatements" value="0"/> 33 <!-- 在当前连接数耗尽的时候,一次获取的新的连接数 --> 34 <property name="acquireIncrement" value="3"/> 35 </bean> 36 37 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 38 <property name="dataSource" ref="dataSource" /> 39 <property name="hibernateProperties"> 40 <props> 41 <prop key="hibernate.dialect">${dataSource.dialect}</prop> 42 <prop key="hibernate.show_sql">true</prop> 43 <prop key="hibernate.hbm2ddl.auto">update</prop> 44 </props> 45 </property> 46 <property name="mappingResources"> 47 <list> 48 <value>com/ssh/bean/user.hbm.xml</value> 49 </list> 50 </property> 51 </bean> 52 53 <!-- 事务管理器(拦截器) --> 54 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 55 <property name="sessionFactory" ref="sessionFactory" /> 56 </bean> 57 58 </beans>
4、dataSource.properties(保存数据库的连接信息)
1 dataSource.driverClassName=oracle.jdbc.driver.OracleDriver 2 dataSource.url=jdbc:oracle:thin:@ZHUNIAN-THINK:1521:XE 3 dataSource.username=zhunian 4 dataSource.password=密码 5 dataSource.dialect=org.hibernate.dialect.OracleDialect
5、struts.xml(放置struts的配置文件)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" 4 "http://struts.apache.org/dtds/struts-2.1.dtd"> 5 <struts> 6 <package name="user" extends="struts-default"> 7 <action name="RegAction" class="regAction"> 8 <result name="success">/list.jsp</result> 9 <result name="input">/save.jsp</result> 10 </action> 11 <action name="ListUserAction" class="listUserAction"> 12 <result name="success">/list.jsp</result> 13 <result name="input">/index.jsp</result> 14 </action> 15 <action name="DeleteUserAction" class="deleteUserAction"> 16 <result name="success">/list.jsp</result> 17 <result name="input">/index.jsp</result> 18 </action> 19 </package> 20 </struts>