前言
最近將Spring,Struts,Hiberbate基礎(chǔ)已經(jīng)學(xué)習(xí)完成。想自己把這三個(gè)框架集成一下,然后再寫(xiě)一個(gè)后臺(tái)管理網(wǎng)站練練手。Spring的作用是依賴(lài)注入,而Struts是顯示層的東西,這兩個(gè)框架集成后是什么樣子。一邊學(xué)習(xí),一邊記錄。上車(chē)。
Spring集成所需jar包
首先,Spring集成Struts,那么applicationContext.xml和struts.xml,web.xml肯定是不能少的。前面兩個(gè)是Spring和Struts的配置文件,后面一個(gè)是整個(gè)web的全局配置文件。在每個(gè)配置文件中應(yīng)該怎么配置,怎么相互關(guān)聯(lián)呢。其實(shí)就是將Struts中指定的Action 類(lèi)為Spring注入的類(lèi)。
三大框架集成開(kāi)發(fā)并不難,難的地方在于各個(gè)包的依賴(lài)要搞清楚,版本之間的差異也是一點(diǎn)。下面列出Spring集成Struts所依賴(lài)的包:
依賴(lài)包
此處所有依賴(lài)為Struts2.0和Spring3.0。版本有點(diǎn)老,我用最新版的始終集成不正確。等搞好了再升級(jí)版本。
Number | Package | Platform | Function |
---|---|---|---|
1 | commons-fileupload-1.2.2.jar | common | 文件上傳功能 |
2 | commons-io-2.0.1.jar | common | |
3 | commons-lang-2.5.jar | common | |
4 | commons-logging-1.1.1.jar | common | 日志 |
5 | freemarker-2.3.16.jar | Struts | 模版引擎 |
6 | javassist-3.11.0.GA.jar | common | 動(dòng)態(tài)編程 |
7 | ognl-3.0.1.jar | common | 表達(dá)式語(yǔ)言,提供屬性,方法調(diào)用 |
8 | org.springframework.asm-3.1.1.RELEASE.jar | spring | Spring獨(dú)立的asm程序,Spring2.5.6的時(shí)候需要asmJar 包3.0.6開(kāi)始提供他自己獨(dú)立的asmJar。暫時(shí)我自己也不懂這事干嘛的。 |
9 | org.springframework.beans-3.1.1.RELEASE.jar | spring | Spring IOC實(shí)現(xiàn) |
10 | org.springframework.context-3.1.1.RELEASE.jar | spring | Spring提供在基礎(chǔ)IoC功能上的擴(kuò)展服務(wù),此外還提供許多企業(yè)級(jí)服務(wù)的支持,如郵件服務(wù)、任務(wù)調(diào)度、JNDI定位、EJB集成、遠(yuǎn)程訪問(wèn)、緩存以及各種視圖層框架的封裝等 |
org.springframework.context.support-3.1.1.RELEASE.jar | spring | Spring-context的擴(kuò)展支持,用于MVC方面 | |
12 | org.springframework.core-3.1.1.RELEASE.jar | spring | Spring 核心工具包 |
13 | org.springframework.expression-3.1.1.RELEASE.jar | spring | Spring表達(dá)式語(yǔ)言 |
14 | org.springframework.web-3.1.1.RELEASE.jar | spring | Spring Web工具包 |
15 | org.springframework.web.servlet-3.1.1.RELEASE.jar | spring | 基于servlet的MVC實(shí)現(xiàn) |
16 | struts2-core-2.2.3.1.jar | struts | Struts核心庫(kù) |
17 | xwork-core-2.2.3.1.jar | struts | xwork核心庫(kù) |
18 | struts2-spring-plugin-2.2.3.1.jar | struts | Spring與Struts相互集成 |
19 | antlr-2.7.2.jar | common | 語(yǔ)言語(yǔ)法分析器 |
20 | aopalliance-1.0.jar | common | 面向切面編程接口 |
21 | commons-dbcp.jar | common | DBCP數(shù)據(jù)庫(kù)連接池 |
22 | commons-pool.jar | common | DBCP數(shù)據(jù)庫(kù)連接池 |
23 | dom4j-1.6.1.jar | hibernate | 靈活的xml框架 |
24 | hibernate-jpa-2.0-api-1.0.1.Final.jar | hibernate | 注解使用類(lèi) |
25 | hibernate3.jar | hibernate | 數(shù)據(jù)庫(kù)核心包 |
26 | jta-1.1.jar | hibernate | 分布式事務(wù)處理 |
27 | mysql-connector-java-5.1.18-bin.jar | hibernate | jdbc連接器 |
28 | org.springframework.jdbc-3.1.1.RELEASE.jar | hibernate | spring與jdbc集成 |
29 | org.springframework.orm-3.1.1.RELEASE.jar | hibernate | 數(shù)據(jù)庫(kù)集成 |
30 | org.springframework.transaction-3.1.1.RELEASE.jar | hibernate | 事務(wù)集成 |
31 | slf4j-api-1.6.1.jar | common | 日志系統(tǒng) |
集成
model層
新建Usermodel,如下:
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
|
package com.action; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity @javax .persistence.Table(name= "user" ) public class User implements Serializable{ private static final long serialVersionUID = 1L; @Id @GeneratedValue @Column (name= "id" ) public int id; @Column (name= "name" ) public String name; @Column (name= "password" ) public String password; public int getId() { return id; } public void setId( int id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public String getPassword() { return password; } public void setPassword(String password) { this .password = password; } @Override public String toString() { return "User [name=" + name + ", password=" + password + "]" ; } } |
dao層
新建dao接口:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.dao.impl; import java.util.List; import com.action.User; import com.action.UserAction; public interface UserDao { public void save(User action); public User getUser( int id); public void update(User action); public void delete(User userAction); public List<User> findByName(String name); } |
實(shí)現(xiàn)dao接口
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
|
package com.dao.impl; import java.util.List; import org.hibernate.SessionFactory; import org.springframework.orm.hibernate3.HibernateTemplate; import com.action.User; import com.action.UserAction; public class UserDaoImpl implements UserDao { private SessionFactory sessionFactory; private HibernateTemplate mHibernateTemplate; public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this .sessionFactory = sessionFactory; } public HibernateTemplate getHbernateTemplate() { if (mHibernateTemplate== null ) { mHibernateTemplate = new HibernateTemplate(sessionFactory); } return mHibernateTemplate; } public void save(User action) { // TODO Auto-generated method stub getHbernateTemplate().save(action); } public User getUser( int id) { // TODO Auto-generated method stub User userAction = getHbernateTemplate().get(User. class , new Integer(id)); return userAction; } public void update(User action) { // TODO Auto-generated method stub getHbernateTemplate().update(action); } public void delete(User userAction) { // TODO Auto-generated method stub getHbernateTemplate().delete(userAction); } @SuppressWarnings ( "unchecked" ) public List<User> findByName(String name) { // TODO Auto-generated method stub String queryString = "from User u where u.name like ?" ; return getHbernateTemplate().find(queryString); } } |
view層
顯示以及action
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
|
/** * */ package com.action; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.dao.impl.UserDaoImpl; import com.opensymphony.xwork2.ActionSupport; /** * @author kevin * */ public class UserAction extends ActionSupport { public String name; public String password; private UserDaoImpl userDao; public String getName() { return name; } public void setUserDao(UserDaoImpl userDao) { this .userDao = userDao; } public UserDaoImpl getUserDao() { return userDao; } public void setName(String name) { this .name = name; } public String getPassword() { return password; } public void setPassword(String password) { this .password = password; } @Override public String execute() throws Exception { // 不能直接new 得從applicationContext中獲取 HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType( "text/xml;charset=UTF-8" ); User user = new User(); user.name = name; user.password = password; userDao.save(user); response.getWriter().write(user.toString()); return "success" ; } } |
第一個(gè)頁(yè)面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <!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 >Insert title here</ title > </ head > < body > < h1 >測(cè)試</ h1 > < s:form action = "user" > < s:textfield name = "name" label = "username" ></ s:textfield > < s:textfield name = "password" label = "password" ></ s:textfield > < s:submit ></ s:submit > </ s:form > </ body > </ html > |
第二個(gè)頁(yè)面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<%@ 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 >Insert title here</ title > </ head > < body > < h1 >媽的智障</ h1 > ${name} ${password} </ body > </ html > |
配置文件
添加全局web配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<? 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" xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id = "WebApp_ID" version = "3.0" > < display-name >SpringSS</ 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 > < filter > < filter-name >struts2</ filter-name > < filter-class > org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </ filter-class > </ filter > < listener > < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class > </ listener > < filter-mapping > < filter-name >struts2</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > </ web-app > |
Spring配置文件
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
|
<?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" xmlns:aop= "http://www.springframework.org/schema/aop" xsi:schemaLocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans-3.0.xsd http: //www.springframework.org/schema/aop http: //www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id= "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource" > <property name= "driverClassName" > <value>com.mysql.jdbc.Driver</value> </property> <property name= "url" > <value>jdbc:mysql: //localhost/spring</value> </property> <property name= "username" > <value>root</value> </property> <property name= "password" > <value> 123456 </value> </property> </bean> <bean id= "sessionFactory" class = "org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" > <property name= "dataSource" > <ref local= "dataSource" /> </property> <property name= "annotatedClasses" > <list> <value>com.action.User</value> </list> </property> <property name= "hibernateProperties" > <props> <prop key= "hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop> <prop key= "show_sql" > true </prop> </props> </property> </bean> <bean id= "userDao" class = "com.dao.impl.UserDaoImpl" > <property name= "sessionFactory" > <ref local= "sessionFactory" /> </property> </bean> </beans> |
Struts配置文件
1
2
3
4
5
6
7
8
9
10
11
|
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" > <struts> <constant name= "struts.objectFactory" value= "spring" /> < package name= "default" extends = "struts-default" > <action name= "user" class = "userAction" > <result name= "success" >/user.jsp</result> </action> </ package > </struts> |
結(jié)果顯示
輸入頁(yè)面
結(jié)果頁(yè)面
數(shù)據(jù)庫(kù)
最后看起來(lái),還是不難的嘛。其實(shí)UserDao可以抽象出來(lái),只需要單次注入,等以后再完善。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)服務(wù)器之家的支持。
原文鏈接:https://blog.jiangtao.tech/2017/01/17/Spring集成Struts和Hibernate/