新手SSH基礎框架搭建


 SSH 為 struts+spring+hibernate的一個集成框架,是目前較流行的一種Web應用程序開源框架。

 

首先我們先了解SSH的框架所需的包和基本概念:

 

一、下面我們先來了解一下struts2,下載地址:http://struts.apache.org/

Struts2作為系統的整體基礎架構,負責MVC的分離,在Struts2框架的模型部分,控制業務跳轉。

1、struts2所需十五個jar包

在你下載的里的struts-2.3.16.1\lib找出以下的包。

 

在相同目錄下,以后我們將action注入到spring中必不可少的struts2-spring-plugin.jar插件

 

 

二、接下來我們了解一下spring,下載地址:http://projects.spring.io/spring-framework/

Spring做為管理,支持struts和hibernate基本的功能,當然還有aop切面處理功能,和集成框架功能。

在你下載的目錄spring-framework-4.2.2.RELEASE-dist\spring-framework-4.2.2.RELEASE\libs下

包比較多,可以全部倒出來,然后把javadoc.jar(幫助文檔)后綴的,和sources.jar(源碼)后綴的刪除。

2、spring所需的九個jar包

三、然后我來看一下hibernate 下載地址:http://hibernate.org/orm/downloads/

利用Hibernate框架對持久層(就是把數據保存到可掉電式存儲設備中供之后使用這里指的是數據庫)提供支持。

3、hibernate需要八個包

在目錄E:hibernate-release-5.2.2.Final\lib\required下

hibernate中還有c3p0的jar包

在目錄hibernate-release-5.2.2.Final\lib\optional\c3p0下

如果用dbcp 需要這兩個包(百度包名下載就可以找到)

c3p0和dbcp的包不沖突可以同時存在,自由切換。

 

 最后不要忘了導入對應數據庫的驅動包:我這里用的是mysql

下載地址:http://dev.mysql.com/downloads/mysql/

 環境配置完畢。

 

 四、在web.xml配置文件加入Struts2(過濾器)和spring(監聽器)

3.0之后的版本要自己勾上xml文件

在創建項目的時候選擇next,最后一部記得勾上xml

然后分包:

 

在xml文件中加入

頭文件信息

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
xsi:schemaLocation
="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

 

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監聽器

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

 

寫strut.xml文件

我們先來寫action類

一般我們單獨寫在一個action包中


// 繼承ActionSupport,讓本類具備強大的功能(例如:校驗、國際化等)

public class IndexAction extends ActionSupport {
  
private static final long serialVersionUID = 1L;   private IndexServices is;   //這里用的是spring注入   public void setIs(IndexServices is) {     this.is = is;   }   //1.每一個對外的方法,都是返回String類型   //2.返回的字符串,要跟配置文件一一對應,用於跳轉   //3.execute是默認方法(當struts.xml沒有指定時,默認執行) // 類似於doGet和doPos   public String execute(){     return "success";   }

 

  當然返回的字符串不是唯一的,我們可以通過if語句控制放回值,通過下面的配置文件跳轉到不同的頁面

下面是struts.xml配置文件(struts控制跳轉)

注意這個xml不是寫在WEB-INF下面,默認寫在src目錄下。當然也可以通過配置<param-name>config</param-name>配置路徑。

頭文件信息:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd">

 

<!-- 上面的頭,注意版本,從樣例里復制過來 showcase.war\WEB-INF\src\java\struts.xml -->

 

<struts>
  <!--指定Struts2默認的ObjectFactoryBean,該屬性默認值是spring--!>   <constant name="struts.objectFactory" value="spring"></constant>   <!-- 第1步:先定義一個包 -->
  <package name="mypck" extends="struts-default">
    <!-- 第2步:定義一個action,配置跳轉信息 name 類似於Servlet @WebServlet("/IndexServlet")     http://xxxx/xxx/Index.action http://xxxx/xxx/Index class 對應於自己寫的Action類 當不寫method屬性時,默認調用的是execute -->
    <!--class屬性在action注入spring后和bean中id一一對應 后面spring注入的時候會提到--!>     <action name="Index" class="myIndexAction" method="execute">       <!-- /WEB-INF/是防止jsp不經過action就可以訪問-->
      <result name="success">/WEB-INF/jsp/index.jsp</result>
      <result name="error">/WEB-INF/jsp/error.jsp</result>
    </action>
  </package>
</struts>

 

 

接下來寫applicationContext.xml(用於spring注入)

注意applicationcontext.xml和struts.xml位置一樣,都是在src目錄下。

<?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" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

 

至於頭文件哪里來的,我們直接百度:“applicationcontext.xml頭文件” 就可以輕易找到。

接下來寫<bean>如下:

