SSH框架整合

文章根據傳智播客人事管理系系統OA整理,感謝傳智播客。

一、新建JavaWeb項目,並修改字符編碼。html

在項目上右擊選擇最後一項,修改成UTF-8
1.1添加Junit4,右擊項目選擇Build path
打開,
 
 
添加如下jar包,寫配置文件
二、添加struts2環境。
2.1添加過jar包以後,拷貝配置文件
2.2在web.xml中加入struts2最核心的過濾器
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <display-name>SSH-1</display-name>
  4. <!--Struts2過濾器 -->
  5. <filter>
  6. <!--過濾器名稱 -->
  7. <filter-name>struts2</filter-name>
  8. <!--過濾器類 -->
  9. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  10. </filter>
  11. <!--Struts2過濾器映射 -->
  12. <filter-mapping>
  13. <!--過濾器名稱 -->
  14. <filter-name>struts2</filter-name>
  15. <!--過濾器映射 -->
  16. <url-pattern>/*</url-pattern>
  17. </filter-mapping>
  18. <welcome-file-list>
  19. <welcome-file>index.jsp</welcome-file>
  20. </welcome-file-list>
  21. </web-app>
修改struts2的配置文件爲
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5. <struts>
  6. <!-- 更改struts默認的主題 -->
  7. <constant name="struts.ui.theme" value="simple"/>
  8. <!-- 配置爲開發模式 ,好處是修改過以後當即生效-->
  9. <constant name="struts.devMode" value="true"></constant>
  10. <!--聲明包 -->
  11. <package name="pm" namespace="/" extends="json-default">
  12. <action name="product_*" class="com.atfuture.PM.action.ProductAction" method="{1}">
  13. <result name="list">/WEB-INF/jsp/product/product_list.jsp</result>
  14. <result name="success" type="redirect">/product_list</result>
  15. <result name="input">/WEB-INF/jsp/product/product_input.jsp</result>
  16. <result name="productStockAll">/WEB-INF/jsp/product/product_stock.jsp</result>
  17. <result name="cg_input">/WEB-INF/jsp/product/product_input.jsp</result>
  18. <result name="fintByCategory">/WEB-INF/jsp/product/product_cg_list.jsp</result>
  19. <result name="addinput">/WEB-INF/jsp/product/product_addinput.jsp</result>
  20. <result name="stockQuerySomeSuccess">/WEB-INF/jsp/product/someProduct.jsp</result>
  21. </action>
  22. </package>
  23. </struts>
三、添加hibernate環境
jar包,配置文件,hibernate.hbm.xml 映射文件和    hibernate.cfg.xml主文件
 
  這個必須加,不加報錯。
c3p0數據源
 3.1添加過jar包後,拷貝配置文件和映射文件
 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  4. <hibernate-configuration>
  5. <session-factory>
  6. <!-- 1.數據庫鏈接信息 <property name="connection.url">jdbc:mysql:///itcastoa0720</property>
  7. <property name="connection.driver_class">com.jdbc.mysql.Driver</property>
  8. <property name="connection.username">root</property> <property name="connection.password"></property> -->
  9. <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
  10. <!-- 2.其餘配置 -->
  11. <property name="hbm2ddl.auto">update</property>
  12. <!-- 3.映射文件 -->
  13. <property name="show_sql">true</property>
  14. <property name="format_sql">true</property>
  15. <!-- 測試 -->
  16. <!-- <mapping resource="com/future/test/User.hbm.xml" /> -->
  17. <mapping resource="com/future/domain/User.hbm.xml" />
  18. </session-factory>
  19. </hibernate-configuration>
  1. 只是一個例子,具體內容之後修改。
  2. <?xml version="1.0"?>
  3. <!DOCTYPE hibernate-mapping PUBLIC
  4. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  5. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  6. <hibernate-mapping package="com.future.domain">
  7. <class name="User" table="t_user">
  8. <id name="id" type="int">
  9. <column name="I_ID" />
  10. <generator class="native" />
  11. </id>
  12. <property name="name" type="java.lang.String" unique="false">
  13. <column name="S_NAME" />
  14. </property>
  15. <property name="loginName" type="java.lang.String">
  16. <column name="S_LOGINNAME" />
  17. </property>
  18. <property name="password" type="java.lang.String">
  19. <column name="S_PASSWORD" />
  20. </property>
  21. <property name="phoneNum" type="java.lang.String">
  22. <column name="S_PHONENUM" />
  23. </property>
  24. <!--
  25. repairHistorys:本類和repairHistory一對多
  26. -->
  27. <set name="repairHistorys">
  28. <key column="repairManId"></key>
  29. <one-to-many class="RepairHistory" />
  30. </set>
  31. </class>
  32. </hibernate-mapping>
四、添加spring環境 5個
核心包
依賴AOP
 子類代理
日誌
以後考入配置文件:(注:這個配置文件必須本身手動建立,而後複製別人的內容)
 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:tx="http://www.springframework.org/schema/tx"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  7. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  8. <!--自動掃描與裝配bean 包含子包 -->
  9. <context:component-scan base-package="com.future"></context:component-scan>
  10. <!-- 導入外部得properties -->
  11. <context:property-placeholder location="classpath:jdbc.properties"/>
  12. </beans>
至此,jar包添加完畢
五、struts2與spring整合
寫測試類
  1. package cn.itcast.test;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. public class TestAction extends ActionSupport{
  4. public String execute() throws Exception{
  5. System.out.println("==>>testAction execute!!");
  6. return "success";
  7. }
  8. }
5.1以後配置測試頁
 
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. struts2 添加成功!
  11. </body>
  12. </html>
struts2配置文件:
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5. <struts>
  6. <!-- 更改struts默認的主題 -->
  7. <constant name="struts.ui.theme" value="simple"/>
  8. <!-- 配置爲開發模式 ,好處是修改過以後當即生效-->
  9. <constant name="struts.devMode" value="true"></constant>
  10. <!--聲明包 -->
  11. <package name="defult" extends="struts-default">
  12. <!-- 配置測試用Action,爲與spring整合,因此寫全類名 -->
  13. <action name="test" class="cn.itcast.test.TestAction">
  14. <result name="success">Test.jsp</result>
  15. </action>
  16. <!-- <action name="product_*" class="com.atfuture.PM.action.ProductAction" method="{1}">
  17. <result name="list">/WEB-INF/jsp/product/product_list.jsp</result>
  18. <result name="success" type="redirect">/product_list</result>
  19. <result name="input">/WEB-INF/jsp/product/product_input.jsp</result>
  20. <result name="productStockAll">/WEB-INF/jsp/product/product_stock.jsp</result>
  21. <result name="cg_input">/WEB-INF/jsp/product/product_input.jsp</result>
  22. <result name="fintByCategory">/WEB-INF/jsp/product/product_cg_list.jsp</result>
  23. <result name="addinput">/WEB-INF/jsp/product/product_addinput.jsp</result>
  24. <result name="stockQuerySomeSuccess">/WEB-INF/jsp/product/someProduct.jsp</result>
  25. </action> -->
  26. </package>
  27. </struts>
 
若是報錯:在web.xml中添加
  1. <!-- spring用於初始化容器對象得監聽器配置監聽器 -->
  2. <context-param>
  3. <param-name>contextConfigLocation</param-name>
  4. <param-value>classpath:applicationContext.xml</param-value>
  5. </context-param>
  6. <listener>
  7. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  8. </listener>
 連個JSP頁面放在WebContext下。
若是頁面出現:
 而且訪問test,出現
 表明struts2配置成功,
(整合與爲整合主要看struts配置文件)
 
接下來測試spring
 
  1. package cn.itcast.test;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class SpringTest {
  6. private ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
  7. @Test
  8. public void TestSpring(){
  9. TestAction testAction = (TestAction) ctx.getBean("testAction");
  10. System.out.println(testAction);
  11. }
  12. }
不成功,由於尚未配置;
在這出現註解
 如今TestAction中添加註解
 spring掃描到這個Bean,
在applicationContext中添加自動掃描
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:tx="http://www.springframework.org/schema/tx"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  7. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  8. <!--自動掃描與裝配bean 包含子包 -->
  9. <context:component-scan base-package="cn.itcast.test"></context:component-scan>
  10. <!-- 導入外部得properties -->
  11. <!-- <context:property-placeholder location="classpath:jdbc.properties"/> -->
  12. </beans>
輸入
spring獨立運行~~~
5.2接下來整合spring
5.2.1 添加spring監聽(由於上面解決錯誤添加,如今就不用添加了)
 5.2.2添加整合jar包
 
5.2.3測試
 在測試頁中添加一句話
 顯示
 整合成功、
 
六、Hibernate與spring整合
6.1首先
在spring配置文件中 配置sessionFactory,
applicationContext文件,配置以前寫屬性文件
 
  1. jdbcUrl=jdbc:mysql:///carmanage?useUnicode=true&characterEncoding=utf8
  2. driverClass=com.mysql.jdbc.Driver
  3. user=root
  4. password=root
 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:tx="http://www.springframework.org/schema/tx"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  7. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  8. <!--自動掃描與裝配bean 包含子包 -->
  9. <context:component-scan base-package="cn.itcast.test"></context:component-scan>
  10. <!-- 導入外部得properties -->
  11. <context:property-placeholder location="classpath:jdbc.properties"/>
  12. <!--配置sessionFactory -->
  13. <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  14. <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
  15. <property name="dataSource">
  16. <bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
  17. <!-- 數據鏈接信息 -->
  18. <property name="jdbcUrl" value="${jdbcUrl}"></property>
  19. <property name="driverClass" value="${driverClass}"></property>
  20. <property name="user" value="${user}"></property>
  21. <property name="password" value="${password}"></property>
  22. <!-- 其餘配置 -->
  23. <!--初始化時獲取三個鏈接,取值應在minPoolSizemaxPoolSize之間。Default: 3 -->
  24. <property name="initialPoolSize" value="3"></property>
  25. <!--鏈接池中保留的最小鏈接數。Default: 3 -->
  26. <property name="minPoolSize" value="3"></property>
  27. <!--鏈接池中保留的最大鏈接數。Default: 15 -->
  28. <property name="maxPoolSize" value="5"></property>
  29. <!--當鏈接池中的鏈接耗盡的時候c3p0一次同時獲取的鏈接數。Default: 3 -->
  30. <property name="acquireIncrement" value="3"></property>
  31. <!-- 控制數據源內加載的PreparedStatements數量。若是maxStatementsmaxStatementsPerConnection均爲0,則緩存被關閉。Default: 0 -->
  32. <property name="maxStatements" value="8"></property>
  33. <!--maxStatementsPerConnection定義了鏈接池內單個鏈接所擁有的最大緩存statements數。Default: 0 -->
  34. <property name="maxStatementsPerConnection" value="5"></property>
  35. <!--最大空閒時間,1800秒內未使用則鏈接被丟棄。若爲0則永不丟棄。Default: 0 -->
  36. <property name="maxIdleTime" value="1800"></property>
  37. </bean>
  38. </property>
  39. </bean>
  40. </beans>
在springTest測試文件中測試sessionFactory
 
  1. package cn.itcast.test;
  2. import org.hibernate.SessionFactory;
  3. import org.junit.Test;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. public class SpringTest {
  7. private ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
  8. @Test
  9. public void TestSpring(){
  10. TestAction testAction = (TestAction) ctx.getBean("testAction");
  11. System.out.println(testAction);
  12. }
  13. @Test
  14. public void TestSessionFactory(){
  15. SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");
  16. System.out.println(sessionFactory);
  17. }
  18. }
在這會出現一個錯誤,由於找不到hibernate得映射文件,在是以前複製得時候多複製了,如今把它註釋掉
 在運行,成功~~
 6.2配置事務管理,在spring得配置文件applicationContext中配置。
  1. <!-- 配置聲明式事物 -->
  2. <bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  3. <property name="sessionFactory" ref="sessionFactory"></property>
  4. </bean>
  5. <!-- 配置處理事務的註釋 -->
  6. <tx:annotation-driven transaction-manager="transactionManager"/>
6.3配置測試用的javaBean
 
  1. package cn.itcast.domain;
  2. public class User {
  3. private Long id;
  4. private String name;
  5. public Long getId() {
  6. return id;
  7. }
  8. public void setId(Long id) {
  9. this.id = id;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. }
6.4配置映射文件
 
  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping package="cn.itcast.domain">
  6. <class name="User" table="test_user">
  7. <id name="id" type="int">
  8. <column name="I_ID" />
  9. <generator class="native" />
  10. </id>
  11. <property name="name" type="java.lang.String" >
  12. <column name="S_NAME" />
  13. </property>
  14. </class>
  15. </hibernate-mapping>
在hibernate得配置文件中增長mapping
 6.5準備測試。
6.5.1首先創建server層
 
  1. package cn.itcast.test;
  2. import javax.annotation.Resource;
  3. import org.hibernate.Session;
  4. import org.hibernate.SessionFactory;
  5. import org.springframework.stereotype.Service;
  6. import org.springframework.transaction.annotation.Transactional;
  7. import cn.itcast.domain.User;
  8. @Service("testService")
  9. public class TestService {
  10. @Resource
  11. private SessionFactory sessionFactory;
  12. @Transactional
  13. public void saveTwoUse(){
  14. Session session = sessionFactory.getCurrentSession();
  15. session.save(new User());
  16. int a=1/0;
  17. session.save(new User());
  18. }
  19. }
 接下來測試事務,在springTest中添加
  1. @Test
  2. public void TestTransaction(){
  3. TestService testService = (TestService) ctx.getBean("testService");
  4. testService.saveTwoUse();
  5. }
提示,
 表明事務成功。
註釋掉:
 
提示:
最後:完整測試
修改TestAction文件
 修改Test.jsp
 ,運行訪問
提示
 
 
徹底整合完成~
七、資源分類
7.1測試和主要內容分開,而且把配置文件放到一塊兒
 7.2關於建包
 7.3關於資源
八、配置日誌
 日誌jar包
 最後,關於日誌在視頻中找~~~。
相關文章
相關標籤/搜索