前言
SSH不是一個框架,而是多個框架(struts+spring+hibernate)的集成,是目前較流行的一種Web應用程序開源集成框架,用于構建靈活、易于擴展的多層Web應用程序。
集成SSH框架的系統從職責上分為四層:表示層、業務邏輯層、數據持久層和域模塊層(實體層)。
Struts作為系統的整體基礎架構,負責MVC的分離,在Struts框架的模型部分,控制業務跳轉,利用Hibernate框架對持久層提供支持。Spring一方面作為一個輕量級的IoC容器,負責查找、定位、創建和管理對象及對象之間的依賴關系,另一方面能使Struts和Hibernate更好地工作。
使用MyEclipse整合SSH三大框架,并實現一個模擬用戶注冊的Demo,對應版本:
Struts版本:2.1;
Spring版本:3.1;
Hibernate版本:3.3;
一、整合前準備工作
1.建立一個Web項目,如下:
注意:支持action的包名必須是“action”,且action類必須是以Action結尾,即形如XxxAction這種形式,如上圖中所示
2.創建數據庫以及表:
1
2
3
4
5
6
|
CREATE table t_user( id INT PRIMARY KEY , username VARCHAR (10), password VARCHAR (20) ) |
3.導入數據庫連接池c3p0jar包,點擊可下載:
c3p0-0.9.2-pre1.jar、mysql-connector-java-5.1.13-bin.jar
二、Struts框架的配置:
1.選中項目,右鍵選擇:MyEclipse -> Project Facets[Capabilities] -> Install Apache Struts (2.x) Facet,如下:
2.選擇版本,在這里我選擇的是2.1,點擊"Finish",如下:
3.完成上述步驟以后,會發現在src目錄下多出一個struts.xml 文件,內容如下:
1
2
3
4
5
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> < struts > </ struts > |
4.在WEB-INF目錄下的web.xml文件中多一段關于struts過濾器的配置代碼,如下:
5.參考上圖,將*.action修改為"/*",至此struts框架配置完畢;
三、Spring框架的配置:
1.參考struts的配置,選中項目,右鍵選擇:MyEclipse -> Project Facets[Capabilities] -> Install Spring Facet,選擇版本,在此選擇3.1如下:
2.點擊"Finish",會發現src目錄下多了一個applicationContext.xml文件,WEB-INF目錄下多了一個spring-form.tld與spring.tld文件,并且在web.xml文件中多了一段與spring配置有關的代碼,spring框架搭建基本完畢(引入命名空間會在后面講到),如下所示:
四、Hibernate框架的配置:
1.參考struts的配置,選中項目,右鍵選擇:MyEclipse -> Project Facets[Capabilities] -> Install HibernateFacet,選擇版本,在此選擇3.3如下:
2.點擊"Finish",會發現src目錄下多了一個缺省包(可以刪除),并且在web.xml文件中多了一段代碼(后面會重新配置),如下所示:
3.支持“@Entity”注解的jar包導入:選中項目,右鍵選擇:MyEclipse -> Project Facets[Capabilities] ->Manage...,然后照下圖中的步驟操作:
完成上述步驟,三大框架基本就搭建起來了,接下來整合它們。
五、整合
1.為了不讓applicationContext.xml看起來太臃腫,以及便于管理,我們將Hibernate有關的配置保存在另外一個.xml文件中,然后再在applicationContext.xml導入,其具體步驟:
(1)在src目錄下(與applicationContext.xml同級)創建一個名為hibernateContext.xml的文件,復制applicationContext.xml里面的內容,然后再做修改;
(2)hibernateContext.xml文件里面的內容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:p = "http://www.springframework.org/schema/p" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd" > <!-- sessionFactory 配置 --> < bean id = "sessionFactory" class = "org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" > <!-- dataSource的屬性會在applicationContext.xml文件中配置,在這里先引用 --> < property name = "dataSource" ref = "dataSource" ></ property > <!-- 設置hibernate相關的配置項 --> < property name = "hibernateProperties" > <!-- props標簽是為了注入Properties這個類型的屬性 --> <!-- key必須加上hibernate.前綴 --> < props > < prop key = "hibernate.dialect" >org.hibernate.dialect.MySQLDialect</ prop > <!-- show_sql目的是打印sql語句 --> < prop key = "hibernate.show_sql" >true</ prop > <!-- 美化SQL的打印格式 --> < prop key = "hibernate.format_sql" >true</ prop > <!-- a) create-drop:在執行程序的時候創建數據表,在執行完了之后刪除表,實際開發中,常用于測試 b) create:在每次執行程序的時候重新創建數據表 c) update:在執行程序的時候會判斷,如果存在,不創建表,否則創建數據表,并且會根據實體類中的屬性的增加,而自動增加數據表中的字段(開發環境) d) validate:在執行程序的時候會判斷,如果實體類中的屬性與表中的字段不一致,那么就報錯(生產環境) --> < prop key = "hibernate.hbm2ddl.auto" >validate</ prop > </ props > </ property > <!-- 配置hibernate的實體類 --> < property name = "packagesToScan" > <!--list標簽是用來注入String[]類型的屬性 ,其值一般是對應的bean包的全限名,而bean包中的類一般又是與數據庫中的表對應--> < list > < value >com.beauxie.bean</ value > </ list > </ property > </ bean > <!-- 配置 hibernateTemplate模板 --> < bean id = "hibernateTemplate" class = "org.springframework.orm.hibernate3.HibernateTemplate" > < property name = "sessionFactory" ref = "sessionFactory" ></ property > </ bean > </ beans > |
(3)在applicationContext.xm刪除“sessionFactory”的配置(因為在hibernateContext.xml中已經配置好了),然后導入已經修改好的hibernateContext.xml內容,導入完以后,此時applicationContext.xml內容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:p = "http://www.springframework.org/schema/p" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd" > < bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" > </ bean > <!-- 導入其他的spring配置文件 ,如果都放在一個文件里,會看起來比較臃腫--> < import resource = "hibernateContext.xml" /> </ beans > |
2.在applicationContext.xm文件中原先dataSource的基礎上,修改其配置(數據庫名、用戶名、密碼等),(注意:value標簽中一定不能含有空格、回車!!),如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
< bean id = "dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource" > < property name = "jdbcUrl" > <!--如果直接用value屬性,而不用value標簽,則需要將“&”轉義(&) ,用value標簽,<span style="color:#FF0000;">標簽中一定不能含有空格、回車,因為它會將空格轉換成" "</span>,導致數據庫會連接不上,除非重寫數據源 --> < value > <![CDATA[jdbc:mysql://localhost:3306/sshdemo?useUnicode=true&characterEncoding=UTF8&useServerPrepStmts=true&prepStmtCacheSqlLimit=256&cachePrepStmts=true&prepStmtCacheSize=256&rewriteBatchedStatements=true]]> </ value > </ property > < property name = "driverClass" value = "com.mysql.jdbc.Driver" ></ property > < property name = "user" value = "root" ></ property > < property name = "password" value = "root" ></ property > < property name = "acquireIncrement" value = "3" ></ property > < property name = "initialPoolSize" value = "10" ></ property > < property name = "minPoolSize" value = "2" ></ property > < property name = "maxPoolSize" value = "10" ></ property > </ bean > |
3.在applicationContext.xm中,配置spring的掃描器,這樣給我們的類加上spring組件注解,就可以實現bean的自動載入,具體步驟如下:(1)引入context命名空間,支持context標簽,點擊底部的"Namespaces",然后勾選context那一項即可:
(2)配置spring掃描器:
<!-- 配置spring的掃描器,然后給我們的類加上spring組件注解,就可以實現bean的自動載入-->
<context:component-scan base-package="com.beauxie.action,com.beauxie.service,com.beauxie.dao">
</context:component-scan>
至此ssh三大框架環境搭建完畢,接下來是在ssh框架基礎上實現用戶注冊
六、案例:簡單的模仿用戶注冊
1.前臺注冊頁面代碼,index.jsp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> < html > < head > < base href="<%=basePath%>"> < title >歡迎注冊</ title > < meta http-equiv = "pragma" content = "no-cache" > < meta http-equiv = "cache-control" content = "no-cache" > < meta http-equiv = "expires" content = "0" > < meta http-equiv = "keywords" content = "keyword1,keyword2,keyword3" > < meta http-equiv = "description" content = "This is my page" > <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </ head > < body > < form action = "${pageContext.request.contextPath }/user/regist" method = "POST" > <!-- 也可以使用user.username自動裝入user屬性,但在這里不是重點,所以就在后臺手動獲取其值--> 用戶名:< input type = "text" name = "username" >< br > 密 碼:< input type = "password" name = "password" >< br > < input type = "submit" value = "注冊" > </ form > </ body > </ html > |
2.User類代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
package com.beauxie.bean; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; /** * @author Beauxie * 在這里User的屬性應當與t_user表中的字段相同, * 否則就需要手動為不相同的屬性指定對應表中的字段 */ @Entity //映射數據庫表 @Table (name= "t_user" ) //不加這個注解,默認對應的是user表 public class User { @Id //對應t_user表中的主鍵 private int id; //用戶ID private String username; //用戶名 private String password; //密碼 public int getId() { return id; } public void setId( int id) { this .id = id; } 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; } } |
3.UserDao類代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package com.beauxie.dao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.stereotype.Repository; import com.beauxie.bean.User; /** * @author Beauxie * Dao層,對數據庫進行操作 */ @Repository //這個屬性對應的是持久層(一般為Dao層),說明交給spring管理,而對應的包下的類名也會有一個"S" public class UserDao { @Autowired //自動注入,不需要設值,因為在spring配置文件中已經配置過 private HibernateTemplate template; /** * 用戶注冊,即向表中添加一條新的記錄 * @param user */ public void addUser(User user){ //往數據庫中添加一條數據,一句話就可以搞定 template.save(user); } } |
4.UserService類代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package com.beauxie.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.beauxie.bean.User; import com.beauxie.dao.UserDao; /** * @author Beauxie * Service層 */ @Service //這個屬性對應的是業務層一般為Service層),說明交給spring管理,而對應的包下的類名也會有一個"S" public class UserService { @Autowired //同樣是自動注入 private UserDao userDao; public void addUser(User user){ //調用Dao層的addUser方法 userDao.addUser(user); } } |
5.UserAction類代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
package com.beauxie.action; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.beauxie.bean.User; import com.beauxie.service.UserService; /** * @author Beauxie * */ @Controller //用于標注控制層組件 @Namespace ( "/user" ) //url前綴 @Scope ( "prototype" ) //Action默認是單例,但實際開發中,一般是多例,因為一般一個Action可能會對應多個不同的請求 //@ParentPackage("struts-default")//繼承特定的package,默認是“struts-default”,因此可以省略不寫 @Results ({ @Result (name= "registSuccess" ,location= "/msg.jsp" ) }) public class UserAction { @Autowired //自動注入 private UserService service ; //struts默認攔截“.action以及不加任何后綴” @Action (value= "regist" ) //訪問:/user/regist.action 或 /user/regist public String regist(){ //獲取request HttpServletRequest request = ServletActionContext.getRequest(); //獲取表單提交的數據 String username = request.getParameter( "username" ); String password = request.getParameter( "password" ); //封裝userBean User user = new User(); user.setId( 1000 ); user.setUsername(username); user.setPassword(password); //調用service層的方法,向數據庫中增加一條記錄 service.addUser(user); //將提示信息存入request域中,用以前臺顯示 request.setAttribute( "msg" , "恭喜您,注冊成功!<br>注冊名:" +username); return "registSuccess" ; } } |
6.消息提示界面:msg.jsp代碼,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> < html > < head > < base href="<%=basePath%>"> < title >消息提示</ title > < meta http-equiv = "pragma" content = "no-cache" > < meta http-equiv = "cache-control" content = "no-cache" > < meta http-equiv = "expires" content = "0" > < meta http-equiv = "keywords" content = "keyword1,keyword2,keyword3" > < meta http-equiv = "description" content = "This is my page" > <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </ head > < body > ${msg } </ body > </ html > |
7.將項目添加到服務器中,啟動服務,打開瀏覽器,訪問:http://localhost/SSHDemo/user/regist
8.輸入用戶名與密碼,點擊“注冊”,顯示結果:
9.控制臺輸出sql語句(在hibernateContext.xml文件中已經配置過輸出并美化SQL語句):
10.查看數據庫結果:
到此這個簡單的案例就已經結束了,關于表單提交數據校驗、以及亂碼問題并未涉及,后續應該會更新吧、、、
七、總結:
1.三大框架的整合,應該先引入每個框架以后,再整合;
2.一定要記得導入數據庫jar包;
3.Action類應該要放在包名為"action"的包下,并且類名應當要以Action結尾,形如“XxxAction”;
4.在配置Hibernate時,一定要導入支持“@Entity”注解的jar包;
5.可以再struts.xml文件中定義struts攔截的請求類型,默認為.action與不加后綴
6.可以再web.xml文件中定義struts過濾器的過濾類型,默認為*.action,應當改為/*;
7.在applicationContext.xm文件中需要配置:sessionFactory、hibernate的實體類、hibernateTemplate模板 、數據源dataSource、spring掃描器五部分(包含hibernateContext.xml);
8.各個類中一定要加上對應的注解,以及Action中的方法上也要加注解。
實例源碼下載:SSHzhuce.rar
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。