java ssh三大框架搭建整合


今天開個坑java ssh三大框架搭建整合(注解+XML 用全注解不是很好,有點地方需要用模板的時候XML直接可以復制一段)

1 所用框架、技術

編號

工具

版本

說明

  1.  

Struts 2

2.3.20

 

  1.  

Hibernate

4.3.9

實現持久化操作

  1.  

Spring

4.1.5

 

  1.  

Junit

4

單元測試

 

 

 

 

 

 

 

 

2.  開發環境

操作系統

Windows 7

開發工具

Eclipse Java EE 

數據庫

Oracle 11g

Web容器

Tomcat 7.0.63

JAVA

JDK 1.7

 

 

 

 

 

 

 

 

 

 

3.建立project

新建一個 dynamic web project

 

最終的整個工程結構是這樣的

 

4.添加Junit

configure Build Path 后點Add library 選junit4 即可

 

5.添加Struts2

copy所需的jar包到lib文件夾(先排除struts2-spring-plugin.jar后面會說明原因) 

 

准備struts.xml,web.xml

 

模板,直接在Struts文件夾里面搜索文件名struts.xml,web.xml。

 

web.xml(示例)   

我這些XML 元素web-app 有版本信息,不一致會報錯。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app  version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 5 
 6     
 7     <!-- 配置Struts2的核心的過濾器 -->
 8     <filter>
 9         <filter-name>struts2</filter-name>
