日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用maven整合SSH框架详细步骤

發(fā)布時間:2024/9/30 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用maven整合SSH框架详细步骤 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

(文章所使用的的框架為Struts2+Spring+Hibernate,項目的結(jié)構(gòu)圖參照文章尾部)

1、第一步:創(chuàng)建maven工程,在pom.xml文件中導(dǎo)入需要的jar包依賴:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zwp</groupId><artifactId>ssh</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><!-- 定義屬性 --><properties><spring.version>4.2.4.RELEASE</spring.version><hibernate.version>5.0.7.Final</hibernate.version><struts.version>2.3.24</struts.version></properties><!-- 鎖定版本,struts2-2.3.24、spring4.2.4、hibernate5.0.7 --><dependencyManagement><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-core</artifactId><version>${hibernate.version}</version></dependency><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-core</artifactId><version>${struts.version}</version></dependency><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-spring-plugin</artifactId><version>${struts.version}</version></dependency></dependencies></dependencyManagement><!-- 依賴管理 --><dependencies><!-- spring --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId></dependency><!-- 導(dǎo)入 struts2 --><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-core</artifactId></dependency><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-spring-plugin</artifactId></dependency><!-- hibernate --><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-core</artifactId></dependency><!-- 數(shù)據(jù)庫驅(qū)動 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version><scope>runtime</scope></dependency><!-- c3p0 --><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version></dependency><!-- servlet jsp --><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jsp-api</artifactId><version>2.0</version><scope>provided</scope></dependency><!-- 日志 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.2</version></dependency><!-- 單元測試 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><!-- jstl --><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency></dependencies><build><plugins><!-- 設(shè)置編譯版本為1.8 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin><!-- 默認的tomcat6不支持jdk1.8,訪問頁面時報錯:頁面編譯成.class失敗解決:配置tomcat7插件:mvn命令改為:tomcat7:run --><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version><configuration><url>http://127.0.0.1:8080/manager/text</url><server>tomcat8</server><path>/ssh</path></configuration></plugin></plugins></build> </project>

?

2、第二步:搭建struts2環(huán)境:

(1)創(chuàng)建action,創(chuàng)建struts.xml配置文件,配置action:

//Customer的web層: public class CustomerAction extends ActionSupport {//根據(jù)主鍵查詢:public String findOne() throws Exception{return SUCCESS;} } <?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><package name="demo" extends="struts-default" namespace="/"><!-- 和spring整合后,action將交由spring創(chuàng)建,class的值寫bean的id值 --><action name="customerAction_*" class="com.zwp.web.CustomerAction" method="{1}"><result name="success">/index.jsp</result></action></package> </struts>

(2)配置struts2的過濾器(web.xml文件):

<!-- 配置strut2過濾器 --><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>

?

3、第三步:搭建hibernate環(huán)境:

(1)創(chuàng)建實體類:

//Customer實體類: public class Customer {private String custId;private String custName;private String address;public String getCustName() {return custName;}public void setCustName(String custName) {this.custName = custName;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public String getCustId() {return custId;}public void setCustId(String custId) {this.custId = custId;} }

(2)配置實體類和數(shù)據(jù)庫表映射關(guān)系:Customer.hbm.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- 創(chuàng)建映射配置文件 --> <hibernate-mapping><class name="com.zwp.domain.Customer" table="t_cust"><id name="custId" column="custId"><generator class="uuid"></generator> </id><property name="custName" column="custName"></property><property name="address" column="address"></property></class> </hibernate-mapping>

(3)創(chuàng)建hibernate核心配置文件:hibernate.cfg.xml

- 引入映射配置文件

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property><property name="hibernate.show_sql">true</property><property name="hibernate.format_sql">true</property><property name="hibernate.hbm2ddl.auto">update</property><!-- 把映射文件放到核心配置文件中 --><mapping resource="com/zwp/domain/Customer.hbm.xml" /> </session-factory> </hibernate-configuration>

?

4、第四步:搭建spring環(huán)境:

(1)創(chuàng)建spring核心配置文件:applicationContext.xml

(2)讓spring配置文件在服務(wù)器啟動時候加載(web.xml文件)

- 配置監(jiān)聽器

- 指定spring配置文件位置

<!-- 配置監(jiān)聽器,并指定服務(wù)器在啟動的時候加載配置文件 --> <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>

?

5、第五步:struts2和spring整合:

(1)把action在spring配置(action多實例的):applicationContext.xml文件

<?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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"<!-- 創(chuàng)建對象與對象注入: --><bean id="customerAction" class="com.zwp.web.CustomerAction" scope="prototype"></bean> </beans>

(2)在struts.xml中action標(biāo)簽class屬性里面的值該寫成 bean的id值:

<?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><package name="demo" extends="struts-default" namespace="/"><!-- 將action的創(chuàng)建交由spring,class的值寫bean的id值 --><action name="customerAction_*" class="customerAction" method="{1}"><result name="success">/index.jsp</result></action></package> </struts>

?

6、第六步:spring和hibernate整合:

