SSH框架配置

一、Struts2(MVC)的搭建:
相关描述
Ⅰ.本质上相当于一个Servlet
Ⅱ.不需要手动获取传递参数 (只需要定义声明属性名和设置get、set的方法)、也不需要手动跳转(只需要struts.xml配置相关的路径)
Ⅲ.对项目的分包(例如:dao、service、entity等等),实现MVC模式的开发
Ⅳ.MVC: Action属于Model,而JSP是View页面的展示,其中过滤器起到了Controller的作用
实现流程
1.首先新建一个项目
2.把下载好的Struts-2.3.30解压后,找到struts-2.3.30\apps\ struts2-blank.war \WEB-INF\lib的jar包,复制到项目的WebContent\WEB-INF\lib目录下
在这里插入图片描述在这里插入图片描述在这里插入图片描述
3.打开WebContent\WEB-INF\lib\web.xml,定义过滤器,编辑如下

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

在src目录下新建一个类,必须继承ActionSupport父类才是Action类,编辑如下
在这里插入图片描述

  //默认方法,struts自动调用
    public class IndexAction extends ActionSupport {
        public String execute() {
            //默认返回值
            return "success";
        }
    }

4.在项目的src目录下新建一个struts.xml文件
在这里插入图片描述

<?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">
<!-- 上面的头,注意版本,从样例里复制过来 showcase.war\WEB-INF\src\java\struts.xml -->
<struts>
    <package name="mypck" extends="struts-default">
        <!—name对应的action请求名称,method(不写等于默认方法)默认执行方法是 execute,class定义具体的类-->
        <action name="Index" class="ssh.action.IndexAction" method="execute">
            <!-- 接收Action类返回的值,进行跳转指定的页面 -->
            <result name="success">/WEB-INF/jsp/index.jsp</result>
            <result name="error">/WEB-INF/jsp/error.jsp</result>
        </action>
    </package>
</struts>

完成以上流程,基本上Struts2的搭建就ok了
二、Spring(注入实例)的使用
相关描述
Ⅰ.每一层的代码之间的耦合改为模块(分离/解耦),代码之间互不影响
Ⅱ.不再关注具体的实现类的实例
Ⅲ.更换不同的技术(模块),不需要改动代码,只需要修改applicationContext.xml的相关配置信息
Ⅳ.主要功能IOC(控制反转)松耦合、AOP (面向切面)内聚性
实现流程
1.首先在Struts2搭建基础上,再把下载好的Spring-4.2.2解压后,找到spring-framework-4.2.2.RELEASE\libs的jar包,复制到项目的WebContent\WEB-INF\lib目录下
(注意: 包含<Javadoc、sources源码>的都可以不要)
在这里插入图片描述在这里插入图片描述 在这里插入图片描述
(注意:导入Spring的包还不够,还差了Struts-2.3.30包里的struts-2.3.30-all\struts-2.3.30\apps\struts2-showcase.war\WEB-INF\lib的两个jar包)
2. 编辑web.xml文件,定义监听器 ,添加下列代码

<!-- 不写默认Spring需要读取 WebContent\WEB-INF\applicationContext.xml-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <!-- 改变默认读取路径为src目录下的 applicationContext.xml-->
    <!-- 在改变的路径下还是没找到,便自动到默认路径查找 -->
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!-- 定义监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

3.在src目录下新建applicationContext.xml用作编写Spring
在这里插入图片描述

<?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">
    <!-- 上面的头,注意版本,从样例里复制过来 -->
    <!-- id定义名字,class具体的类,scope="prototype"定义非单例 -->
    <bean id="myActionIndex" class="ssh.action.IndexAction" scope="prototype">
        <!-- 本例没有 -->
        <!-- 为Action类里的is属性注入id为myIndexService的实例 -->
        <property name="is" ref="myIndexService" />    
    </bean>
    <!-- 本例没有 -->
        <bean id="myIndexService" class="ssh.service.IndexServiceImpl" scope="prototype">
    </bean>
</beans>


//本例没有
public class IndexAction extends ActionSupport {
    //声明service,但不给它创建具体的实现类的实例,
    private IndexService is = null;
    public void setIs(IndexService is) {
        this.is = is;
    }
}

4.编辑struts.xml文件,添加下列代码(注意:新版本可以不用写,包括本例)

<struts>
     <!-- 告知Struts2运行时使用Spring来创建对象 -->
    <constant name="struts.objectFactory" value="spring" />
</struts>

改写action里的class属性

