簡化SSH框架的整合

1、開發環境:html

(1)    OS:Windows 7java

(2)    DB:MySql 5.1.6mysql

(3)    JDK:1.8.0_17web

(4)    Server:Apache Tomcat 8.0.26spring

(5)    IDE:Eclipsesql

 

2、SSH框架版本:apache

(1)    Struts2.3.2session

(2)    Spring4.0.2app

(3)    Hibernate5.2.5框架

注意:我根據須要並無導入全部JAR 包;

 

3、配置Sturts2框架:

1.在eclipse新建一個項目news;

2.導入Sturts2.X的jar包;

 

3.而後配置在WebContext/WEB-INF/web.xml:

 1   <display-name>news</display-name>
 2   <welcome-file-list>
 3     <welcome-file>default.jsp</welcome-file>
 4   </welcome-file-list>
 5   <filter>
 6     <filter-name>struts2</filter-name>
 7     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 8   </filter>
 9   <filter-mapping>
10     <filter-name>struts2</filter-name>
11     <url-pattern>/*</url-pattern>
12   </filter-mapping>

注意:default.jsp表明一個默認首頁;

注意:/*表明過濾全部;

注意:struts2的入口是一個過濾器,因此能夠把struts2當過濾器來配置;

4.而後配置src/struts.xml

<!-- 第1步:先定義一個包 -->
    <package name="mypck001" extends="struts-default">
        <!-- myNewsAction用於spring注入 -->
        <action name="NewsAction_*" class="myNewsAction" method="{1}">
            <result name="success">/WEB-INF/jsp/index.jsp</result>
            <result name="deleteOK">/WEB-INF/jsp/ok.jsp</result>
            <result name="deleteFailed">/WEB-INF/jsp/error_delete.jsp</result>
        </action>        
    </package>

注意:NewsAction_*表明全部以NewsAction_開頭的地址;

注意:{1}表明方法名跟NewsAction_*中*的值同樣;

5.而後建立4個jsp文件:default.jsp、Index.jsp、ok.jsp、error.jsp

default.jsp文件放在WEBContext目錄下,很方便的跳轉運行項目;

 
 

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
  <% response.sendRedirect("myNews2Action_getMyNewsAll.action"); %>
</body>
</html>

其餘三個,爲了防止用戶直接訪問JSP頁面把它們放在WEB-INF下;

6.而後再src目錄myNews2.action包下建立MyNews2Action.action;

 

4、配置spring框架:

1.添加spring jar包;

注意:spring框架須要經過struts2-spring-plugin-2.3.30 jar 以struts插件的形式鏈接struts;

2.而後配置web.xml

1   <context-param>
2     <param-name>contextConfigLocation</param-name>
3     <param-value>classpath:applicationContext.xml</param-value>
4   </context-param>
5   <listener>
6     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
7   </listener>

注意:spring以監聽器的形式監聽全部實例的建立;

注意:applicationcontext.xml是spring的配置文件,由於它的默認查找路徑在WEB—INF/classses下,而咱們要把它放到src下就在這配置一下;

3.而後在src目錄下建立jdbc.properties文件,目的是但願防止不應碰applicationContext.xml文件的人碰它;

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/news
jdbc.user=root
jdbc.password=123456

4.而後配置applicationContext.xml:

<context:component-scan base-package="myNews2" />

注意:它是spring註解解析器,用註解能夠各類<Bean>的建立;

    <context:property-placeholder location="classpath:jdbc.properties" />

!引入jdbc.properties;

5.而後對各個類進行IOC的注入:

1 @Repository
2 @Scope("prototype")
3 public class MyNewsDaoImpl implements MyNewsDao {
4 
5     @Resource
6     private SessionFactory sf;

注意:@Repository表明這個類的實例交給spring來管理;

@Controller
@Scope("prototype")
public class MyNews2Action extends ActionSupport{
    
    @Resource
    private MyNewsService ms;

注意:@Scope("prototype")表明這個類的做用範圍;

@Service
@Scope("prototype")
public class MyNews2ServiceImpl implements MyNewsService {
    
    @Resource
    private MyNewsDao mn;

注意:模型層用@Service,控制層用@Controller,數據層用@Repository;

注意:@Resource是JDK 解析器;spring的是@Autowired和Qualifier;

 

5、配置Hibernate框架:

1.添加Hibernate jar包;

2.配置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: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-4.2.xsd  
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd  
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
    
    <context:component-scan base-package="myNews2" />

    <context:property-placeholder location="classpath:jdbc.properties" />

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
        scope="prototype">
        <property name="dataSource" ref="dataSource"></property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</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>
        <property name="mappingResources">
                <list>
                    <value>News.hbm.xml</value>
                </list>
        </property>
    </bean>
    
    <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}" />
            <!-- 每300秒檢查全部鏈接池中的空閒鏈接 -->
            <property name="idleConnectionTestPeriod" value="300"></property>
            <!-- 最大空閒時間,900秒內未使用則鏈接被丟棄。若爲0則永不丟棄 -->
            <property name="maxIdleTime" value="900"></property>
            <!-- 最大鏈接數 -->
            <property name="maxPoolSize" value="2"></property>
    </bean>

</beans>

3.配置News.hbm.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping xmlns="http://www.hibernate.org/xsd/hibernate-mapping">
     <class name="myNews2.entity.News" table="news">
         <id name="id" column="id">
            <generator class="native"/>         
         </id>
         <!-- 屬性對應字段  -->
        <property name="title" type="string" length="50" column="title" not-null="true"></property>
         <property name="content" type="text" length="50000" column="content" not-null="true"></property>
        <property name="begintime" type="date" column="begintime" not-null="true"></property>
        <property name="username" type="string" length="20" column="username" not-null="true"></property>
    </class>
</hibernate-mapping>

 

 測試MyNews2Action.action

 啓動Tomcat

測試URL:http://localhost:8080/myNews2/MyNews2Action.action

相關文章
相關標籤/搜索