(1)把hibernate核心配置文件中數(shù)據(jù)庫配置,在spring里面配置:

(2)把hibernate的sessionFactory在spring配置

<!-- 加載屬性文件 --> <context:property-placeholder location="classpath:db.properties"/><!-- 配置連接池 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driver}"></property><property name="jdbcUrl" value="${jdbc.url}"></property><property name="user" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean><!-- 把hibernate的sessionFactory在spring配置文件中創(chuàng)建 --><bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"><!-- 注入dataSource屬性 --><property name="dataSource" ref="dataSource"></property><!-- 指定hibernate核心配置文件的位置 --><property name="configLocations" value="classpath:hibernate.cfg.xml"></property></bean>

db.properties文件:

jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:///ssh jdbc.username=root jdbc.password=admin

?

7、第七步:完成互相注入關(guān)系(在dao里面使用hibernateTemplate):

(1)在dao注入hibernateTemplate對象

(2)在hibernateTemplate對象中注入sessionFactory

(3)在service層注入dao層對象

(4)在web層注入service層對象

//Customer的Dao層接口 public interface CustomerDao {Customer findOne(String custId); } //Customer的Dao層接口實現(xiàn)類,繼承HibernateDaoSupport,該類已經(jīng)注入了hibernateTemplate對象。 public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {@Overridepublic Customer findOne(String custId) {return this.getHibernateTemplate().get(Customer.class, custId);} } //Customer的Service層接口: public interface CustomerService {Customer findOne(String custId); } //Customer的Service接口實現(xiàn)類: public class CustomerServiceImpl implements CustomerService{//注入CustomerDaoprivate CustomerDao customerDao;public void setCustomerDao(CustomerDao customerDao) {this.customerDao = customerDao;}public Customer findOne(String custId) {return customerDao.findOne(custId);} } //Customer的web層: public class CustomerAction extends ActionSupport {//注入CustomerService對象;private CustomerService customerService;public void setCustomerService(CustomerService customerService) {this.customerService = customerService;}//屬性注入:private String custId;public void setCustId(String custId) {this.custId = custId;}//根據(jù)主鍵查詢:public String findOne() throws Exception{Customer customer = customerService.findOne(custId);ActionContext.getContext().getValueStack().push(customer);return SUCCESS;} }

applicationContext.xml:

<!-- 開啟事務(wù)注解:以下對象的創(chuàng)建和注入,可以使用注解創(chuàng)建 --><!-- 第一步:開啟注解驅(qū)動;第二步:在類上使用注解 --><!-- <tx:annotation-driven transaction-manager="transactionManager"/> --><!-- 創(chuàng)建對象與對象注入: --><bean id="customerDao" class="com.zwp.dao.impl.CustomerDaoImpl"><property name="sessionFactory" ref="sessionFactory"></property></bean><bean id="customerService" class="com.zwp.service.impl.CustomerServiceImpl"><property name="customerDao" ref="customerDao"></property></bean><bean id="customerAction" class="com.zwp.web.CustomerAction" scope="prototype"><property name="customerService" ref="customerService"></property></bean>

(5)之前在hibernate配置與本地線程綁定的session:

<!-- 配置與本地線程綁定的session --> <property name="hibernate.current_session_context_class">thread</property>

在hibernate與spring整合后,不需要在spring里面配置與本地線程綁定的session。

?

8、第八步:配置事務(wù):(applicationContext.xml)

<!-- 配置事務(wù) --><bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"><!-- 注入sessionFactory,因為sessionFactory里面包含了dataSource對象 --><property name="sessionFactory" ref="sessionFactory"></property></bean><!-- xml方式管理事務(wù): --><!-- 配置通知:具體增強邏輯 --><tx:advice id="txAdvice"><tx:attributes><!-- 匹配業(yè)務(wù)類中方法名稱 --><tx:method name="save*"/><tx:method name="update*"/><tx:method name="delete*"/><tx:method name="find*" read-only="true"/><tx:method name="*"/></tx:attributes></tx:advice><!-- 配置aop --><aop:config><!-- 配置切點:具體哪些方法要增強(真正被增強的方法) --><aop:pointcut expression="execution(* com.zwp.serviec.*.*(..))" id="cut"/><!-- 配置切面:將增強邏輯作用到切點 --><aop:advisor advice-ref="txAdvice" pointcut-ref="cut"/></aop:config>

?

9、第九步:編寫成功頁面index.jsp:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>主頁</title> </head> <body><!-- 取出push進值棧里面的值 --><s:property value="custName"/>歡迎光臨 </body> </html>

?

10、第十步? 啟動并測試:

(1)啟動maven項目輸入命令:tomcat7:run。

(2)在瀏覽器地址欄輸入:http://localhost:8080/ssh/customerAction_findOne.action?custId=1

(3)出現(xiàn)以下界面和數(shù)據(jù)則代表成功:

?

附:項目的工程結(jié)構(gòu):

?

總結(jié)

以上是生活随笔為你收集整理的使用maven整合SSH框架详细步骤的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。