zoukankan      html  css  js  c++  java
  • SSH 架构

    这几天学习了 ssh 架构,中间出了好多错误,现在终于整理好了,就记录下来

    ssh机构的框架构成,以及它们的作用

        struts2 :这个框架主要用做控制处理的,其核心是 Contraller ,即 ActionServlet。而 ActionServlet 的核心是 struts.xml。用于分配处理web请求需要执行哪个业务逻辑。

        spring :spring 可以理解为粘合剂的作用。它把这三个框架糅合在一起使用。利用本身的控制反转(IOC) 来达到解耦的目的。

        Hibernate:及时数据持久层。只要用来操作数据库。它通过把关系型数据的 数据结构映射编程面向对象的概念。让程序员不用变换思维就离异实现对数据库的操作。

    这次实例使用的各个框架的版本。

       struts-2.3.15.3

       spring-3.2.0

       hibernate-3.6.10

    使用的 jar 包

     1、struts2 使用的 jar 包

            1)  struts2 → apps → struts-blank 实例空项目 → web-INF → lib 下的所有 jar 包。(从官网在的struts2 里面有这个 struts-blank 实例空项目)

         

            2)struts2-convention-plugin-2.3.15.3.jar    struts2 注解开发用的 jar 包。

            3)struts2-spring-plugin-2.3.15.3.jar   struts2用于整合 spring 的 jar 包。

     2、Hibernate 使用的 jar 包

           1) Hibernate3.jar     Hibernate 根目录下的 

           2)  hibernate → lib → required 目录下的所有 jar 包。

               

          3)hibernate-entitymanager-2.0.Final.jar    hibernate → lib → required 目录下

          4)日志记录用的包 slf4j-log4j12.1.7.5.jar

          5) mysql-connector-java-5.1.25-bin.jar  数据库的驱动包

     3、spring 使用的 jar 包   

         1) IOC 的开发用到的包

                  spring-beans-3.2.5.RELEASE.jar

            spring-context-3.2.5.RELEASE.jar

         spring-core-3.2.5.RELEASE.jar

         spring-expression-3.2.5.RELEASE.jar

            com.springsource.org.apache.log4j-1.2.15.jar

            com.springsource.org.apache.commons.logging-1.1.1.jar  (不进行具体的日志记录,整合其他的日志系统的)

        2)AOP 的开发

                 spring-aop-3.2.5.RELEASE.jar

         spring-aspects-3.2.5.RELEASE.jar

         com.springsource.org.aopalliance-1.0.0.jar (AOP 联盟的包)

            com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar 

            spring-jdbc-3.2.5.RELEASE.jar

            spring-test-3.2.5.RELEASE.jar

         spring-web-3.2.5.RELEASE.jar

       spring-orm-3.2.5.RELEASE.jar

            spring-tx-3.2.5.RELEASE.jar

     引入相应的配置文件

      1、struts2 框架的配置文件

             web.xml (关于struts2 的配置)

    <!-- 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>
        </filter-mapping>
    View Code

         struts.xml

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!-- /* * Licensed to the Apache Software Foundation (ASF) under one * or 
     3     more contributor license agreements. See the NOTICE file * distributed with 
     4     this work for additional information * regarding copyright ownership. The 
     5     ASF licenses this file * to you under the Apache License, Version 2.0 (the 
     6     * "License"); you may not use this file except in compliance * with the License. 
     7     You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 
     8     * * Unless required by applicable law or agreed to in writing, * software 
     9     distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT 
    10     WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the 
    11     License for the * specific language governing permissions and limitations 
    12     * under the License. */ -->
    13 <!DOCTYPE struts PUBLIC
    14         "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    15         "http://struts.apache.org/dtds/struts-2.3.dtd">
    16 
    17 
    18 <struts> 
    19     
    20 
    21     
    22 </struts>
    View Code

       2、Hibernate 配置文件

             hibernate.cfg.xml (在整理中这个配置文件时可以省略的,相应的内容写到 spring 配置文件中了)

             映射文件(*.bhm.xml)

     3、spring 配置文件

             web.xml (关于spring的配置)

    1 <!-- spring 的核心监听器 -->
    2     <listener>
    3         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    4     </listener>
    5 
    6     <context-param>
    7         <param-name>contextConfigLocation</param-name>
    8         <param-value>classpath:applicationContext.xml</param-value>
    9     </context-param>
    View Code

        applicationContext.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 
     3 <beans xmlns="http://www.springframework.org/schema/beans"
     4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
     5     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
     6     xsi:schemaLocation="
     7         http://www.springframework.org/schema/beans
     8         http://www.springframework.org/schema/beans/spring-beans.xsd
     9         http://www.springframework.org/schema/aop
    10         http://www.springframework.org/schema/aop/spring-aop.xsd
    11         http://www.springframework.org/schema/tx
    12         http://www.springframework.org/schema/tx/spring-tx.xsd
    13         http://www.springframework.org/schema/context 
    14         http://www.springframework.org/schema/context/spring-context.xsd ">
    15 
    16     <!-- 引入外部文件 -->
    17     <context:property-placeholder location="classpath:jdbc.properties" />
    18     <!-- 设置数据库链接池 c3p0 -->
    19     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    20         <property name="driverClass" value="${jdbc.driverClass}"></property>
    21         <property name="jdbcUrl" value="${jdbc.url}"></property>
    22         <property name="user" value="${jdbc.username}"></property>
    23         <property name="password" value="${jdbc.password}"></property>
    24         <!-- <property name="driverClass" value="com.mysql.jdbc.Driver"></property> -->
    25         <!-- <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/testdb"></property> -->
    26         <!-- <property name="user" value="root"></property> -->
    27         <!-- <property name="password" value="123456"></property> -->
    28     </bean>
    29 
    30     <!-- 配置hibernate的相关属性 -->
    31     <bean id="sessionFactory"
    32         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    33         <!-- 注入連接池 -->
    34         <property name="dataSource" ref="dataSource"></property>
    35         <!-- 配置 Hibernate 属性 -->
    36         <property name="hibernateProperties">
    37             <props>
    38                 <!-- mysql 方言 -->
    39                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    40                 <!-- 是否打印 sql 语句 -->
    41                 <prop key="hibernate.show_sql">true</prop>
    42                 <!-- sql 语句是否格式话,格式化就是能够方便阅读的格式 -->
    43                 <prop key="hibernate.format_sql">true</prop>
    44                 <!-- 是否自动更新表结构 -->
    45                 <prop key="hibernate.hbm2ddl.auto">update</prop>
    46             </props>
    47         </property>
    48         <!-- 加载 Hibernate 映射属性 -->
    49         <property name="mappingResources">
    50             <list>
    51                 <value>cn/myssh/entity/Product.hbm.xml</value>
    52             </list>
    53         </property>
    54     </bean>
    55 
    56     <bean id="productAction" class="cn.myssh.action.ProductAction">
    57         <property name="productService" ref="productService"></property>
    58     </bean>
    59 
    60     <bean id="productService" class="cn.myssh.service.ProductService">
    61         <property name="productDao" ref="productDao"></property>
    62     </bean>
    63 
    64     <bean id="productDao" class="cn.myssh.dao.ProductDao">
    65         <!-- 因为 ProductDao 继承了HibernateDaoSupport,HibernateDaoSupport 里面有 setSessionFactory 
    66             方法, -->
    67         <!-- 所以可以直接注入,HibernateDaoSupport 里面还有创建 HibernateTemplate 方法 -->
    68         <property name="sessionFactory" ref="sessionFactory"></property>
    69     </bean>
    70 
    71     <!-- 事物管理配置器 -->
    72     <bean id="transactionManager"
    73         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    74         <property name="sessionFactory" ref="sessionFactory"></property>
    75     </bean>
    76 
    77     <!-- 开启注解事物 -->                       
    78     <tx:annotation-driven transaction-manager="transactionManager"/>
    79 </beans>
    applicationContext

     建造包结构及实体

     1 package cn.myssh.entity;
     2 
     3 /**
     4  * 商品管理的实体类
     5  * @author Administrator
     6  *
     7  */
     8 public class Product implements java.io.Serializable{
     9     private int pid;
    10     private String pname;
    11     private double price;
    12     public int getPid() {
    13         return pid;
    14     }
    15     public void setPid(int pid) {
    16         this.pid = pid;
    17     }
    18     public String getPname() {
    19         return pname;
    20     }
    21     public void setPname(String pname) {
    22         this.pname = pname;
    23     }
    24     public double getPrice() {
    25         return price;
    26     }
    27     public void setPrice(double price) {
    28         this.price = price;
    29     }
    30 }
    实体类 product 对象

    struts2 整合 spring   保存添加的商品为例

    1、添加页面例子

     1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
     2 <%
     3     String path = request.getContextPath();
     4     String basePath = request.getScheme() + "://"
     5             + request.getServerName() + ":" + request.getServerPort()
     6             + path + "/";
     7 %>
     8 <%@taglib uri="/struts-tags" prefix="s"%>
     9 
    10 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    11 <html>
    12 <head>
    13 <base href="<%=basePath%>">
    14 
    15 <title>My JSP 'addProduct.jsp' starting page</title>
    16 
    17 <meta http-equiv="pragma" content="no-cache">
    18 <meta http-equiv="cache-control" content="no-cache">
    19 <meta http-equiv="expires" content="0">
    20 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    21 <meta http-equiv="description" content="This is my page">
    22 <!--
    23     <link rel="stylesheet" type="text/css" href="styles.css">
    24     -->
    25 
    26 </head>
    27 
    28 <body>
    29     <h1>保存商品</h1>
    30     <s:form action="product_save" method="post" namespace="/" theme="simple">
    31         <table border="1" width="500px">
    32             <tr>
    33                 <td>商品名称</td>
    34                 <td><s:textfield name="pname"></s:textfield></td>
    35             </tr>
    36             <tr>
    37                 <td>商品价格</td>
    38                 <td><s:textfield name="price"></s:textfield></td>
    39             </tr>
    40             <tr>
    41                 <td colspan="2"><s:submit value="tijiao"></s:submit></td>
    42             </tr>
    43         </table>
    44     </s:form>
    45 </body>
    46 </html>
    addProduct.jsp

    2、后台逻辑条用的过程

      addProduct.jsp 发出请求 → 在struts.xml 中找到对应的 Action 执行(productAction     save) → 通过注入的 ProductService 方法找到 对应的 逻辑处理 → 通过注入到 ProductService 里面的对象找到对应的的 productDao执行 → 更新到数据库里面。

    3、关于 productAction 类

      1) ProductService 注入到 productAction 里面。

            1 在以前注入的时候都需要借用 beans 工具类,就是在 applicationContext 配置,然后写一个类来获取折耳根配置。

       2 现在有了 struts2-spring-plugin-2.3.20.jar(整理struts2和plugin)jar 包。可以自动装配了。

                 在 jar 包中 有个 struts-plugin.xml 文件,中间看了    <constant name="struts.objectFactory" value="spring" />  这是一个 struts2 的常量。在 struts-core 包中 有个 default.properties。里面有常量的介绍。struts.objectFactory = spring 这句话原本是被注释的,但是  <constant name="struts.objectFactory" value="spring" />  给打开了。就引发了 struts.objectFactory.spring.autoWire = name 即 是自动装配

    1 <!-- 在 struts.xml 里面配置 -->
    2     <package name="myssh" extends="struts-default" namespace="/">
    3     <!-- productAction 之所以直接用 productAction 这个是因为在 applicationContext.xml 进行了配置 -->
    4         <action name="product_*" class="productAction"
    5             method="{1}">
    6             <result name="none">index.jsp</result>
    7         </action>
    8 
    9     </package>
     1 package cn.myssh.action;
     2 
     3 import com.opensymphony.xwork2.ActionSupport;
     4 import com.opensymphony.xwork2.ModelDriven;
     5 
     6 import cn.myssh.entity.Product;
     7 import cn.myssh.service.ProductService;
     8 
     9 /**
    10  * 商品管理的 action 类
    11  * @author Administrator
    12  * 
    13  */
    14 public class ProductAction extends ActionSupport implements ModelDriven<Product> {
    15     /**
    16      * 模型驱动使用的类
    17      */
    18     private Product product = new Product();
    19     
    20     public Product getModel()
    21     {
    22         return product;
    23     }
    24     //spring 和 struts2 整合过程中,按照名称自动注入业务层类
    25     private ProductService productService;
    26 
    27     public void setProductService(ProductService productService) {
    28         this.productService = productService;
    29     }
    30 
    31     /**
    32      * 保存商品的方法
    33      */
    34     public String save()
    35     {
    36         System.out.println("save the product");
    37         productService.save(product);
    38         return "none";
    39     }
    40 }
    productAction

     4、在 applicationContext.xml 配置 Action、service、dao 。(见上面贴出的 applicationContext.xml )

    5、添加保存方法。

    1 /**
    2      * 保存商品的方法
    3      */
    4     public String save()
    5     {
    6         System.out.println("save the product");
    7         productService.save(product);
    8         return "none";
    9     }
    ProductAction 里面的 save 方法
    1 public void save(Product product)
    2     {
    3         productDao.save(product);
    4         System.out.println("service 中的方法执行了");
    5     }
    ProductService 里面 save 方法
    1 public void save(Product product) {
    2         System.out.println("dao 的save 方法");
    3     }
    productDao 里面的 save 方法

    到这儿,执行 addProduct 方法,后台的过程就是框架整合后的过程,只是还没有更新到数据库里面

    整合 spring 和 Hibernate

    1、引入 jdbc.properties ,目录在src 下。

    1 jdbc.driverClass=com.mysql.jdbc.Driver
    2 jdbc.url=jdbc:mysql://localhost:3306/testdb
    3 jdbc.username=root
    4 jdbc.password=123456

    2、配置映射文件,这个文件需要和映射的提示文件放在一个包下。

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 
     3 <!DOCTYPE hibernate-mapping PUBLIC 
     4     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     5     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     6 
     7 <hibernate-mapping>
     8     <class name="cn.myssh.entity.Product" table="product">
     9         <id name="pid" column="pid">
    10             <generator class="native"></generator>
    11         </id>
    12 
    13         <property name="pname" column="pname" />
    14         <property name="price" column="price" />
    15     </class>
    16 </hibernate-mapping>
    Product.hbm.xml

    3、applicationContext.xml 获取  jdbc.properties 配置数据库链接 用的是 c3p0 的连接方式。

     1 <!-- 引入外部文件 -->
     2     <context:property-placeholder location="classpath:jdbc.properties" />
     3     <!-- 设置数据库链接池 c3p0 -->
     4     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
     5         <property name="driverClass" value="${jdbc.driverClass}"></property>
     6         <property name="jdbcUrl" value="${jdbc.url}"></property>
     7         <property name="user" value="${jdbc.username}"></property>
     8         <property name="password" value="${jdbc.password}"></property>
     9         <!-- <property name="driverClass" value="com.mysql.jdbc.Driver"></property> -->
    10         <!-- <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/testdb"></property> -->
    11         <!-- <property name="user" value="root"></property> -->
    12         <!-- <property name="password" value="123456"></property> -->
    13     </bean>
    14 
    15     <!-- 配置hibernate的相关属性 -->
    16     <bean id="sessionFactory"
    17         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    18         <!-- 注入連接池 -->
    19         <property name="dataSource" ref="dataSource"></property>
    20         <!-- 配置 Hibernate 属性 -->
    21         <property name="hibernateProperties">
    22             <props>
    23                 <!-- mysql 方言 -->
    24                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    25                 <!-- 是否打印 sql 语句 -->
    26                 <prop key="hibernate.show_sql">true</prop>
    27                 <!-- sql 语句是否格式话,格式化就是能够方便阅读的格式 -->
    28                 <prop key="hibernate.format_sql">true</prop>
    29                 <!-- 是否自动更新表结构 -->
    30                 <prop key="hibernate.hbm2ddl.auto">update</prop>
    31             </props>
    32         </property>
    33         <!-- 加载 Hibernate 映射属性 -->
    34         <property name="mappingResources">
    35             <list>
    36                 <value>cn/myssh/entity/Product.hbm.xml</value>
    37             </list>
    38         </property>
    39     </bean>
    applicationContext.xml 的 Hibernate 相关配置部分

    4、dao 方法继承 ActionSupport 实现接口 ModelDriven<T>

     1 package cn.myssh.action;
     2 
     3 import com.opensymphony.xwork2.ActionSupport;
     4 import com.opensymphony.xwork2.ModelDriven;
     5 
     6 import cn.myssh.entity.Product;
     7 import cn.myssh.service.ProductService;
     8 
     9 /**
    10  * 商品管理的 action 类
    11  * @author Administrator
    12  * 
    13  */
    14 public class ProductAction extends ActionSupport implements ModelDriven<Product> {
    15     /**
    16      * 模型驱动使用的类
    17      */
    18     private Product product = new Product();
    19     
    20     public Product getModel()
    21     {
    22         return product;
    23     }
    24     //spring 和 struts2 整合过程中,按照名称自动注入业务层类
    25     private ProductService productService;
    26 
    27     public void setProductService(ProductService productService) {
    28         this.productService = productService;
    29     }
    30 
    31     /**
    32      * 保存商品的方法
    33      */
    34     public String save()
    35     {
    36         System.out.println("save the product");
    37         productService.save(product);
    38         return "none";
    39     }
    40 }
    productDao

    5、创建数据库 testdb,配置了

    1 <!-- 是否自动更新表结构 -->
    2                 <prop key="hibernate.hbm2ddl.auto">update</prop>

           运行项目 会自动创建 / 更新表结构,

    结束感悟

    1、原来三个框架都是最新版本,有些包总是找不到,代码检查没有错,还是执行不了。怀疑是 版本兼容问题。

    2、在配置过程路径或者文件名字,能复制的尽量复制。手桥极容易敲错,但是配置不顺利。比如我桥 classpath 就写成了 classpash 废了好大劲儿才检查出来。

    3、配置中能用自动提醒的,就用自动提醒。

    4、配置方法有注解和xml两种。

  • 相关阅读:
    java攻城狮之路(Android篇)--widget_webview_metadata_popupwindow_tabhost_分页加载数据_菜单
    java攻城狮之路(Android篇)--MP3 MP4、拍照、国际化、样式主题、图片移动和缩放
    java攻城狮之路(Android篇)--BroadcastReceiver&Service
    内存调试的东西D/dalvikvm( 809 ): GC_CONCURRENT freed
    java攻城狮之路(Android篇)--Activity生命
    java攻城狮之路(Android篇)--与服务器交互
    java实例练习
    java攻城狮之路(Android篇)--ListView与ContentProvider
    java攻城狮之路(Android篇)--SQLite
    Android中使用自身携带的Junit新建一个测试工程
  • 原文地址:https://www.cnblogs.com/pengweiqiang/p/8973220.html
Copyright © 2011-2022 走看看