寫了好多篇的Android代碼了,在寫幾篇關於Java的,博客園裏確定都是java的前輩啊,寫的很差多給意見。html
SSH,什麼是SSH呢,Struts+Spring+Hibernate,這三個就是整個的SSH了。java
集成SSH框架的系統從職責上分爲四層:表示層、業務邏輯層、數據持久層和域模塊層,以幫助開發人員在短時間內搭建結構清晰、可複用性好、維護方便的Web應用程序。其中使用Struts做爲系統的總體基礎架構,負責MVC的分離,在Struts框架的模型部分,控制業務跳轉,利用Hibernate框架對持久層提供支持,Spring作管理,管理struts和hibernate。具體作法是:用面向對象的分析方法根據需求提出一些模型,將這些模型實現爲基本的Java對象,而後編寫基本的DAO(Data Access Objects)接口,並給出Hibernate的DAO實現,採用Hibernate架構實現的DAO類來實現Java類與數據庫之間的轉換和訪問,最後由Spring作管理,管理struts和hibernate。web
須要的Jar包:spring
首先看配置applicationContext.xml的代碼,注意這個的名稱別寫錯了:sql
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- Spring和Hibernate的整合分爲兩種 第一種:加載整個hibernate配置文件 第二種:首先配置數據與而後配置會話工廠 --> <!-- 這個是須要hibernate.cfg。xml文件 --> <!-- 第一種配置:加載整個hibernate配置文件 1 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean> --> <!-- ==============================================第二種方式===================================================== --> <!-- 第二種不要sessionFactory 2 2 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="url" value="jdbc:oracle:thin:@localhost:1521:****"></property> <property name="driverClassName" value="oracle.jdbc.OracleDriver"></property> <property name="username" value="****"></property> <property name="password" value="****"></property> </bean> <!-- 加載數據庫鏈接 2--> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 加載實體類配置文件 --> <!-- 加載工具例如 show_sql --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <!-- 加載實體類配置文件 --> <!-- 第二種的 另外另種配置實體類方式 1 這一個是找的具體的***。hbm.xml文件--> <!-- <property name="mappingResources"> <list> <value>com/dc/entity/Dept.hbm.xml</value> <value>com/dc/entity/Emp.hbm.xml</value> </list> </property> --> <!-- 第二種的 另外另種配置實體類方式 2 這個是找的所在的包 --> <property name="mappingDirectoryLocations"> <list> <value>classpath:com/dc/entity/</value> </list> </property> </bean> <!-- ==============================================第二種方式===================================================== --> <!-- 只是上邊不同,這倆個都有 --> <!-- hibernateTemplate --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- ============================Dao的配置 ============================--> <bean id="empDao" class="com.dc.dao.impl.SaleOrderLineDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> <!-- SaleOrderDaoImpl --> <bean id="SaleOrderDao" class="com.dc.dao.impl.SaleOrderDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> <!-- ============================Service的配置============================ --> <bean id="empService" class="com.dc.service.impl.SaleOrderLineServiceImpl"> <property name="soldao" ref="empDao"></property> </bean> <!-- SaleOrderServiceImpl --> <bean id="SaleOrderService" class="com.dc.service.impl.SaleOrderServiceImpl"> <property name="sodao" ref="SaleOrderDao"></property> </bean> <!-- ============================Action的配置============================ --> <bean id="empAction" class="com.dc.action.SaleOrderLineAction"> <property name="solSer" ref="empService"></property> </bean> <bean id="getInfoById" class="com.dc.action.SaleOrderLineAction"> <property name="solSer" ref="empService"/> </bean> <bean id="getMaxOdrId" class="com.dc.action.SaleOrderAction"> <property name="soSer" ref="SaleOrderService"></property> </bean> <bean id="addSaleOrder" class="com.dc.action.SaleOrderAction"> <property name="soSer" ref="SaleOrderService"></property> </bean> <!-- ==============================================配置事物管理器===================================================== --> <!-- 配置事務管理器 --> <bean id="txTran" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 聲明式事物 --> <!-- <tx:method name="" read-only="false" propagation="REQUIRED"></tx:method> read-only查詢時使用 propagation修改時使用 --> <tx:advice id="txAdvice" transaction-manager="txTran"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="up*" propagation="REQUIRED"/> <tx:method name="dl*" propagation="REQUIRED"/> <!-- 下面的是查詢的 --> <tx:method name="get*" read-only="true"/> <tx:method name="find*" read-only="true"/> <tx:method name="ser*" read-only="true"/> <tx:method name="load*" read-only="true"/> </tx:attributes> </tx:advice> <!-- --> <aop:config> <aop:pointcut expression="execution(* com.dc.dao.*.*(..))" id="pointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> </aop:config> </beans>
struts.xml的配置數據庫
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 該屬性設置Struts 2是否支持動態方法調用,該屬性的默認值是true。若是須要關閉動態方法調用,則可設置該屬性爲false。 --> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <!-- 該屬性設置Struts 2應用是否使用開發模式。若是設置該屬性爲true,則能夠在應用出錯時顯示更多、更友好的出錯提示。該屬性只接受true和flase兩個值,該屬性的默認值是false。一般,應用在開發階段,將該屬性設置爲true,當進入產品發佈階段後,則該屬性設置爲false。 --> <constant name="struts.devMode" value="true" /> <constant name="struts.i18n.encoding" value="UTF-8" /> <constant name="struts.configuration.xml.reload" value="true" /> <constant name="strus.objectFactory" value="spring"></constant> <package name="default" namespace="/" extends="struts-default"> <action name="show" class="empAction"> <result name="success">findAllSaleOrderLine.jsp</result> </action> <!-- <action name="getSaleOrderLineInfoByoolId" class="com.dc.action.SaleOrderLineAction" method="getInfoById"> <result name="success">getSaleOrderLineInfoByoolId.jsp</result> </action> --> <!-- 不建議使用這個,配合上邊的constant --> <action name="getSaleOrderLineInfoByoolId" class="getInfoById" method="getInfoById"> <result name="success">getSaleOrderLineInfoByoolId.jsp</result> </action> <action name="getMaxOdrId" class="getMaxOdrId" method="getMaxOdrId"> <result name="success">addSaleOrder.jsp</result> </action> <action name="addSaleOrder" class="addSaleOrder" method="addSaleOrder"> <result name="success" type="redirectAction">show</result> <result name="error" type="redirectAction">getMaxOdrId</result> <!-- <result name="input">index.jsp</result> --> </action> </package> </struts>
web.xml的配置express
<?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>Spring_04_Sale</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> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <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>*.action</url-pattern> </filter-mapping> <filter> <filter-name>EncodingFilter</filter-name> <filter-class>com.dc.util.EncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>