1. 程式人生 > >SSH框架搭建詳細步驟整理

SSH框架搭建詳細步驟整理

學習Java面前有兩座山,一座山叫SSM,一座山叫SSH,跨越了這兩座山之後才能感受到這個語言的魅力所在,SSM框架的搭建詳細在之前部落格已經涉及了,今天來整理SSH框架詳細步驟;

生有涯  而  學無涯

搭建步驟有:


建立Web Project專案;
匯入jar包
配置web.xml相關資訊
配置applicationContext.xml相關資訊
配置JDBC
配置Struts.xml
配置log4j日誌檔案

 


首先,開啟你的MyEclipse 2015;建立一個新的Web Project專案;其中選擇jdk版本為1.8;Tomcat版本為tomcat8;next–>next 勾選web.xml配置檔案,finish;

 

 


然後需要匯入Jar包,將Jar包全部匯入至lib資料夾下;(如需Jar包可加QQ:1090239782,可分享與您);

 


開啟web.xml配置Spring,Struts 以及中文亂碼解決;該部分直接附上原始碼:

<?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>SSH</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>
  <!-- 配置監聽器 -->
    <context-param>
        <!-- 上下文配置路徑 -->
        <param-name>contextConfigLocation</param-name>
        <!-- 訪問spring配置資訊 -->
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!-- 配置spring啟動listener入口 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 中文亂碼解決 -->
    <filter>
        <filter-name>EncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>EncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- Struts 2配置資訊 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
      <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
</web-app>123456789101112131415161718192021222324252627282930313233343536373839404142434445

 

然後,配置application-Context.xml檔案,注意Hibernate的版本;
資料來源的配置與SSM不同;


配置 application-Context.xml檔案中Hibernate的事務處理器及配置檔案;附上原始碼:

 

<?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.xsd 
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd 
        http://www.springframework.org/schema/jee
        http://www.springframework.org/schema/jee/spring-jee.xsd 
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">   
    <context:component-scan base-package="com.jredu" />
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean id="dataSource"
        class="com.mchange.v2.c3p0.ComboPooledDataSource"
        p:driverClass="${jdbc.driverClassName}"
        p:jdbcUrl="${jdbc.url}"
        p:user="${jdbc.username}"
        p:password="${jdbc.password}"
        p:initialPoolSize="${jdbc.initialSize}"
        p:maxPoolSize="${jdbc.maxActive}"/>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="hibernateProperties">
            <props>
                <prop key="show_sql">true</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hbm2ddl.auto">update</prop>
                <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
            </props>
        </property>
        <!-- Hibernate的對映檔案 -->
        <property name="mappingLocations">
            <list>
                <value>classpath:com/jredu/entity/*.hbm.xml</value>
            </list>
        </property>

    </bean>
    <!-- 與SSM框架的區別所在 -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="append*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="modify*" propagation="REQUIRED" />
            <tx:method name="edit*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="repair" propagation="REQUIRED" />
            <tx:method name="delAndRepair" propagation="REQUIRED" />
            <tx:method name="get*" propagation="SUPPORTS" />
            <tx:method name="find*" propagation="SUPPORTS" />
            <tx:method name="load*" propagation="SUPPORTS" />
            <tx:method name="search*" propagation="SUPPORTS" />
            <tx:method name="datagrid*" propagation="SUPPORTS" />
            <tx:method name="*" propagation="SUPPORTS" />
        </tx:attributes>
    </tx:advice>
    <aop:config> 
        <aop:pointcut id="serviceOperation" 
            expression="execution(* com.jredu.service.*.*(..))" /> 
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" /> 
    </aop:config>
    <!-- 通過Spring來管理Action -->
    <bean id="userAction" class="com.jredu.action.UserAction"></bean>
</beans>12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182

 

然後,配置JDBC;


附上原始碼:

 

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc\:oracle\:thin\:@localhost\:1521\:XXXX
jdbc.username=XXXXXXXX
jdbc.password=XXXXXXX
jdbc.initialSize=0 
jdbc.maxActive=20 
jdbc.maxIdle=20 
jdbc.minIdle=1 
jdbc.maxWait=60000  123456789

 

配置Struts 配置檔案及其國際化配置;

 

附上Struts配置檔案原始碼:

 

<?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">
<struts>
    <!-- struts預設樣式是xhtml -->
    <!-- <constant name="struts.ui.theme" value="simple"/> -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <!-- 設定預設資原始檔 -->
    <constant name="struts.custom.i18n.resources" value="message_zh_HK"/>
    <!-- 設定編碼格式 -->
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <!-- struts結合spring的配置意思是  Struts2的action由Spring來負責進行例項化  -->
    <constant name="struts.objectFactory" value="spring" />
    <!-- 建立一個default包,繼承自Struts2的struts-default包 -->
    <package name="default" namespace="/" extends="struts-default">
    </package>
</struts>123456789101112131415161718

 

配置log4j 日誌檔案(log4j.properties)

 


至此,完整的SSH框架已經搭建完畢,下篇部落格,就介紹一個SSH框架完成一個小的登入功能;
---------------------
原文:https://blog.csdn.net/An1090239782/article/details/78406650