struts2整合spring:Struts2整合Spring Hibernate的CRUD的例子

Struts 2 + Spring 2.0 + Hibernate 3.0整合操作可以参照我这篇文章
http://bbs.bccn.net/thread-239025-1-1.html

本文提供Struts2整合Spring HibernateCRUD例子源代码

1. 数据库脚本数据库采用MySQL 5.0

CREATE TABLE `user` (
`id` (11) NOT NULL auto_increment,
`username` varchar(32) NOT NULL,
`password` varchar(32) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


2. 几个重要文件

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="[url=http://java.sun.com/xml/ns/javaee]http://java.sun.com/xml/ns/javaee[/url]"
xmlns:xsi="[url=http://www.w3.org/2001/XMLSchema-instance]http://www.w3.org/2001/XMLSchema-instance[/url]"
xsi:schemaLocation="[url=http://java.sun.com/xml/ns/javaee]http://java.sun.com/xml/ns/javaee[/url]
[url=http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd]http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd[/url]">
<!-- spring应用上下文 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<!-- struts2 过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter->
org.apache.struts2.dispatcher.FilterDispatcher
</filter->
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring监听器以便在启动时就自动加载spring配置 -->
<listener>
<listener->
org.springframework.web.context.ContextLoaderListener
</listener->
</listener>
<!-- OpenSessionInViewFilter过滤器 -->
<filter>
<filter-name>lazyLoadingFilter</filter-name>
<filter->
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter->
</filter>
<filter-mapping>
<filter-name>lazyLoadingFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>/user/userAdd.jsp</welcome-file>
</welcome-file-list>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="[url=http://java.sun.com/xml/ns/javaee]http://java.sun.com/xml/ns/javaee[/url]"
xmlns:xsi="[url=http://www.w3.org/2001/XMLSchema-instance]http://www.w3.org/2001/XMLSchema-instance[/url]"
xsi:schemaLocation="[url=http://java.sun.com/xml/ns/javaee]http://java.sun.com/xml/ns/javaee[/url]
[url=http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd]http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd[/url]">
<!-- spring应用上下文 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<!-- struts2 过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter->
org.apache.struts2.dispatcher.FilterDispatcher
</filter->
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring监听器以便在启动时就自动加载spring配置 -->
<listener>
<listener->
org.springframework.web.context.ContextLoaderListener
</listener->
</listener>
<!-- OpenSessionInViewFilter过滤器 -->
<filter>
<filter-name>lazyLoadingFilter</filter-name>
<filter->
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter->
</filter>
<filter-mapping>
<filter-name>lazyLoadingFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>/user/userAdd.jsp</welcome-file>
</welcome-file-list>
</web-app>

applicationContext.xml


Java代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="[url=http://www.springframework.org/schema/beans]http://www.springframework.org/schema/beans[/url]"
xmlns:xsi="[url=http://www.w3.org/2001/XMLSchema-instance]http://www.w3.org/2001/XMLSchema-instance[/url]"
xsi:schemaLocation="[url=http://www.springframework.org/schema/beans]http://www.springframework.org/schema/beans[/url] [url=http://www.springframework.org/schema/beans/spring-beans-2.0.xsd]http://www.springframework.org/schema/beans/spring-beans-2.0.xsd[/url]">

<bean id="dataSource"
="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/catalog">
</property>
<property name="username" value="root"></property>
<property name="password" value="ethip"></property>
</bean>
<bean id="sessionFactory"
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>org/ethip/catalog/model/User.hbm.xml</value>
</list>
</property>
</bean>
<bean id="UserDAO" ="org.ethip.catalog.dao.UserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="UserService"
="org.ethip.catalog.service.UserService">
<property name="dao">
<ref bean="UserDAO" />
</property>
</bean>
<bean name="addUserBean" ="org.ethip.catalog.web.UserAction"
scope="prototype">
<property name="service">
<ref bean="UserService" />
</property>
</bean>
<bean name="listUserBean" ="org.ethip.catalog.web.UserAction"
scope="prototype">
<property name="service">
<ref bean="UserService" />
</property>
</bean>
<bean name="deleteUserBean" ="org.ethip.catalog.web.UserAction"
scope="prototype">
<property name="service">
<ref bean="UserService" />
</property>
</bean>
<bean name="editUserBean" ="org.ethip.catalog.web.UserAction"
scope="prototype">
<property name="service">
<ref bean="UserService" />
</property>
</bean>
<bean name="updateUserBean" ="org.ethip.catalog.web.UserAction"
scope="prototype">
<property name="service">
<ref bean="UserService" />
</property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="[url=http://www.springframework.org/schema/beans]http://www.springframework.org/schema/beans[/url]"
xmlns:xsi="[url=http://www.w3.org/2001/XMLSchema-instance]http://www.w3.org/2001/XMLSchema-instance[/url]"
xsi:schemaLocation="[url=http://www.springframework.org/schema/beans]http://www.springframework.org/schema/beans[/url] [url=http://www.springframework.org/schema/beans/spring-beans-2.0.xsd]http://www.springframework.org/schema/beans/spring-beans-2.0.xsd[/url]">

<bean id="dataSource"
="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/catalog">
</property>
<property name="username" value="root"></property>
<property name="password" value="ethip"></property>
</bean>
<bean id="sessionFactory"
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>org/ethip/catalog/model/User.hbm.xml</value>
</list>
</property>
</bean>
<bean id="UserDAO" ="org.ethip.catalog.dao.UserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="UserService"
="org.ethip.catalog.service.UserService">
<property name="dao">
<ref bean="UserDAO" />
</property>
</bean>
<bean name="addUserBean" ="org.ethip.catalog.web.UserAction"
scope="prototype">
<property name="service">
<ref bean="UserService" />
</property>
</bean>
<bean name="listUserBean" ="org.ethip.catalog.web.UserAction"
scope="prototype">
<property name="service">
<ref bean="UserService" />
</property>
</bean>
<bean name="deleteUserBean" ="org.ethip.catalog.web.UserAction"
scope="prototype">
<property name="service">
<ref bean="UserService" />
</property>
</bean>
<bean name="editUserBean" ="org.ethip.catalog.web.UserAction"
scope="prototype">
<property name="service">
<ref bean="UserService" />
</property>
</bean>
<bean name="updateUserBean" ="org.ethip.catalog.web.UserAction"
scope="prototype">
<property name="service">
<ref bean="UserService" />
</property>
</bean>
</beans>

struts.xml


Java代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "[url=http://struts.apache.org/dtds/struts-2.0.dtd]http://struts.apache.org/dtds/struts-2.0.dtd[/url]">
<struts>
< file="struts-default.xml" />
<!-- 此constant设置临时文件存放目录默认default.properties中没有指定 -->
<constant name="struts.multipart.saveDir" value="c:\"></constant>
<!-- 将action托管给spring -->
<constant name="struts.objectFactory" value="spring"></constant>
<package name="SSH2" extends="struts-default">
<action name="userAdd" ="addUserBean" method="userAdd">
<result name="success" type="redirect">userList.action</result>
</action>
<action name="userList" ="listUserBean" method="userList">
<result name="success">/user/userList.jsp</result>
</action>
<action name="userDelete" ="deleteUserBean"
method="userDelete">
<result name="success" type="redirect">userList.action</result>
</action>
<action name="userEdit" ="editUserBean"
method="userEdit">
<result name="success">/user/userEdit.jsp</result>
</action>
<action name="userUpdate" ="updateUserBean"
method="userUpdate">
<result name="success" type="redirect">userList.action</result>
</action>
</package>
</struts>
3. 本例子仅实现CRUD功能没有实现其他处理如数据校验、国际化、分页等.

4. 由于附件过大lib下所有jar都删掉了请各位学习者按照上操作导入
Tags:  struts2spring2.5 struts2spring struts2hibernate struts2整合spring

延伸阅读

最新评论

发表评论