  <!-- 類似於財務部門一樣,類就是錢,所有需要類的實例都由srping去管理 --> <bean id="myIndexAction" class="ssh.action.IndexAction" scope="prototype"> <!-- setIs(myIndexService) --> <property name="is" ref="myIndexService"/> </bean> <!-- myIndexService = new service.IndexServiceImpl() --> <bean id="myIndexService" class="service.IndexServiceImpl" scope="prototype"> <property name="id" ref="myIndexDao"/> </bean> <bean id="myIndexDao" class="dao.IndexDaoImpl" scope="prototype"> <property name="sessionFactory" ref="mySessionFactory"></property> </bean>  

 

最后配置Hibernate我們可以在剛剛的applicationContext.xml中編輯,也可以重寫一個hibernate.cfg.xml文件

<bean id="mySessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入連接池,包含了數據庫用戶名,密碼等等信息 -->
<property name="dataSource" ref="myDataSource"/>


<property name="hibernateProperties">   <props>     <!--針對特點的關系型數據庫生成優化的SQL -->     <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>     <!--顯示SQL-->     <prop key="hibernate.show_sql">true</prop>     <!-- 格式化SQL -->     <prop key="hibernate.format_sql">true</prop>     <prop key="hibernate.connection.autocommit">false</prop>     <!-- 開機自動生成表 -->     <prop key="hibernate.hbm2ddl.auto">update</prop>   </props> </property>   

  <!--數據庫映射配置文件-->   <property name="mappingResources">     <list>       <!-- 映射文件位置 -->       <value>entity/BookCard.hbm.xml</value>     </list>   </property> </bean>

 

我們先來寫一下entity/BookCard.hbm.xml映射文件

文件位置必須與上面寫的路徑一致

 

 

 

 c3p0

 

<!-- 引入外部屬性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  <property name="driverClass" value="${jdbc.driver}"/>
  <property name="jdbcUrl" value="${jdbc.url}"/>
  <property name="user" value="${jdbc.user}"/>
  <property name="password" value="${jdbc.password}"/>
  <!-- 每300秒檢查所有連接池中的空閑連接 -->    
  <property name="idleConnectionTestPeriod" value="300"></property>
  <!-- 最大空閑時間,900秒內未使用則連接被丟棄。若為0則永不丟棄 -->
  <property name="maxIdleTime" value="900"></property>
  <!-- 最大連接數 -->    
  <property name="maxPoolSize" value="2"></property>
</bean>

 

dbcp和c3p0除了寫法和所需包不同其他基本一樣

<bean id="myDataSource" destroy-method="close" class="org.apache.commons.dbcp2.BasicDataSource">
  <property name="driverClassName" value="${jdbc.driver}"/>
  <property name="url" value="${jdbc.url}"/>
  <property name="username" value="${jdbc.user}"/>
  <property name="password" value="${jdbc.password}"/>
</bean>

 

最后我們寫一下jdbc.properties外部引用文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/CardDB
jdbc.user=root
jdbc.password=kkk

 整個ssh框架就搭建完畢了,我們最后看一下每個文件的具體位置:

 

如果結合列子講會更好一些。本人新手,如有不對的地方請及時指出,多多交流,共同進步,文明留言。

 


免責聲明!

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



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