SSH 架构

时间:2023-03-08 22:46:23

这几天学习了 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 实例空项目)

SSH 架构

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 包。

SSH 架构

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>

   struts.xml

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

2、Hibernate 配置文件

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

映射文件(*.bhm.xml)

3、spring 配置文件

web.xml (关于spring的配置)

 <!-- 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>

   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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd "> <!-- 引入外部文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 设置数据库链接池 c3p0 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<!-- <property name="driverClass" value="com.mysql.jdbc.Driver"></property> -->
<!-- <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/testdb"></property> -->
<!-- <property name="user" value="root"></property> -->
<!-- <property name="password" value="123456"></property> -->
</bean> <!-- 配置hibernate的相关属性 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入連接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置 Hibernate 属性 -->
<property name="hibernateProperties">
<props>
<!-- mysql 方言 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!-- 是否打印 sql 语句 -->
<prop key="hibernate.show_sql">true</prop>
<!-- sql 语句是否格式话,格式化就是能够方便阅读的格式 -->
<prop key="hibernate.format_sql">true</prop>
<!-- 是否自动更新表结构 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 加载 Hibernate 映射属性 -->
<property name="mappingResources">
<list>
<value>cn/myssh/entity/Product.hbm.xml</value>
</list>
</property>
</bean> <bean id="productAction" class="cn.myssh.action.ProductAction">
<property name="productService" ref="productService"></property>
</bean> <bean id="productService" class="cn.myssh.service.ProductService">
<property name="productDao" ref="productDao"></property>
</bean> <bean id="productDao" class="cn.myssh.dao.ProductDao">
<!-- 因为 ProductDao 继承了HibernateDaoSupport,HibernateDaoSupport 里面有 setSessionFactory
方法, -->
<!-- 所以可以直接注入,HibernateDaoSupport 里面还有创建 HibernateTemplate 方法 -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!-- 事物管理配置器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!-- 开启注解事物 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

applicationContext

建造包结构及实体

SSH 架构

 package cn.myssh.entity;

 /**
* 商品管理的实体类
* @author Administrator
*
*/
public class Product implements java.io.Serializable{
private int pid;
private String pname;
private double price;
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}

实体类 product 对象

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

1、添加页面例子

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%@taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'addProduct.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<h1>保存商品</h1>
<s:form action="product_save" method="post" namespace="/" theme="simple">
<table border="1" width="500px">
<tr>
<td>商品名称</td>
<td><s:textfield name="pname"></s:textfield></td>
</tr>
<tr>
<td>商品价格</td>
<td><s:textfield name="price"></s:textfield></td>
</tr>
<tr>
<td colspan="2"><s:submit value="tijiao"></s:submit></td>
</tr>
</table>
</s:form>
</body>
</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 即 是自动装配

 <!-- 在 struts.xml 里面配置 -->
<package name="myssh" extends="struts-default" namespace="/">
<!-- productAction 之所以直接用 productAction 这个是因为在 applicationContext.xml 进行了配置 -->
<action name="product_*" class="productAction"
method="{1}">
<result name="none">index.jsp</result>
</action> </package>
 package cn.myssh.action;

 import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; import cn.myssh.entity.Product;
import cn.myssh.service.ProductService; /**
* 商品管理的 action 类
* @author Administrator
*
*/
public class ProductAction extends ActionSupport implements ModelDriven<Product> {
/**
* 模型驱动使用的类
*/
private Product product = new Product(); public Product getModel()
{
return product;
}
//spring 和 struts2 整合过程中,按照名称自动注入业务层类
private ProductService productService; public void setProductService(ProductService productService) {
this.productService = productService;
} /**
* 保存商品的方法
*/
public String save()
{
System.out.println("save the product");
productService.save(product);
return "none";
}
}

productAction

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

5、添加保存方法。

 /**
* 保存商品的方法
*/
public String save()
{
System.out.println("save the product");
productService.save(product);
return "none";
}

ProductAction 里面的 save 方法

 public void save(Product product)
{
productDao.save(product);
System.out.println("service 中的方法执行了");
}

ProductService 里面 save 方法

 public void save(Product product) {
System.out.println("dao 的save 方法");
}

productDao 里面的 save 方法

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

整合 spring 和 Hibernate

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

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

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

 <?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"> <hibernate-mapping>
<class name="cn.myssh.entity.Product" table="product">
<id name="pid" column="pid">
<generator class="native"></generator>
</id> <property name="pname" column="pname" />
<property name="price" column="price" />
</class>
</hibernate-mapping>

Product.hbm.xml

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

 <!-- 引入外部文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 设置数据库链接池 c3p0 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<!-- <property name="driverClass" value="com.mysql.jdbc.Driver"></property> -->
<!-- <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/testdb"></property> -->
<!-- <property name="user" value="root"></property> -->
<!-- <property name="password" value="123456"></property> -->
</bean> <!-- 配置hibernate的相关属性 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入連接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置 Hibernate 属性 -->
<property name="hibernateProperties">
<props>
<!-- mysql 方言 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!-- 是否打印 sql 语句 -->
<prop key="hibernate.show_sql">true</prop>
<!-- sql 语句是否格式话,格式化就是能够方便阅读的格式 -->
<prop key="hibernate.format_sql">true</prop>
<!-- 是否自动更新表结构 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 加载 Hibernate 映射属性 -->
<property name="mappingResources">
<list>
<value>cn/myssh/entity/Product.hbm.xml</value>
</list>
</property>
</bean>

applicationContext.xml 的 Hibernate 相关配置部分

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

 package cn.myssh.action;

 import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; import cn.myssh.entity.Product;
import cn.myssh.service.ProductService; /**
* 商品管理的 action 类
* @author Administrator
*
*/
public class ProductAction extends ActionSupport implements ModelDriven<Product> {
/**
* 模型驱动使用的类
*/
private Product product = new Product(); public Product getModel()
{
return product;
}
//spring 和 struts2 整合过程中,按照名称自动注入业务层类
private ProductService productService; public void setProductService(ProductService productService) {
this.productService = productService;
} /**
* 保存商品的方法
*/
public String save()
{
System.out.println("save the product");
productService.save(product);
return "none";
}
}

productDao

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

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

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

结束感悟

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

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

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

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