由于SSH組合開發在實際的大型項目中是用得最多的,所有SSH的環境搭配對于開發人員來說也是比較重要的。
SSH的環境搭配主要包括兩個方面:Struts與Spring的組合;Hibernate與Spring的組合.
SSH的組合工作主要有兩步:導入需要的Jar包;實現SSH的組合
第一步,導入需要的Jar包
在MyEclipse中,導入SSH的順序應該是Spring、Hibernate、Struts。
導入的操作比較簡單,其中最需要注意的就是在導入Hibernate的時候要選擇通過Spring的applicationContext.xml文件對Hibernate進行管理,即不需要創建hibernate.cfg.xml的原因。這也是為什么Spring在Hibernate之前導入的原因了。
第二步,實現SSH的組合,這也是SSH組合的重點
首先是Struts與Spring的組合,他們的組合無非就是將Struts的Action交給Spring管理,這需要添加一個插件,在struts-config.xml文件中生成等如下的代碼:
在上述代碼中是添加了一個ContextLoaderPlugIn插件,并添加了一個屬性contextConfigLocation,其指向的是/WEB-INF/classes/applicationContext.xml,即Spring的控制文件,除此之外,還需要添加一個controller,所以最終在struts-config.xml文件生成的代碼為:
其中的controller的作用是對所有的Struts的請求都叫給Spring管理。
struts-config.xml文件修改完了以后,還需要在web.xml文件中添加一個context-param,具體代碼如下:
這一點是與struts-config.xml文件中的plugin中的屬性相對應的。
到此為止,Struts與Spring的組合搭配就算完成了。
然后就是Hibernate與Spring的搭配了,現在的applicationContext.xml文件應該為如下的內容:
在上述代碼中,應在sessionFactory的HibernateProperties屬性中添加相應內容,最后的效果如下:
接著在applicationContext.xml文件添加如下的節點:
最后是在web.xml文件添加對session的管理代碼:
在以后的文章中我會介紹這樣組合以后如何使用各個框架,以上這些也是我在學習SSH的過程中的一些總結吧,希望能對各位同樣的JAVA愛好有一定的幫助。
QQ交流群:90623790
SSH的環境搭配主要包括兩個方面:Struts與Spring的組合;Hibernate與Spring的組合.
SSH的組合工作主要有兩步:導入需要的Jar包;實現SSH的組合
第一步,導入需要的Jar包
在MyEclipse中,導入SSH的順序應該是Spring、Hibernate、Struts。
導入的操作比較簡單,其中最需要注意的就是在導入Hibernate的時候要選擇通過Spring的applicationContext.xml文件對Hibernate進行管理,即不需要創建hibernate.cfg.xml的原因。這也是為什么Spring在Hibernate之前導入的原因了。
第二步,實現SSH的組合,這也是SSH組合的重點
首先是Struts與Spring的組合,他們的組合無非就是將Struts的Action交給Spring管理,這需要添加一個插件,在struts-config.xml文件中生成等如下的代碼:
1
<plug-in
2
className="org.springframework.web.struts.ContextLoaderPlugIn">
3
<set-property property="contextConfigLocation"
4
value="/WEB-INF/classes/applicationContext.xml" />
5
</plug-in>

2

3

4

5

在上述代碼中是添加了一個ContextLoaderPlugIn插件,并添加了一個屬性contextConfigLocation,其指向的是/WEB-INF/classes/applicationContext.xml,即Spring的控制文件,除此之外,還需要添加一個controller,所以最終在struts-config.xml文件生成的代碼為:
1
<controller
2
processorClass="org.springframework.web.struts.DelegatingRequestProcessor">
3
</controller>
4
5
<plug-in
6
className="org.springframework.web.struts.ContextLoaderPlugIn">
7
<set-property property="contextConfigLocation"
8
value="/WEB-INF/classes/applicationContext.xml" />
9
</plug-in>

2

3

4

5

6

7

8

9

其中的controller的作用是對所有的Struts的請求都叫給Spring管理。
struts-config.xml文件修改完了以后,還需要在web.xml文件中添加一個context-param,具體代碼如下:
1
<context-param>
2
<param-name>contextConfigLocation</param-name>
3
<param-value>
4
/WEB-INF/classes/applicationContext.xml
5
</param-value>
6
</context-param>

2

3

4

5

6

這一點是與struts-config.xml文件中的plugin中的屬性相對應的。
到此為止,Struts與Spring的組合搭配就算完成了。
然后就是Hibernate與Spring的搭配了,現在的applicationContext.xml文件應該為如下的內容:
1
<bean id="dataSource"
2
class="org.springframework.jndi.JndiObjectFactoryBean">
3
<property name="jndiName" value="java:comp/env/jdbc/demo"></property>
4
</bean>
5
<bean id="sessionFactory"
6
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
7
<property name="dataSource">
8
<ref bean="dataSource" />
9
</property>
10
<property name="hibernateProperties">
11
<props></props>
12
</property>
13
</bean>

2

3

4

5

6

7

8

9

10

11

12

13

在上述代碼中,應在sessionFactory的HibernateProperties屬性中添加相應內容,最后的效果如下:
1
<bean id="sessionFactory"
2
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
3
<property name="dataSource">
4
<ref bean="dataSource" />
5
</property>
6
<property name="hibernateProperties">
7
<props>
8
<prop key="hibernate.dialect">
9
org.hibernate.dialect.MySQLDialect
10
</prop>
11
<prop key="hibernate.connection.autocommit">true</prop>
12
<prop key="hibernate.show_sql">true</prop>
13
</props>
14
</property>
15
<property name="mappingResources">
16
<list>
17
</list>
18
</property>
19
</bean>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

接著在applicationContext.xml文件添加如下的節點:
1
<bean id="hibernateTemplate"
2
class="org.springframework.orm.hibernate3.HibernateTemplate">
3
<property name="sessionFactory">
4
<ref bean="sessionFactory" />
5
</property>
6
</bean>

2

3

4

5

6

最后是在web.xml文件添加對session的管理代碼:
1
<filter>
2
<filter-name>opensession</filter-name>
3
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
4
</filter>
5
<filter-mapping>
6
<filter-name>opensession</filter-name>
7
<url-pattern>*.jsp</url-pattern>
8
</filter-mapping>
完成這一步以后呢,那么整個SSH組合的搭配基本也就結束了,你就可以利用Spring的強大功能完成對Struts和Hibernate的控制了。
2

3

4

5

6

7

8

在以后的文章中我會介紹這樣組合以后如何使用各個框架,以上這些也是我在學習SSH的過程中的一些總結吧,希望能對各位同樣的JAVA愛好有一定的幫助。
QQ交流群:90623790