Struts2+Hibernate3.0+Spring的基本搭建(非常基础的)
具体操作步骤截图见附件(源代码不包含lib包):S2SH截图
除了操作的配置代码:
WEB-INF下的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 <!-- 配置Struts 2框架的核心Filter -->
7 <filter>
8 <filter-name>struts</filter-name>
9 <filter-class>
10 org.apache.struts2.dispatcher.FilterDispatcher
11 </filter-class>
12 </filter>
13
14 <!-- 配置Filter拦截的URL -->
15 <filter-mapping>
16 <filter-name>struts</filter-name>
17 <url-pattern>/*</url-pattern>
18 </filter-mapping>
19 <!-- 配置Spring -->
20 <context-param>
21 <param-name>contextConfigLocation</param-name>
22 <param-value>classpath:applicationContext.xml</param-value>
23 </context-param>
24
25 <listener>
26 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
27 </listener>
28
29 <welcome-file-list>
30 <welcome-file>index.jsp</welcome-file>
31 </welcome-file-list>
32 </web-app>
33
applicationContext.xml:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans
3 xmlns="http://www.springframework.org/schema/beans"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
6 <!-- Spring 风格的Hibernate的配置 -->
7 <!-- 使用外部属性文件/WEB-INF/jdbc.properties -->
8 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
9 <property name="location" value="/WEB-INF/jdbc.properties"></property>
10 </bean>
11 <!-- 配置数据源 -->
12 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
13 destroy-method="close" >
14 <property name="driverClassName" value="${jdbc.driverClassName}"></property>
15 <property name="url" value="${jdbc.url}"></property>
16 <property name="username" value="${jdbc.username}"></property>
17 <property name="password" value="${jdbc.password}"></property>
18 </bean>
19
20
21 <!-- 创建SessionFactory实例 -->
22 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
23 <property name="dataSource" ref="dataSource"></property><!-- 数据源 -->
24
25 <property name="mappingResources">
26 <list>
27
28 </list>
29
30 </property>
31
32
33 <!-- 配置数据库方言 -->
34 <property name="hibernateProperties">
35 <props>
36 <prop key="hibernate.dialect">
37 ${hibernate.dialect}
38 </prop>
39 <prop key="hibernate.show_sql">true</prop>
40 <prop key="hibernate.generate_satistices">true</prop>
41 </props>
42 </property>
43 </bean>
44
45
46 <!-- 使用Hibernate3事务管理配置 -->
47 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
48 <property name="sessionFactory" ref="sessionFactory"></property>
49 </bean>
50
51
52 <!-- 配置HibernateTemplate -->
53 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
54 <property name="sessionFactory" ref="sessionFactory"></property>
55 </bean>
56
57
58 </beans>
struts.xml:
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE struts PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4 "http://struts.apache.org/dtds/struts-2.0.dtd">
5 <struts>
6 <!-- 包含外部struts的配置 -->
7 <!-- <include file="filename"></include> -->
8
9 <!-- 配置UTF-8 -->
10 <constant name="struts.i18n.encoding" value="utf-8"></constant>
11 <package name="struts2" extends="struts-default">
12
13 <!-- 写Action的配置 -->
14
15
16 </package>
17
18 </struts>
hibernate.cfg.xml:什么都做,全交给Spring管理
1 <?xml version='1.0' encoding='UTF-8'?>
2 <!DOCTYPE hibernate-configuration PUBLIC
3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
5
6 <!-- Generated by MyEclipse Hibernate Tools. -->
7 <hibernate-configuration>
8
9 <session-factory>
10
11 </session-factory>
12
13 </hibernate-configuration>
WEB-INF下的数据库配置文件 jdbc.properties
1 hibernate.dialect= org.hibernate.dialect.MySQLDialect
2 jdbc.driverClassName=com.mysql.jdbc.Driver
3 jdbc.url=jdbc\:mysql\://localhost\:3306/bookbooks
4 jdbc.username=root
5 jdbc.password=123456
6