struts1.2.9+spring2.0.6+hibernate3.0.5 整合开发的一个例子
一个小例子,看看struts1.2.9+spring2.0.6+hibernate3.0.5 是如何整合在一起进行javaweb开发的
一个简单的OA小系统,下面是预览图和项目结构
这个SSH整合的例子,用到了许多经典的整合技巧,能帮助初学者清晰的理解整合的步骤和技巧,我们也可以使用这个框架为模版,来搭建自己SSH的环境。
首先我列出关键的整合步骤
1.建立WEB项目的目录结构
a.strtus的 ActionServlet 配置在web.xml 中
b.将spring的监听器 配置在web.xml
c.字符乱码过滤器 配置在web.xml
- <?xml version= "1.0" encoding= "UTF-8" ?>
- <web-app xmlns= "http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" version= "2.4"
- xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/applicationContext.xml</param-value>
- </context-param>
- <listener>
- <listener- class >
- org.springframework.web.context.ContextLoaderListener
- </listener- class >
- </listener>
- <filter>
- <filter-name>Set Character Encoding</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>
- <filter-name>sitemesh</filter-name>
- <filter- class >
- com.opensymphony.module.sitemesh.filter.PageFilter
- </filter- class >
- </filter>
- <filter-mapping>
- <filter-name>sitemesh</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>Set Character Encoding</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <servlet>
- <servlet-name>action</servlet-name>
- <servlet- class >
- org.apache.struts.action.ActionServlet
- </servlet- class >
- <init-param>
- <param-name>config</param-name>
- <param-value>/WEB-INF/struts-config.xml</param-value>
- </init-param>
- <init-param>
- <param-name>debug</param-name>
- <param-value> 3 </param-value>
- </init-param>
- <init-param>
- <param-name>detail</param-name>
- <param-value> 3 </param-value>
- </init-param>
- <load-on-startup> 0 </load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>action</servlet-name>
- <url-pattern>*. do </url-pattern>
- </servlet-mapping>
- <session-config>
- <session-timeout> 120 </session-timeout>
- </session-config>
- <!-- The Usual Welcome File List -->
- <welcome-file-list>
- <welcome-file>index. do </welcome-file>
- </welcome-file-list>
- <jsp-config>
- <taglib>
- <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
- <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
- </taglib>
- <taglib>
- <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
- <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
- </taglib>
- <taglib>
- <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
- <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
- </taglib>
- <taglib>
- <taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
- <taglib-location>
- /WEB-INF/struts-nested.tld
- </taglib-location>
- </taglib>
- <taglib>
- <taglib-uri>/WEB-INF/struts-template.tld</taglib-uri>
- <taglib-location>
- /WEB-INF/struts-template.tld
- </taglib-location>
- </taglib>
- <taglib>
- <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
- <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
- </taglib>
- <taglib>
- <taglib-uri>sitemesh-page</taglib-uri>
- <taglib-location>
- /WEB-INF/sitemesh-page.tld
- </taglib-location>
- </taglib>
- <taglib>
- <taglib-uri>sitemesh-decorator</taglib-uri>
- <taglib-location>
- /WEB-INF/sitemesh-decorator.tld
- </taglib-location>
- </taglib>
- </jsp-config>
- <welcome-file-list>
- <welcome-file>login.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>Set Character Encoding</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>
<filter-name>sitemesh</filter-name>
<filter-class>
com.opensymphony.module.sitemesh.filter.PageFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>120</session-timeout>
</session-config>
<!-- The Usual Welcome File List -->
<welcome-file-list>
<welcome-file>index.do</welcome-file>
</welcome-file-list>
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
<taglib-location>
/WEB-INF/struts-nested.tld
</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-template.tld</taglib-uri>
<taglib-location>
/WEB-INF/struts-template.tld
</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>sitemesh-page</taglib-uri>
<taglib-location>
/WEB-INF/sitemesh-page.tld
</taglib-location>
</taglib>
<taglib>
<taglib-uri>sitemesh-decorator</taglib-uri>
<taglib-location>
/WEB-INF/sitemesh-decorator.tld
</taglib-location>
</taglib>
</jsp-config>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
2.编写spring的配置文件 applicationContext.xml
a. 配置数据源
b.让spring来管理SessionFactory
c.配置事务
d.配置Hibernate 属性以及映射文件
e.定义DAO和DAO代理
f.根据需要配置AOP
g.定义struts的配置
- <?xml version= "1.0" encoding= "UTF-8" ?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
- "http://www.springframework.org/dtd/spring-beans-2.0.dtd" >
- <beans>
- <!-- 配置数据源 -->
- <bean id= "dataSource"
- class = "org.apache.commons.dbcp.BasicDataSource"
- destroy-method= "close" >
- <property name= "driverClassName" >
- <value>com.mysql.jdbc.Driver</value>
- </property>
- <property name= "url" >
- <value>jdbc:mysql: //localhost:3306/demo?useUnicode=true&characterEncoding=utf-8</value>
- </property>
- <property name= "username" >
- <value>root</value>
- </property>
- <property name= "password" >
- <value> 12345678 </value>
- </property>
- </bean>
- <!-- 配置Hibernate -->
- <bean id= "sessionFactory"
- class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
- <property name= "dataSource" >
- <ref local= "dataSource" />
- </property>
- <property name= "mappingResources" >
- <list>
- <value>com/demo/hibernate/beans/User.hbm.xml</value>
- <value>com/demo/hibernate/beans/Address.hbm.xml</value>
- <value>com/demo/hibernate/beans/Schedule.hbm.xml</value>
- <value>com/demo/hibernate/beans/Worklog.hbm.xml</value>
- <value>com/demo/hibernate/beans/Sms.hbm.xml</value>
- <value>com/demo/hibernate/beans/Notice.hbm.xml</value>
- <value>com/demo/hibernate/beans/Meeting.hbm.xml</value>
- </list>
- </property>
- <property name= "hibernateProperties" >
- <props>
- <prop key= "hibernate.dialect" >
- org.hibernate.dialect.MySQLDialect
- </prop>
- <prop key= "hibernate.show_sql" > true </prop>
- <prop key= "hibernate.hbm2ddl.auto" >update</prop>
- </props>
- </property>
- </bean>
- <!-- 配置事务 -->
- <bean id= "transactionManager"
- class = "org.springframework.orm.hibernate3.HibernateTransactionManager" >
- <property name= "sessionFactory" >
- <ref local= "sessionFactory" />
- </property>
- </bean>
- <!-- 定义DAO -->
- <bean id= "userDAO" class = "com.demo.hibernate.dao.UserDAO" >
- <property name= "sessionFactory" >
- <ref local= "sessionFactory" />
- </property>
- </bean>
- <bean id= "addressDAO" class = "com.demo.hibernate.dao.AddressDAO" >
- <property name= "sessionFactory" >
- <ref local= "sessionFactory" />
- </property>
- </bean>
- <bean id= "scheduleDAO" class = "com.demo.hibernate.dao.ScheduleDAO" >
- <property name= "sessionFactory" >
- <ref local= "sessionFactory" />
- </property>
- </bean>
- <bean id= "worklogDAO" class = "com.demo.hibernate.dao.WorklogDAO" >
- <property name= "sessionFactory" >
- <ref local= "sessionFactory" />
- </property>
- </bean>
- <bean id= "smsDAO" class = "com.demo.hibernate.dao.SmsDAO" >
- <property name= "sessionFactory" >
- <ref local= "sessionFactory" />
- </property>
- </bean>
- <bean id= "noticeDAO" class = "com.demo.hibernate.dao.NoticeDAO" >
- <property name= "sessionFactory" >
- <ref local= "sessionFactory" />
- </property>
- </bean>
- <bean id= "meetingDAO" class = "com.demo.hibernate.dao.MeetingDAO" >
- <property name= "sessionFactory" >
- <ref local= "sessionFactory" />
- </property>
- </bean>
- <!-- 定义DAO代理 -->
- <bean id= "UserDAOProxy"
- class = "org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
- <property name= "transactionManager" >
- <ref bean= "transactionManager" />
- </property>
- <property name= "target" >
- <ref local= "userDAO" />
- </property>
- <property name= "transactionAttributes" >
- <props>
- <prop key= "insert*" >PROPAGATION_REQUIRED</prop>
- <prop key= "*" >PROPAGATION_REQUIRED,readOnly</prop>
- </props>
- </property>
- </bean>
- <bean id= "addressDAOProxy"
- class = "org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
- <property name= "transactionManager" >
- <ref bean= "transactionManager" />
- </property>
- <property name= "target" >
- <ref local= "addressDAO" />
- </property>
- <property name= "transactionAttributes" >
- <props>
- <prop key= "insert*" >PROPAGATION_REQUIRED</prop>
- <prop key= "update*" >PROPAGATION_REQUIRED</prop>
- <prop key= "delete*" >PROPAGATION_REQUIRED</prop>
- <prop key= "*" >PROPAGATION_REQUIRED,readOnly</prop>
- </props>
- </property>
- </bean>
- <bean id= "scheduleDAOProxy"
- class = "org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
- <property name= "transactionManager" >
- <ref bean= "transactionManager" />
- </property>
- <property name= "target" >
- <ref local= "scheduleDAO" />
- </property>
- <property name= "transactionAttributes" >
- <props>
- <prop key= "insert*" >PROPAGATION_REQUIRED</prop>
- <prop key= "update*" >PROPAGATION_REQUIRED</prop>
- <prop key= "delete*" >PROPAGATION_REQUIRED</prop>
- <prop key= "*" >PROPAGATION_REQUIRED,readOnly</prop>
- </props>
- </property>
- </bean>
- <bean id= "worklogDAOProxy"
- class = "org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
- <property name= "transactionManager" >
- <ref bean= "transactionManager" />
- </property>
- <property name= "target" >
- <ref local= "worklogDAO" />
- </property>
- <property name= "transactionAttributes" >
- <props>
- <prop key= "insert*" >PROPAGATION_REQUIRED</prop>
- <prop key= "update*" >PROPAGATION_REQUIRED</prop>
- <prop key= "delete*" >PROPAGATION_REQUIRED</prop>
- <prop key= "*" >PROPAGATION_REQUIRED,readOnly</prop>
- </props>
- </property>
- </bean>
- <bean id= "smsDAOProxy"
- class = "org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
- <property name= "transactionManager" >
- <ref bean= "transactionManager" />
- </property>
- <property name= "target" >
- <ref local= "smsDAO" />
- </property>
- <property name= "transactionAttributes" >
- <props>
- <prop key= "insert*" >PROPAGATION_REQUIRED</prop>
- <prop key= "update*" >PROPAGATION_REQUIRED</prop>
- <prop key= "delete*" >PROPAGATION_REQUIRED</prop>
- <prop key= "*" >PROPAGATION_REQUIRED,readOnly</prop>
- </props>
- </property>
- </bean>
- <bean id= "noticeDAOProxy"
- class = "org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
- <property name= "transactionManager" >
- <ref bean= "transactionManager" />
- </property>
- <property name= "target" >
- <ref local= "noticeDAO" />
- </property>
- <property name= "transactionAttributes" >
- <props>
- <prop key= "insert*" >PROPAGATION_REQUIRED</prop>
- <prop key= "update*" >PROPAGATION_REQUIRED</prop>
- <prop key= "delete*" >PROPAGATION_REQUIRED</prop>
- <prop key= "*" >PROPAGATION_REQUIRED,readOnly</prop>
- </props>
- </property>
- </bean>
- <bean id= "meetingDAOProxy"
- class = "org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
- <property name= "transactionManager" >
- <ref bean= "transactionManager" />
- </property>
- <property name= "target" >
- <ref local= "meetingDAO" />
- </property>
- <property name= "transactionAttributes" >
- <props>
- <prop key= "insert*" >PROPAGATION_REQUIRED</prop>
- <prop key= "update*" >PROPAGATION_REQUIRED</prop>
- <prop key= "delete*" >PROPAGATION_REQUIRED</prop>
- <prop key= "*" >PROPAGATION_REQUIRED,readOnly</prop>
- </props>
- </property>
- </bean>
- <!-- 定义Struts配置 -->
- <bean name= "/login" class = "com.demo.struts.actions.LoginAction" >
- <property name= "userDAO" >
- <ref local= "userDAO" />
- </property>
- </bean>
- <bean name= "/logout" class = "com.demo.struts.actions.LogoutAction" >
- <property name= "userDAO" >
- <ref local= "userDAO" />
- </property>
- </bean>
- <bean name= "/register"
- class = "com.demo.struts.actions.RegisterAction" >
- <property name= "userDAO" >
- <ref local= "userDAO" />
- </property>
- </bean>
- <!-- 定义Struts配置:address -->
- <bean name= "/address"
- class = "com.demo.struts.actions.AddressAction" >
- <property name= "addressDAO" >
- <ref local= "addressDAO" />
- </property>
- </bean>
- <bean name= "/address_add"
- class = "com.demo.struts.actions.AddressAction" >
- <property name= "addressDAO" >
- <ref local= "addressDAO" />
- </property>
- </bean>
- <bean name= "/address_edit"
- class = "com.demo.struts.actions.AddressAction" >
- <property name= "addressDAO" >
- <ref local= "addressDAO" />
- </property>
- </bean>
- <!-- 定义Struts配置:schedule -->
- <bean name= "/schedule"
- class = "com.demo.struts.actions.ScheduleAction" >
- <property name= "scheduleDAO" >
- <ref local= "scheduleDAO" />
- </property>
- </bean>
- <bean name= "/schedule_add"
- class = "com.demo.struts.actions.ScheduleAction" >
- <property name= "scheduleDAO" >
- <ref local= "scheduleDAO" />
- </property>
- </bean>
- <bean name= "/schedule_edit"
- class = "com.demo.struts.actions.ScheduleAction" >
- <property name= "scheduleDAO" >
- <ref local= "scheduleDAO" />
- </property>
- </bean>
- <!-- 定义Struts配置:worklog -->
- <bean name= "/worklog"
- class = "com.demo.struts.actions.WorklogAction" >
- <property name= "worklogDAO" >
- <ref local= "worklogDAO" />
- </property>
- </bean>
- <bean name= "/worklog_add"
- class = "com.demo.struts.actions.WorklogAction" >
- <property name= "worklogDAO" >
- <ref local= "worklogDAO" />
- </property>
- </bean>
- <bean name= "/worklog_edit"
- class = "com.demo.struts.actions.WorklogAction" >
- <property name= "worklogDAO" >
- <ref local= "worklogDAO" />
- </property>
- </bean>
- <!-- 定义Struts配置:sms -->
- <bean name= "/sms" class = "com.demo.struts.actions.SmsAction" >
- <property name= "smsDAO" >
- <ref local= "smsDAO" />
- </property>
- </bean>
- <bean name= "/sms_add" class = "com.demo.struts.actions.SmsAction" >
- <property name= "smsDAO" >
- <ref local= "smsDAO" />
- </property>
- </bean>
- <!-- 定义Struts配置:notice -->
- <bean name= "/notice" class = "com.demo.struts.actions.NoticeAction" >
- <property name= "noticeDAO" >
- <ref local= "noticeDAO" />
- </property>
- </bean>
- <bean name= "/notice_add"
- class = "com.demo.struts.actions.NoticeAction" >
- <property name= "noticeDAO" >
- <ref local= "noticeDAO" />
- </property>
- </bean>
- <bean name= "/notice_edit"
- class = "com.demo.struts.actions.NoticeAction" >
- <property name= "noticeDAO" >
- <ref local= "noticeDAO" />
- </property>
- </bean>
- <!-- 定义Struts配置:meeting -->
- <bean name= "/meeting"
- class = "com.demo.struts.actions.MeetingAction" >