<!-- class改写成Spring注入的id定义的名字 -->
<action name="Index" class="ssh.action.IndexAction" method="execute">

完成以上流程,基本上Struts2-Spring的搭建就ok了
三、Hibernate(数据层)的搭建:
相关描述
Ⅰ.服务器与数据库之间的交互
Ⅱ. Hibernate封装了一系列的JDBC代码,提供相应的方法我们使用,使我们的开发变得简单快捷,效率大大提高
实现流程

  1. 首先在Struts2-spring搭建基础上,再把下载好的Hibernate5.2.2解压后,找到hibernate-release-5.2.2.Final\lib\required的jar包,复制到项目的WebContent\WEB-INF\lib目录下(注意:本例使用是MySQL,记得导入对应的数据库驱动包)
    在这里插入图片描述在这里插入图片描述

     <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
                 <!-- 注入连接池,包含了数据库用户名,密码等等信息 -->
                 <property name="dataSource" ref="myDataSource"></property>
                 <property name="hibernateProperties">
                     <props>
                         <!-- 数据库的方言 -->
                         <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                         <!-- 是否显示打印sql语句-->
                         <prop key="hibernate.show_sql">true</prop>
                         <!-- 是否格式化显示sql语句-->
                         <prop key="hibernate.format_sql">true</prop>
                         <!-- 是否自动提交-->
                         <prop key="hibernate.connection.autocommit">false</prop>
                         <!-- 没表帮你创建表,有表就更新表信息 -->
                         <prop key="hibernate.hbm2ddl">update</prop>
                     </props>
                 </property>
                 <property name="mappingResources">
                     <list>
                         <!-- 映射文件路径(实体类) -->
                         <value>ssh/entity/BookCard.hbm.xml</value>
                     </list>
                 </property>
    

3.为c3p0数据池导入相应的jar包,在Hibernate5.2.2压缩包hibernate-release-5.2.2.Final\lib\optional\c3p0的3个jar包, 复制到项目的WebContent\WEB-INF\lib目录下(提议:也可以使用…\optional\dbcp的数据池)(区别:c3p0:稳定性、dbcp:速度比较快)
在这里插入图片描述

<!-- 定义c3p0数据池 -->
    <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 数据库的驱动 -->
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <!-- 数据库的路径 -->
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/CardDB"/>
        <!-- 数据库的用户名 -->
        <property name="user" value="root"/>
        <!-- 数据库的密码 -->
        <property name="password" value="123456"/>
        <!-- 每300秒检查所有连接池中的空闲连接 -->
        <property name="idleConnectionTestPeriod" value="300"></property>
        <!-- 最大空闲时间,900秒内未使用则连接被丢弃。若为0则永不丢弃 -->
        <property name="maxIdleTime" value="900"></property>
        <!-- 最大连接数 -->
        <property name="maxPoolSize" value="2"></property>
    </bean>

5.第4步也可以替换成,以下写法,先在src目录下创建一个jdbc.properties的文件(用于记录保存数据库相关的信息)
在这里插入图片描述

1 jdbc.driver=com.mysql.jdbc.Driver
2 jdbc.url=jdbc:mysql://localhost:3306/CardDB
3 jdbc.user=root
4 jdbc.password=123456

然后applicationContext.xml文件就可以引用该文件内的参数

1 <property name="driverClass" value="${jdbc.driver}"/>
2 <property name="jdbcUrl" value="${jdbc.url}"/>
3 <property name="user" value="${jdbc.user}"/>
4 <property name="password" value="${jdbc.password}"/>

(注意:引用前提必须要在applicationContext.xml文件里添加以下代码,声明一下引用的文件路径)

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

6.最后在实体类的目录下新建一个实体类.hbm.xml的文件,进行以下编辑

<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping xmlns="http://www.hibernate.org/xsd/hibernate-mapping">
      <!-- name是实体类的具体路径,table是定义创建表的名字 -->
    <class name="ssh.entity.BookCard" table="BookCard">
        <!-- 定义主键的名称 -->
        <id name="cid" column="cid">
            <!-- 定义主键为自动增长 -->
            <generator class="native"></generator>
        </id>
        <!-- 定义数据库的其他的字段 的具体描述-->
        <property name="name" type="string" length="50" column="name" not-null="true"></property>
        <property name="sex" type="string" length="2" column="sex"></property>
        <property name="cardDate" type="date" column="cardDate"></property>
        <property name="deposit" type="double" column="deposit"></property>
    </class>
</hibernate-mapping>

猜你喜欢

转载自blog.csdn.net/CSDN1782747395/article/details/87923884