Struts2.3.4+Hibernate3.6+Spring3.0.5框架整合(新手看看)
2012-09-28 14:21
387 查看
工欲善其事,必先利其器,相关的下载就不多说,数据库是MySql5.1
新建一个WEB工程,将如下的包导入工程的WEB-INF/lib目录下,
导入包的说明:struts2.3.4中因为使用c3p0连接数据库所以导入了c3p0.jar,其它的就是struts2.3.4的基本的包的导入,上图中的slf4j-nop-1.6.1.jar导不导入影响不大,然后导入Hibernate的核心包,以及Spring中dist目录下的所有包,去掉重复的包,保留较高的版本,结果如上图所示,如果你想使用struts2,Hibernate以及Spring的更多的功能,请导入相关的包。
下面是web.xml文件的配置
web.xml
接着是spring的配置文件applicationContext.xml
Hibernate的映射文件User.hbm.xml
strusts.xml文件
用户注册Action:RegisterAction
用户Bean:User
数据库Dao 接口:UserDao
Dao的实现类:UserDaoimpl
相关的jsp页面;
index.jsp:用户注册
显示结果的页面:success.jsp
最后说明:包的导入情况没有细说,如果出现异常,可以根据异常信息自己百度神马的酌情加入或者删去一些包,此工程的配置文件除web.xml外,其它均放在WEB-INF/config目录下,此工程只是简单的测试一下框架的整合情况,所以只是前台一个jsp填写信息,然后写入数据库这样简单的逻辑。
最后希望路过的各位大神,给予宝贵的学习SSH框架的意见及建议。。。
新建一个WEB工程,将如下的包导入工程的WEB-INF/lib目录下,
导入包的说明:struts2.3.4中因为使用c3p0连接数据库所以导入了c3p0.jar,其它的就是struts2.3.4的基本的包的导入,上图中的slf4j-nop-1.6.1.jar导不导入影响不大,然后导入Hibernate的核心包,以及Spring中dist目录下的所有包,去掉重复的包,保留较高的版本,结果如上图所示,如果你想使用struts2,Hibernate以及Spring的更多的功能,请导入相关的包。
下面是web.xml文件的配置
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>SSHFramework</display-name> <!-- 加载Spring的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/applicationContext.xml</param-value> </context-param> <!-- 加载spring的监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- struts2的配置 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>config</param-name> <param-value>struts-default.xml,struts-plugin.xml,../config/struts.xml</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
接着是spring的配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- 定义数据源 ,使用c3p0数据源实现--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property> <property name="user" value="root"></property> <property name="password" value="123456"></property> </bean> <!-- Hibernate sessionFactory创建 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- Hibernate属性 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">create</prop> </props> </property> <!-- 映射文件 --> <property name="mappingResources"> <list> <!-- 统一到WEB-INF/config下管理配置文件 --> <value>../config/User.hbm.xml</value> </list> </property> </bean> <bean id="userDao" class="cn.zj.qiao.spring.dao.userimpl.UserDaoImpl" scope="prototype"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="registerAction" class="cn.zj.qiao.actions.RegisterAction" scope="prototype"> <property name="ud" ref="userDao"></property> </bean> </beans>
Hibernate的映射文件User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="cn.zj.qiao.entity.vo.User" table="user_table"> <id name="id" column="uid"> <generator class="identity"></generator> </id> <property name="uname"></property> <property name="upass"></property> </class> </hibernate-mapping>
strusts.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="default" namespace="/" extends="struts-default"> <action name="register" class="registerAction"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
用户注册Action:RegisterAction
package cn.zj.qiao.actions; import cn.zj.qiao.entity.vo.User; import cn.zj.qiao.interfaces.UserDao; import com.opensymphony.xwork2.Action; public class RegisterAction implements Action{ private String uname; private String upass; private UserDao ud; public UserDao getUd(){ return ud; } public void setUd(UserDao ud){ this.ud = ud; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpass() { return upass; } public void setUpass(String upass) { this.upass = upass; } @Override public String execute() throws Exception { User user = new User(); user.setUname(uname); user.setUpass(upass); ud.addUser(user); return SUCCESS; } }
用户Bean:User
package cn.zj.qiao.entity.vo; public class User { private Long id; private String uname; private String upass; public User() { super(); } public User(Long id, String uname, String upass) { super(); this.id = id; this.uname = uname; this.upass = upass; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpass() { return upass; } public void setUpass(String upass) { this.upass = upass; } }
数据库Dao 接口:UserDao
package cn.zj.qiao.interfaces; import cn.zj.qiao.entity.vo.User; public interface UserDao { public Long addUser(User user); }
Dao的实现类:UserDaoimpl
package cn.zj.qiao.spring.dao.userimpl; import org.hibernate.SessionFactory; import org.springframework.orm.hibernate3.HibernateTemplate; import cn.zj.qiao.entity.vo.User; import cn.zj.qiao.interfaces.UserDao; public class UserDaoImpl implements UserDao { private HibernateTemplate ht = null; private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory){ this.sessionFactory = sessionFactory; } public SessionFactory getSessionFactory(){ return sessionFactory; } public HibernateTemplate getHibernateTemplate(){ if(ht == null){ ht = new HibernateTemplate(sessionFactory); } return ht; } @Override public Long addUser(User user) { return (Long)getHibernateTemplate().save(user); } }
相关的jsp页面;
index.jsp:用户注册
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>用户注册</title> </head> <body> <form action="register.action" method="post"> 用户名:<input type="text" name="uname"/> 密 码:<input type="password" name="upass"/> <input type="submit" value="注册"/> </form> </body> </html>
显示结果的页面:success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>success</title> </head> <body> 注册成功 </body> </html>
最后说明:包的导入情况没有细说,如果出现异常,可以根据异常信息自己百度神马的酌情加入或者删去一些包,此工程的配置文件除web.xml外,其它均放在WEB-INF/config目录下,此工程只是简单的测试一下框架的整合情况,所以只是前台一个jsp填写信息,然后写入数据库这样简单的逻辑。
最后希望路过的各位大神,给予宝贵的学习SSH框架的意见及建议。。。
相关文章推荐
- Struts2.3.4+Hibernate3.6+Spring3.0.5框架整合(新手看看)
- 框架整合之--------spring2.5+struts2.1+hibernate3.6
- java S2SH项目框架Struts2.3+Spring3.1+Hibernate3.6框架搭建整合以及实例demo代码分享
- Struts2.3.4 、Hibernate3.6 和Spring3.1.2整合所需jar包列表
- Struts2.3.4 、Hibernate3.6 和Spring3.1.2整合所需jar包列表
- ssh整合配置(struts2.2.1+ hibernate3.6+spring3.0.5)
- ssh最新整合配置(struts2.2.1+ hibernate3.6+spring3.0.5)
- SSH(Spring+Struts+Hibernate)框架学习之ajax技术和struts的整合
- 浅谈ssh(struts,spring,hibernate三大框架)整合的意义及其精髓
- Spring+Struts+Hibernate框架整合配置
- struts2.0、hibernate3、spring2.5框架整合流程实例
- Activiti5工作流框架学习之Struts2+Hibernate+Spring整合配置
- 12月1日struts、spring、hibernate等框架的整合培训日记
- 浅谈ssh(struts,spring,hibernate三大框架)整合的意义及其精髓
- 12月1日struts、spring、hibernate等框架的整合培训日记
- struts2.2.1+spring 3.0.3+hibernate3.6+dwr3.0全注解整合详解
- Hibernate3.3.2+Spring2.5.5+Struts2.1.6+Extjs3.0.0 Annotations注解框架整合及其代码分享
- 12月1日struts、spring、hibernate等框架的整合培训日记
- 浅谈ssh(struts,spring,hibernate三大框架)整合的意义及其精髓
- java Spring-3.2.0+Struts-2.3.4+Hibernate-4.1.6整合