S2SH 环境搭建笔记

在myeclipse 8.6 环境下搭建一个S2SH的环境,通过一个简单的用户信息输入存储整理一下。对于S2SH环境的搭建可以认为是以spring为中心,将struts2和hiberbate整合进来。因此本文分两部分来整理:

一.对struts 2的整合:

核心就是将原本由struts框架创建的action托管给spring构造bean。

1.在web.xml中添加spring的监听器,spring框架会根据参数中给出的xml文件构造bean,如下:

   <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/classes/inputhandler.xml,/WEB-INF/classes/application*.xml</param-value>
    </context-param>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

2.修改strut.xml文件,将action的创建托管给spring,如下:

<constant name="struts.objectFactory" 
value="spring"></constant> //注意在myeclipse环境下要添加spring.jar和struts-spring-plugin-XX.jar
<package name = "struts" namespace="" extends="struts-default">
    <action name = "login" class="LogAction"> //此时的class里不再是具体的类,而是spring创建的bean id
        <result name = "LOG">/index.jsp</result>
    </action>
</package>
</struts>  

3.在spring的xml中创建bean,为了清晰起见,在applicationContext.xml增加import resource,新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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
   <bean id="des" class="com.DesInputHandler" />
    
    <bean id="LogAction" class="com.LogAction">
        <property name="handler">
            <ref bean="des"/>
        </property>
        <property name="usrdao">
            <ref bean="usrdao"/>
        </property>
        <property name = "preName">
            <value>Hello</value>
        </property>
    </bean>
</beans>

 

 

 

4.对应测试用的action类如下:

 

public class LogAction extends ActionSupport {
    private String  password;
    private String  name;
    private String  email;
    
//下面这些属性作为bean的property在构造时被赋值
private InputHandler handler; private String preName; private UsrDao usrdao;

二.hibernate整合

hibernate的整合相对简单,在myeclipse下创建时自动将sessionFactory构造成spring bean,见applicationContext中的定义:

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName"
            value="com.mysql.jdbc.Driver">
        </property>
        <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
  
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>./USER.hbm.xml</value></list>
        </property></bean>
        
        <bean id ="hibernateTemplate" class = "org.springframework.orm.hibernate3.HibernateTemplate">
            <property name = "sessionFactory" ref = "sessionFactory"></property>
        </bean>
        <bean id = "usrdao" class = "com.UsrDapImpl">
            <property name = "hibernateTemplate">
                <ref bean = "hibernateTemplate"></ref>
            </property>
            <property name = "sessionFactory">
                <ref bean = "sessionFactory"></ref>
            </property>
        </bean>
    <import resource="inputhandler.xml"/>
    </beans>

spring提供了hibernateTemplate实现CRUD操作,见UsrDaoImpl实现:

public class UsrDaoImpl implements UsrDao {
    
    private SessionFactory sessionFactory;
    private HibernateTemplate hibernateTemplate;
    
    public void save(USER usr) {
        // TODO Auto-generated method stub
        System.out.println(usr.getEmail() + "saved");
        getHibernateTemplate().save(usr);
        
    }

    public HibernateTemplate getHibernateTemplate() {
        if(hibernateTemplate == null){
            HibernateTemplate hibernateTemplate 
            = new HibernateTemplate(sessionFactory);
        }
        return hibernateTemplate;
    }

备注: 

出现异常问题org.apache.commons.dbcp.BasicDataSource 和org.springframework.orm.hibernate3.LocalSessionFactoryBean 没有找到的错误。在myeclipse 8.6环境下采用其插件构建S2SH环境主要原因是在spring框架倒入时加入的包不够。应导入包括:spring 3.0 persistence core libirary 和spring 3.0 persistence jdbc libirary

posted @ 2013-04-05 15:53  Fredric_2013  阅读(239)  评论(0编辑  收藏  举报