JAVA SHH框架搭建(多圖)
SHH架構圖
搭建SHH架構
1新建專案
2 sturs2配置
struts2引入jar包、struts2中的路徑為 struts-2.3.16.1\apps\struts2-blank\WEB-INF\lib
複製貼上到專案裡面
還有struts-2.3.16.1\apps\struts2-json-plugin-2.3.16.1.jar(用於序列化)
還有struts-2.3.16.1\apps\struts2-spring-plugin-2.3.16.1.jar
struts2配置檔案
Web.xml配置
過濾器
下面程式碼複製進去
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 後加的配置 --> <!-- 配置Spring的核心監聽器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 配置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> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping> <!-- 後加的配置 完畢--> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
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>
3 spring配置
要引入的 jar包:(從我給的jar裡都有)
Spring3.2 開發最基本
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.log4j-1.2.15.jar
AOP開發
spring-aop-3.2.0.RELEASE.jar
spring-aspects-3.2.0.RELEASE.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
SpringJdbc開發
spring-jdbc-3.2.0.RELEASE.jar
spring-tx-3.2.0.RELEASE.jar
Spring事務管理
spring-tx-3.2.0.RELEASE.jar
Spring整合其他ORM框架
spring-orm-3.2.0.RELEASE.jar
Spring在web中使用
spring-web-3.2.0.RELEASE.jar
Spring整合Junit測試
spring-test-3.2.0.RELEASE.jar
配置檔案
Web.xml
配置監聽器(程式碼在struts2配置時候有了)
將下面兩個複製到src中
applicationContext.xml
log4j.properties
如圖
4 Hibernate配置
引入jar
hibernate-distribution-3.6.10.Final\hibernate3.jar
hibernate-distribution-3.6.10.Final\lib\required\*.jar
hibernate-distribution-3.6.10.Final\lib\jpa\*.jar
整合的jar包 :slf4j-log4j
資料庫驅動:mysql-connector-java-5.0.4-bin.jar
連線池:(c3p0連線池)c3p0-0.9.1.jar
配置檔案:
沒有hibernate的核心配置檔案的方式整合:
配置applicationContext.xml
連結池
<!-- 配置連線池: -->
<!-- 引入外部屬性檔案 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置C3P0連線池: -->
<bean id="dataSource" 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}"/>
</bean>
事務管理:
配置applicationContext.xml
<!-- 事務管理: -->
<!-- 事務管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 開啟註解事務 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
Hibernate相關資訊:
配置applicationContext.xml
<!-- Hibernate的相關資訊 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入連線池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置Hibernate的其他的屬性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 配置Hibernate的對映檔案 -->
</bean>
以上配置在我給的applicationContext.xml都寫好了、咳咳咳= =
——————————簡單的搭建——————————
————————————chenchen————————————