Ssh整合開發(fā)介紹和簡(jiǎn)單的登入案例實(shí)現(xiàn) 一 介紹: Ssh是strtus2-2.3.1.2+ spring-2.5.6+hibernate-3.6.8整合的開發(fā),這是目前我的整合開發(fā)的使用技術(shù)和版本,使用的數(shù)據(jù)庫為mySql。使用的開發(fā)工具是eclipse,eplipse的版本為Indigo Service Release 2 二 搭建環(huán)境 1. 首先要先引入struts2和sping和hibernate所需要的包 (1)struts2的包為: struts-2.3.1.2\lib\ struts2-core-2.3.1.2.jar struts-2.3.1.2\lib\ognl-3.0.4.jar struts-2.3.1.2\lib\ xwork-core-2.3.1.2.jar struts-2.3.1.2\lib\ freemarker-2.3.18.jar struts-2.3.1.2\lib\commons-fileupload-1.2.2.jar struts-2.3.1.2\lib\ commons-io-2.0.1.jar struts-2.3.1.2\lib\commons-lang-2.5.jar struts-2.3.1.2\lib\commons-logging-1.1.1.jar struts-2.3.1.2\lib \struts2-json-plugin-2.3.1.2.jar struts-2.3.1.2\lib \struts2-spring-plugin-2.3.1.2.jar (2)引入hibernate的包 hibernate-distribution-3.6.8.Final\lib\required\*.jar 所有的jar包 hibernate-distribution-3.6.8.Final\lib\jpa\hibernate-jpa-2.0-api-1.0.1.Final.jar hibernate-distribution-3.6.8.Final\ hibernate3.jar (3)spring的包為: spring-framework-2.5.6\dist\ spring.jar spring-framework-2.5.6\lib\c3p0\c3p0-0.9.1.2.jar spring-framework-2.5.6\lib\aspectj\*.jar spring-framework-2.5.6\lib\j2ee\common-annotations.jar spring-framework-2.5.6\lib\log4j\log4j-1.2.15.jar spring-framework-2.5.6\lib\jakarta-commons\ commons-logging.jar spring-framework-2.5.6\lib\cglib\cglib-nodep-2.1_3.jar spring-framework-2.5.6\lib\dom4j\dom4j-1.6.1.jar 2. 配置spring下所需要的文件 (1)首先配置spring所需要的xml文件 我們?cè)?span style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); background-color:inherit; font-weight:bold">class下,即在src下創(chuàng)建一個(gè)applicationContext.xml的xml文件,文件的頭文件因?yàn)橐玫礁鞣N標(biāo)簽,所以我們?cè)谝腩^文件的時(shí)候盡量引的比較多點(diǎn),代碼為: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> </beans> (2)在xml中配置和數(shù)據(jù)庫相關(guān)聯(lián),并用c3p0來配置數(shù)據(jù)庫連接池 <!-- 數(shù)據(jù)庫的連接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=UTF-8" /> <property name="user" value="root" /> <property name="password" value="1234" /> <!--初始化時(shí)獲取的連接數(shù),取值應(yīng)在minPoolSize與maxPoolSize之間。Default: 3 --> <property name="initialPoolSize" value="1" /> <!--連接池中保留的最小連接數(shù)。 --> <property name="minPoolSize" value="1" /> <!--連接池中保留的最大連接數(shù)。Default: 15 --> <property name="maxPoolSize" value="300" /> <!--最大空閑時(shí)間,60秒內(nèi)未使用則連接被丟棄。若為0則永不丟棄。Default: 0 --> <property name="maxIdleTime" value="60" /> <!--當(dāng)連接池中的連接耗盡的時(shí)候c3p0一次同時(shí)獲取的連接數(shù)。Default: 3 --> <property name="acquireIncrement" value="5" /> <!--每60秒檢查所有連接池中的空閑連接。Default: 0 --> <property name="idleConnectionTestPeriod" value="60" /> </bean> 當(dāng)然,這樣所寫的可以在一個(gè)properties中讀取。讀取外部文件在xml中的使用為: <!-- 讀取外部的文件 --> <context:property-placeholder location="jdbc.properties" /> (3)配置sessionFactory工廠,相當(dāng)于是hibernate.cfg.xml里面的配置 <!-- sessionFactory工廠 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 注入數(shù)據(jù)源 --> <property name="dataSource" ref="dataSource"></property> <!-- hibernate映射文件的引入 --> <property name="mappingResources"> <list> <value>cn/csdn/hr/s2sh/domain/Admin.hbm.xml</value> </list> </property> <!-- 配置hibernate屬性的設(shè)置 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> </bean> 3.配置struts2.xml中需要的內(nèi)容 (1)配置基本的struts2.xml <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!--使用spring創(chuàng)建管理struts2的action操作 --> <constant name="struts.objectFactory" value="spring"/> <include file="struts-admin.xml"></include> </struts> 注:其中include是引入的文件,一下的語句是非常重要的: <constant name="struts.objectFactory" value="spring"/>,它是struts2和spring的連接的關(guān)鍵 (2)引入的struts-admin.xml文件,意在檢測(cè)并作出一個(gè)簡(jiǎn)單的登入的實(shí)現(xiàn) <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="admin" extends="struts-default" namespace="/csdn"> <!-- class的名稱 等于spring中action配置文件中的id名稱 --> <action name="loginAdmin" class="adminAction" method="login"> <result name="success">../index.jsp</result> </action> </package> </struts> 4. Hibernate.cfg.xml中的內(nèi)容可以省略,它已經(jīng)在spring中的xml中配置了 5.搭建層之間的關(guān)系 (1)首先是domain,包為cn.csdn.hr.s2sh.domain,我們以admin為例 屬性為id和name和pass 封裝的實(shí)體類為: package cn.csdn.hr.s2sh.domain; import java.io.Serializable; public class Admin implements Serializable { private static final long serialVersionUID = 1L; private Integer id; private String name; private String pass; public Admin() { super(); // TODO Auto-generated constructor stub } public Admin(Integer id, String name, String pass) { super(); this.id = id; this.name = name; this.pass = pass; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } @Override public String toString() { return "Admin [id=" + id + ", name=" + name + ", pass=" + pass + "]"; } } (2)在同一個(gè)包下的實(shí)體類的映射文件 <?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 package="cn.csdn.hr.s2sh.domain"> <class name="Admin" table="admin"> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="name" column="name"></property> <property name="pass" column="pass"></property> </class> </hibernate-mapping> (3)創(chuàng)建dao層,創(chuàng)建的包名為cn.csdn.hr.s2sh.dao,創(chuàng)建的接口名為AdminDao,類名為AdminDaoImpl 下面是創(chuàng)建的接口 AdminDao.java package cn.csdn.hr.s2sh.dao; import java.util.List; import cn.csdn.hr.s2sh.domain.Admin; public interface AdminDao { public Admin login(final String name,final String pass); } 創(chuàng)建的類為AdminDaoImpl.java package cn.csdn.hr.s2sh.dao; import java.sql.SQLException; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Session; import org.springframework.orm.hibernate3.HibernateCallback; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import cn.csdn.hr.s2sh.domain.Admin; @SuppressWarnings("unchecked") public class AdminDaoImpl extends HibernateDaoSupport implements AdminDao { public Admin login(final String name, final String pass) { return (Admin) this.getHibernateTemplate().execute( new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { // TODO Auto-generated method stub Object obj = session .createQuery( "from Admin where name=:name and pass=:pass") .setString("name", name) .setString("pass", pass).uniqueResult(); return obj; } }); } } (4)創(chuàng)建service層,創(chuàng)建的包名為cn.csdn.hr.s2sh.service 創(chuàng)建的AdminService.java接口 package cn.csdn.hr.s2sh.service; import cn.csdn.hr.s2sh.dao.AdminDao; public interface AdminService extends AdminDao{ } 創(chuàng)建的AdminServiceImpl.java package cn.csdn.hr.s2sh.service; import java.util.List; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.TransactionCallback; import org.springframework.transaction.support.TransactionTemplate; import cn.csdn.hr.s2sh.dao.AdminDao; import cn.csdn.hr.s2sh.domain.Admin; public class AdminServiceImpl implements AdminService { private AdminDao adminDao; public void setAdminDao(AdminDao adminDao) { this.adminDao = adminDao; } public Admin login(String name, String pass) { // TODO Auto-generated method stub return adminDao.login(name, pass); } } 6.創(chuàng)建訪問struts2的時(shí)候的action,我們把a(bǔ)ction放到一個(gè)bean-action.xml中 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 創(chuàng)建struts2中的action --> <!-- 配置admin的action --> <bean id="adminAction" class="cn.csdn.hr.s2sh.action.AdminAction" scope="prototype"> <property name="adminService" ref="adminService"></property> </bean> </beans> 而ref的adminServiceImpl是在bean-service.xml中 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- admin的業(yè)務(wù)操作 --> <bean id="adminServiceImpl" class="cn.csdn.hr.s2sh.service.AdminServiceImpl"> <property name="adminDao" ref="adminDao"></property> </bean> </beans> 上面的ref是adminDaoImpl,在beans-dao.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- hibernatedao的模板類 HibernateDaoSuppor 抽象類不可以實(shí)例化 加上abstract="true" --> <bean id="hibernateDaoSupport" class="org.springframework.orm.hibernate3.support.HibernateDaoSupport" abstract="true"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- admin的dao對(duì)象 --> <bean id="adminDao" class="cn.csdn.hr.s2sh.dao.AdminDaoImpl" parent="hibernateDaoSupport" /> </beans> 然后我們把上面的三個(gè)beans.xml導(dǎo)入到applicationContext.xml中 <!-- 導(dǎo)入其他的配置文件 --> <import resource="beans-dao.xml" /> <import resource="beans-service.xml" /> <import resource="beans-action.xml" /> 7.配置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>s2sh</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 指定spring的配置文件,默認(rèn)從web根目錄尋找配置文件,我們可以通過spring提供的classpath:前綴指定從類路徑下尋找 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applic*.xml</param-value> </context-param> <!-- 對(duì)Spring容器進(jìn)行實(shí)例化 --> <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> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置 使用spring解決hibernate因session關(guān)閉導(dǎo)致的延遲加載例外問題 --> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> <init-param> <!-- 指定org.springframework.orm.hibernate3.LocalSessionFactoryBean在spring配置文件中的名稱,默認(rèn)值為sessionFactory.如果LocalSessionFactoryBean在spring中的名稱不是sessionFactory,該參數(shù)一定要指定,否則會(huì)出現(xiàn)找不到sessionFactory的例外 --> <param-name>sessionFactoryBeanName</param-name> <param-value>sessionFactory</param-value> </init-param> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 使用spring解決struts2的中文亂碼的問題 --> <filter> <filter-name>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-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> 8.我們來做一個(gè)簡(jiǎn)單的登入,使用的jsp頁面為: <body> <div> <form action="/s2sh/csdn/loginAdmin.action" method="get"> 用戶名:<input type="text" name="admin.name"/><br/> 密 碼:<input type="password" name="admin.pass"/><br/> <input type="submit" value="登入"/> <input type="reset" value="重置"/> </form> </div> <div> 用戶名為:${admin.name} </div> </body> 訪問到的action的處理為: package cn.csdn.hr.s2sh.action; import cn.csdn.hr.s2sh.domain.Admin; import cn.csdn.hr.s2sh.service.AdminService; import com.opensymphony.xwork2.ActionSupport; public class AdminAction extends ActionSupport { private static final long serialVersionUID = 1L; private AdminService adminService; private Admin admin; public Admin getAdmin() { return admin; } public void setAdmin(Admin admin) { this.admin = admin; } // 注入 public void setAdminService(AdminService adminService) { this.adminService = adminService; } public String login() { System.out.println("用戶名:" + admin.getName()); admin = adminService.login(admin.getName(), admin.getPass()); return SUCCESS; } }
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。