SSH整合

note.txt

首先看需要导入的jar包
1.spring(15个):必须的15个jar包(包括logging)
2.hibernate(11个):必须的8的jar包;c3p0的3个
3.MySQL(1个):连接驱动的1个jar包
4.struts(12个):blank的11个(已经去掉了和hibernate中重名的javassist-3.11.0.GA.jar与和spring中重的logging)
        和spring整合额外需要的struts2-spring-plugin-2.3.15.3.jar

SSH有3个重点
    1.整合hibernate,用spring的配置文件配置sessionFactory的Bean,并加入声明式事务
    2.整合struts,在web.xml中加入listener
    3.整合struts,将Action类配置成bean
    
收获:
1.如何在页面上写下拉框
    <s:select name="department" label="Department"
        list="#request.departmentList" listKey="id" listValue="name" headerValue="选择部门" headerKey="">
    </s:select>
    
    <!-- 
        这里通过一个list类型的对象来动态生成一个下拉列表。
        list="#request.departmentList"表示对象栈里有一个list<Department>类型的departmentList属性。
        listKey="id"表示当前option的value从departmentList里的当前department对象的id属性取值。是要传回去的值
        listValue="name"表示当前option的text从departmentList里的当前department对象的name属性取值。下拉框中显示的值
     -->
    <s:select name="department.id" label="Department"
        list="#request.departmentList" listKey="id" listValue="name">
    </s:select>    
    
2.怎么使用类型转换器
    01.首先写好转换器类,需要extends StrutsTypeConverter,填写两个方法
            /**
             * 返回值是转换好的类型的对象
             * String[] arg1:从页面过来的字符串数组
             * Class arg2:表示要把字符串转换成的类型
             * */
            @Override
            public Object convertFromString(Map arg0, String[] arg1, Class arg2) {}
        
            /**
             * 返回值是要在页面上显示的字符串
             * */
            @Override
            public String convertToString(Map arg0, Object arg1) {}
    02.两种配置方法
        >基于类型:可以为一个数据类型配置转换器
                (在web应用初始化时创建,注意此时servletContext为null,创建两次,了解:因为注册器有两个,不是单实例)
                在src下创建xwork-conversion.properties
                内容:数据类型全类名=转换器全类名
        >基于字段:可以为某个 Model(该 Model 类也可能是 Action,道理和下面的类型转换错误消息覆盖的第一种相同)
                的各个属性分别配置一个自定义的转换器.
                (在需要转换时时创建,单实例) 
                创建一个属性文件: ModelClassName-conversion.properties, 该文件需和相对应的 Model 类放在同一个目录下
                内容:属性名=转换器全类名

3.<s:date/>的使用
    <s:date name="birth" format="yyyy年MM月dd日"/>

4.table节点内的属性的意义
    <table cellpadding="10" cellspacing="0" border="1">
    cellpadding:表内字距离表边框的距离是10
    cellspacing:每个小框之间的距离,=0表示没有空隙
    border:表边框的线宽度

5.struts2本身会自动调栈顶对象的toString()方法所以有时候会出空指针异常    

6.hibernate的持久化类中必须有geter方法

7.类型转换错误消息的覆盖
    两种方式都可以
    >若一个model类对象作为栈顶元素被赋值,那么在对应的 Action类或model类所在的包中
        新建  ActionClassName.properties 文件或ModelClassName.properties 文件即可
    >若该输入字段仅存在于Action类中,那么在对应的 Action 类所在的包中新建  ActionClassName.properties 文件
    文件内容都是invalid.fieldvalue.字段名=要显示的错误消息

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

<struts>
    
    <package name="default" namespace="/" extends="struts-default">
    
        <interceptors>
            <interceptor-stack name="myStack">
                <interceptor-ref name="paramsPrepareParamsStack">
                    <param name="prepare.alwaysInvokePrepare">false</param>
                </interceptor-ref>
            </interceptor-stack>
        </interceptors>
        <default-interceptor-ref name="myStack"></default-interceptor-ref>
        
        <!-- class="employeeAction" 即在spring配置文件中配置的id -->
        <action name="*" class="employeeAction" method="{1}">
            <result name="success" type="redirectAction">listEmployee</result>
            <result name="input">/addEmployeePage.jsp</result>
            <result name="{1}">/{1}.jsp</result>
        </action>
    </package>

</struts>

applicationContext_sh.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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">


    <!-- 配置dataSource,因为sessionFactory要用到 -->
    <context:property-placeholder location="classpath:db.properties"/>
    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property>
        <property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property>
    </bean>
    
    <!-- 整合hibernate之核心 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        <!-- !!!!注意要用目录格式,/ -->
        <property name="mappingLocations" value="classpath:entities/*.hbm.xml"></property>
    </bean>
    
    <!-- 将service下的类的方法声明为事务 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
    
    <aop:config>
        <aop:pointcut expression="execution(* service.*.*(..))" id="pointcut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
    </aop:config>
</beans>

applicationContext_bean.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
    <bean class="dao.EmployeeDAO" id="employeeDAO">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <bean class="service.EmployeeService" id="employeeService">
        <property name="employeeDAO" ref="employeeDAO"></property>
    </bean>
    
    <bean id="employeeAction" class="actions.EmployeeAction" scope="prototype">
        <property name="employeeService" ref="employeeService"></property>
    </bean>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    
    
    
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext_*.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <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>
</web-app>
原文地址:https://www.cnblogs.com/feifeiyun/p/6560651.html