10         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
11     </filter>
12     <filter-mapping>
13         <filter-name>struts2</filter-name>
14         <url-pattern>/*</url-pattern>
15     </filter-mapping>
16 
17 
18         <!-- 程序啟動頁面-->
19     <welcome-file-list>
20         <welcome-file>index.jsp</welcome-file>
21     </welcome-file-list>
22 
23 </web-app>

 

struts.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7 
 8     <!-- 配置為開發模式 -->
 9     <constant name="struts.devMode" value="true" />
10     <!-- 把擴展名配置為action -->
11     <constant name="struts.action.extension" value="action" />
12     <!-- 把主題配置為simple -->
13     <constant name="struts.ui.theme" value="simple" />
14     
15    
16     <package name="default" namespace="/" extends="struts-default">
17       
18         <!-- 配置測試用的Action,未與Spring整合,class屬性寫類的全名(包名.類名) -->
19         <!-- 當Struts2與Spring整合后,class屬性可以寫bean的名稱(類名首字母小寫) -->
20         <action name="test" class="edu.hainu.knowledge.test.StrutsAction">
21             <result name="success">/index.jsp</result>
22         </action>
23         
24         
25     </package>
26 
27 
28 </struts>

 

StrutsAction.java(放在如圖位置)

 1 package edu.hainu.knowledge.test;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 
 6 public class StrutsAction extends ActionSupport{
 7 
 8     //用於 測試單個Struts是否成功
 9     public String execute(){
10         System.out.println("success");
11         return "success";
12     }
13 }

 

 

啟動觀察控制台有沒有報錯,報錯的自行百度

用瀏覽器http://localhost:8080/edu.hainu.knowledge/test.action

 

http://服務器所在ip:端口號/projectName/actionName.action(注意我已經在struts.xml 配置類后綴為action)

其實這個地址可以也不用記住,我們可以直接run 一個頁面,eclipse會自動訪問該頁面

頁面成功跳轉,添加struts2成功。

 

 

6.添加spring

添加jar包

 

applicationContext.xml (示例)

同樣的可以去spring文件夾搜索applicationContext.xml (QAQ 不知道為什么找不到)

配置一下 component-scan base-package="edu.hainu.knowledge" 就是Sping將會掃描edu.hainu.knowledge包所有類上的注解

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:tx="http://www.springframework.org/schema/tx"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans
 7         http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context
 9         http://www.springframework.org/schema/context/spring-context.xsd
10             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
11         
12     <!-- 自動掃描與裝配bean -->
13     <context:component-scan base-package="edu.hainu.knowledge"></context:component-scan>
14     
15 </beans>
 

添加StrutsAction 的注解

@Controller
@Scope("prototype")
 1 package edu.hainu.knowledge.test;
 2 
 3 import org.springframework.context.annotation.Scope;
 4 import org.springframework.stereotype.Controller;
 5 
 6 import com.opensymphony.xwork2.ActionSupport;
 7 
 8 @Controller
 9 @Scope("prototype")
10 public class StrutsAction extends ActionSupport{
11 
12     //用於 測試單個Struts是否成功
13     public String execute(){
14         System.out.println("success");
15         return "success";
16     }
17 }
 

在junit的Spring類 

 1 package edu.hainu.knowledge.test;
 2 
 3 import org.junit.Test;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 
 8 
 9 public class Spring {
10     
11     private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
12 
13     
14     //用於測試單個Spring是否成功
15     @Test
16     public void testBean() throws Exception {
17         
18         //Bean的名字 首字母要小寫 
19         StrutsAction StrutsAction = (StrutsAction) ac.getBean("strutsAction");
20         System.out.println(StrutsAction);
21     }
22 
23 }
24  

單元測試testBean()方法,能正常輸出信息 edu.hainu.knowledge.test.StrutsAction@a383aab 表明添加Spring 成功

 

7.整合spring與struts2

在web.xml中配置Spring的監聽器 (配置一下applicationContext.xml  所在位置)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app  version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 5 
 6 
 7     <!-- 配置Spring的用於初始化容器對象的監聽器 -->
 8     <listener>
 9         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
10     </listener>
11     <context-param>
12         <param-name>contextConfigLocation</param-name>
13         <param-value>classpath:applicationContext*.xml</param-value>
14     </context-param>
15     
16     <!-- 配置Struts2的核心的過濾器 -->
17     <filter>
18         <filter-name>struts2</filter-name>
19         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
20     </filter>
21     <filter-mapping>
22         <filter-name>struts2</filter-name>
23         <url-pattern>/*</url-pattern>
24     </filter-mapping>
25 
26 
27 
28     <welcome-file-list>
29         <welcome-file>index.jsp</welcome-file>
30     </welcome-file-list>
31 </web-app>

 

添加jar包 struts2-spring-plugin-2.3.20.jar(這就是文章開頭struts2 排除的jar包,不然會報錯)

 

修改struts.xml  ( <!-- 當Struts2與Spring整合后,class屬性可以寫bean的名稱(類名首字母小寫) -->)

 

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7     <!-- 配置為開發模式 -->
 8     <constant name="struts.devMode" value="true" />
 9     <!-- 把擴展名配置為action -->
10     <constant name="struts.action.extension" value="action" />
11     <!-- 把主題配置為simple -->
12     <constant name="struts.ui.theme" value="simple" />
13     
14    
15     <package name="default" namespace="/" extends="struts-default">
16       
17         <!-- 配置測試用的Action,未與Spring整合,class屬性寫類的全名 -->
18         <!-- 當Struts2與Spring整合后,class屬性可以寫bean的名稱 -->
19         <action name="test" class="strutsAction">
20             <result name="success">/index.jsp</result>
21         </action>
22         
23         <!-- 日志管理 -->
24         <action name="log_*" class="logAction" method="{1}">
25             <result name="list">/WEB-INF/jsp/logAction/list.jsp</result>
26             <result name="saveUI">/WEB-INF/jsp/logAction/saveUI.jsp</result>
27             <result name="toList" type="redirectAction">log_list</result>
28         </action>
29         
30         <!-- 首頁 -->
31         <action name="home_*" class="homeAction" method="{1}">
32             <result name="{1}">/WEB-INF/jsp/homeAction/{1}.jsp</result>
33         </action>
34         
35     </package>
36 
37     <!-- Add packages here -->
38 
39 </struts>

 

重新啟動,訪問 http://localhost:8080/edu.hainu.knowledge/test.action 

成功跳轉表明 整合spring與struts2成功(主要是修改struts.xml 證明)

 

 

8.添加hibernate 並且整合 hibernate 與 spring (我的習慣是)

 

添加jar包 sqljdbc41.jar 用於sqlserver  ojdbc6.jar 用於Oracle

 

applicationContext.xml 

管理SessionFactory實例(只需要一個)

聲明式事務管理

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:tx="http://www.springframework.org/schema/tx"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans
 7         http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context
 9         http://www.springframework.org/schema/context/spring-context.xsd
10             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
11         
12     <!-- 自動掃描與裝配bean -->
13     <context:component-scan base-package="edu.hainu.knowledge"></context:component-scan>
14 
15     <!-- 導入外部的properties文件 -->
16     <context:property-placeholder location="classpath:jdbc.properties"/>
17 
18 
19     <!-- 配置SessionFactory -->
20     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
21         <!-- 指定hibernate的配置文件位置 -->
22         <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
23         <!-- 配置c3p0數據庫連接池 -->
24         <property name="dataSource">
25             <bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
26                 <!-- 數據連接信息 -->
27                 <property name="jdbcUrl" value="${jdbcUrl}"></property>
28                 <property name="driverClass" value="${driverClass}"></property>
29                 <property name="user" value="${user}"></property>
30                 <property name="password" value="${password}"></property>
31                 <!-- 其他配置 -->
32                 <!--初始化時獲取三個連接,取值應在minPoolSize與maxPoolSize之間。Default: 3 -->
33                 <property name="initialPoolSize" value="3"></property>
34                 <!--連接池中保留的最小連接數。Default: 3 -->
35                 <property name="minPoolSize" value="3"></property>
36                 <!--連接池中保留的最大連接數。Default: 15 -->
37                 <property name="maxPoolSize" value="5"></property>
38                 <!--當連接池中的連接耗盡的時候c3p0一次同時獲取的連接數。Default: 3 -->
39                 <property name="acquireIncrement" value="3"></property>
40                 <!-- 控制數據源內加載的PreparedStatements數量。如果maxStatements與maxStatementsPerConnection均為0,則緩存被關閉。Default: 0 -->
41                 <property name="maxStatements" value="8"></property>
42                 <!--maxStatementsPerConnection定義了連接池內單個連接所擁有的最大緩存statements數。Default: 0 -->
43                 <property name="maxStatementsPerConnection" value="5"></property>
44                 <!--最大空閑時間,1800秒內未使用則連接被丟棄。若為0則永不丟棄。Default: 0 -->
45                 <property name="maxIdleTime" value="1800"></property>
46             </bean>
47         </property>
48     </bean>
49     
50     <!-- 配置聲明式事務管理(采用注解的方式) -->
51     <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
52         <property name="sessionFactory" ref="sessionFactory"></property>
53     </bean>
54     <tx:annotation-driven transaction-manager="txManager"/>
55     
56     
57 </beans>

 

 jdbc.properties

//通過SERVICE_NAME連接

1 jdbcUrl    =jdbc:oracle:thin:@//ip:1521/SERVICE_NAME
2 driverClass    = oracle.jdbc.driver.OracleDriver
3 user        = user
4 password    = password

 

Hibernate.java 

 1 package edu.hainu.knowledge.test;
 2 
 3 import org.hibernate.SessionFactory;
 4 import org.junit.Test;
 5 import org.springframework.context.ApplicationContext;
 6 import org.springframework.context.support.ClassPathXmlApplicationContext;
 7 
 8 
 9 
10 public class Hibernate {
11 
12     private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
13 
14     // 測試SessionFactory
15     //用於測試Spring 與 hibernate
16     @Test
17     public void testSessionFactory() throws Exception {
18         SessionFactory sessionFactory = (SessionFactory) ac.getBean("sessionFactory");
19         System.out.println(sessionFactory);
20     }
21     
22 }

 

單元測試 testSessionFactory()

 控制台有輸出org.hibernate.internal.SessionFactoryImpl@5cc5e9d2

整合 hibernate 與 spring 成功

 

 

9.總結

struts 

1.導入相關jar包

2.准備 web.xml  struts2.xml

3.測試 action 成功跳轉代表成功。

 

spring

1.導入相關jar包

2.准備 applicationContext.xml 

3.測試 能否從spring管理中讀取出bean

 

struts 整 合spring

1.struts2-spring-plugin-2.3.20.jar

2.web.xml  配置spring監聽器

3.測試 struts.xml 當Struts2與Spring整合后,class屬性可以寫bean的名稱(類名首字母小寫)

 

hibernate  和 hibernate整合spring

1.導入相關jar包 (以及數據庫連接jar包)

2.applicationContext,xml  配置SessionFactory 配置事務管理  修改 jdbc.properties

3.測試 能否從spring管理中讀取出sessionFactory 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM