一、准备struts2+spring+hibernate所须要的jar包:
二、搭建struts2环境
Struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter Struts2 /*
package com.zhouxiang.action;import com.opensymphony.xwork2.ActionSupport;import com.zhouxiang.service.LoginService;public class LoginAction extends ActionSupport { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String Login() { return "Login"; } public String LoginTest() { if (username!=null&&password!=null)) { return "Success"; } else { return "faliure"; } }}
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> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <!-- <default-action-ref name="index" /> --> <action name="Login" class="LoginAction" method="Login"> <result name="Login"> /Login.jsp </result> </action> <action name="LoginAction" class="LoginAction" method="LoginTest"> <result name="Success"> /LoginSuccess.jsp </result> <result name="faliure"> /LoginSuccess.jsp </result> </action> </package> <!-- Add packages here --> </struts>
三、搭建spring环境并整合struts2
org.springframework.web.context.ContextLoaderListener
四、搭建hibernate环境并与spring及struts2整合
com/zhouxiang/bean/User.hbm.xml hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.format_sql=false
五、登录demo
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>Insert title here
package com.zhouxiang.action;import com.opensymphony.xwork2.ActionSupport;import com.zhouxiang.service.LoginService;public class LoginAction extends ActionSupport { private String username; private String password; private LoginService loginservice; public LoginService getLoginservice() { return loginservice; } public void setLoginservice(LoginService loginservice) { this.loginservice = loginservice; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String Login() { System.out.println("hello"); return "Login"; } public String LoginTest() { if (loginservice.UserInfoValidate(username, password)) { return "Success"; } else { return "faliure"; } }}
package com.zhouxiang.service;import com.zhouxiang.DaoImp.UserDaoImp;public class LoginService { private UserDaoImp userdao; public UserDaoImp getUserdao() { return userdao; } public void setUserdao(UserDaoImp userdao) { this.userdao = userdao; } public Boolean UserInfoValidate(String username, String password) { if (userdao.Query(username)!=null) return true; else return false; }}
package com.zhouxiang.BaseDao;public interface BaseDao{ public Boolean Add(T t); public Boolean Delete(String id); public T Query(String id); public Boolean Modify(T t);}
package com.zhouxiang.BaseDaoImp;import java.lang.reflect.ParameterizedType;import org.hibernate.Session;import org.hibernate.SessionFactory;import com.zhouxiang.BaseDao.BaseDao;public abstract class BaseDaoImpimplements BaseDao { private Class clazz; private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public BaseDaoImp() { // 使用反射技术得到T的真实类型 ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass(); // 获取当前new的对象的 泛型的父类 类型 this.clazz = (Class ) pt.getActualTypeArguments()[0]; // 获取第一个类型參数的真实类型 } protected Session getSession() { return sessionFactory.openSession(); } @Override public Boolean Add(T t) { // TODO Auto-generated method stub return null; } @Override public Boolean Delete(String id) { // TODO Auto-generated method stub return null; } @Override public T Query(String id) { // TODO Auto-generated method stub if(id==null) { return null; } else{ System.out.println("sdfsd"); return (T) getSession().get(clazz, id); } } @Override public Boolean Modify(T t) { // TODO Auto-generated method stub return null; }}
package com.zhouxiang.Dao;import com.zhouxiang.BaseDao.BaseDao;import com.zhouxiang.bean.User;public interface UserDao extends BaseDao{}
package com.zhouxiang.DaoImp;import com.zhouxiang.BaseDaoImp.BaseDaoImp;import com.zhouxiang.Dao.UserDao;import com.zhouxiang.bean.User;public class UserDaoImp extends BaseDaoImpimplements UserDao {}
Struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter Struts2 /* org.springframework.web.context.ContextLoaderListener index.jsp
/Login.jsp /LoginSuccess.jsp /LoginSuccess.jsp
> <!-- - Application context definition for JPetStore's business layer. - Contains bean references to the transaction manager and to the DAOs in - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation"). --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- ========================= GENERAL DEFINITIONS ========================= --> <!-- Configurer that replaces ${...} placeholders with values from properties files --> <!-- (in this case, mail and JDBC related properties) --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql://localhost:3306/TestProject1"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"> </property> <property name="mappingResources"> <list> <value>com/zhouxiang/bean/User.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.format_sql=false </value> </property> </bean> <bean id="basedaoimp" class="com.zhouxiang.BaseDaoImp.BaseDaoImp" abstract="true"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="userdao" class="com.zhouxiang.DaoImp.UserDaoImp" parent="basedaoimp"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="loginService" class="com.zhouxiang.service.LoginService"> <property name="userdao" ref="userdao"></property> </bean> <bean id="LoginAction" class="com.zhouxiang.action.LoginAction"> <property name="loginservice" ref="loginService"></property> </bean> </beans>
六、总结
SSH的环境搭建没有什么难度。须要的仅仅是一些jar包和一些配置文件。struts+spring+hibernate整合的过程中须要注意的是引入的一些jar包及为什么使用这些jar包,这些jar包的作用是什么。当然不可缺少的另一些配置文件,如web项目初始化须要做的,怎样将spring容器增加到项目中。一集怎样将struts跟hibernate、及项目中用到的实体bean、业务逻辑bean交给spring容器管理。整个环境搭建的过程最重要的是去理解三大框架是怎样独立工作的,又是怎样衔接工作的。