SSH---集成Struts2+Spring+Hibernate(一)
本方案让Spring创建Struts2的Action,不让Spring完全管理Struts2的Action,Struts2Action中的依赖对象,默认会根据名称自动装配
1、创建web项目
2、引入Struts2的依赖包,将依赖包拷贝到WEB-INF/lib下
*commons-logging-1.0.4.jar *freemarker-2.3.15.jar *ognl-2.7.3.jar * struts2-core-2.1.8.1.jar *xwork-core-2.1.6.jar *commons-fileupload-1.2.1.jar
3、引入Spring的依赖包,将依赖包拷贝到WEB-INF/lib下
*spring.jar *lib/aspectj/*.jar
4、引入hibernate相关依赖包,将依赖包拷贝到WEB-INF/lib下
*hibernate3.jar *lib/*.jar
5、数据库驱动
*MySQl JDBC Driver
6、将Struts2和Spring集成的依赖包拷贝到WEB-INF/lib下
*struts2-spring-plugin-2.1.8.1.jar
7、在web.xml文件中配置StrutsPrepareAndExecuteFilter
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
8、提供struts.xml配置文件,提供必要属性的配置
*struts.i18n.encoding=GB18030 * struts.configuration.xml.reload=true *struts.devMode=true
9、提供Spring的配置文件
*applicationContext-service.xml *applicationContext-dao.xml *applicationContext-common.xml
10、提供hibernate.cfg.xml配置文件,提供log4j
11、在web.xml文件中配置Spring的ContextLoaderListener,创建BeanFactory
<context-param> <param-name>contextConfigLocation</param-name> <!-- <param-value>classpath:applicationContext-*.xml</param-value> --> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
12、在web.xml文件中配置OpenSessionInViewFilter(需要放到Struts2的Filter前面)
<filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
13、提供用户添加表单和add_success.jsp
14、建立User实体类,编写hibernate映射文件,将映射文件加入到hibernate.cfg.xml中
<mapping resource="/com/tgb/usermgr/domain/User.hbm.xml"/>
15、建立UserDao和UserService,并进行实现
UserDao
//UserDao接口 package com.tgb.usermgr.dao; import com.tgb.usermgr.domain.User; public interfaceUserDao { public void add(User user); } //UserDao实现 packagecom.tgb.usermgr.dao.impl; importorg.springframework.orm.hibernate3.support.HibernateDaoSupport; importcom.tgb.usermgr.dao.UserDao; importcom.tgb.usermgr.domain.User; publicclass UserDaoImpl extends HibernateDaoSupport implements UserDao { public void add(User user) { getHibernateTemplate().save(user); } }
UserService
//UserService接口 package com.tgb.usermgr.service; import com.tgb.usermgr.domain.User; public interfaceUserService { public void add(User user); } //UserService实现 packagecom.tgb.usermgr.service.impl; importcom.tgb.usermgr.dao.UserDao; importcom.tgb.usermgr.domain.User; importcom.tgb.usermgr.service.UserService; publicclass UserServiceImpl implements UserService { private UserDao userDao; public void add(User user) { userDao.add(user); } public void setUserDao(UserDao userDao) { this.userDao = userDao; } }
16、建立Struts2的Action,并配置到Struts2的配置文件中
<struts> <constant name="struts.i18n.encoding" value="GB18030"/> <constant name="struts.configuration.xml.reload" value="true"/> <constant name="struts.devMode" value="true"/> <!-- 默认根据名称自动装配Action中的依赖对象,现在修改为根据类型 --> <constant name="struts.objectFactory.spring.autoWire"value="type"/> <package name="user-package" extends="struts-default" namespace="/user"> <action name="add" class="com.tgb.usermgr.web.action.AddUserAction"> <result>/add_success.jsp</result> </action> </package> </struts>
17、在jsp中调用Action
<body> <form action="user/add.action"> 用户代码:<input type="text" name="user.userCode"><br> 用户姓名:<input type="text" name="user.userName"><br> 年龄:<input type="text" name="user.age"><br> <input type="submit"value="添加"> </form> </body>
最后附上框架的下载链接:http://download.csdn.net/detail/senior_lee/9059887