Struts2.1+Spring3.0+Hebernate3.3 MyEclipse搭建SSH

来源:互联网 发布:base64.decode java 编辑:程序博客网 时间:2024/05/17 02:29

          二面果然失败,不过路还得继续。今天我来讲一讲我与SSH恩怨情仇。

      首先创建Web Project,接着的步骤可以看下面的链接

          http://jingyan.baidu.com/article/c45ad29ceb9b5a051753e298.html

      按照上面配置好之后,可以部署一个简单的登陆框架

      我的包结构如下(怎么创建不懂可留言):

注意自己添加MySQL的驱动包到WebRoot/libs目录下面


数据库:



打开MyEclipse DataBase Explorer,双击左侧mySql驱动,建立连接。你可以通过MySQL-Front管理数据库,在这儿查看数据。

右键点击Table下面的表,选择Hibernate Reverse Engineering


你可以选择是否生成POJO类,在applicationContext.xml(最下面)里面配置。这样会自动生成User.hbm.xml文件,其他表类似。


Action:

这是我的BaseActionuserMgr在applicationContext里面注入,继承的类可以选择其他的HttpServlet等等

public class BaseAction extends ActionSupport{

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    
    private HttpServletRequest resquest;
    private HttpServletResponse response;
    private ActionContext context;
    public IUserService userMgr;

    public void setUserMgr(UserService userMgr) {
        System.out.println("UserService");
        this.userMgr = userMgr;
    }
    protected final Logger logger = LoggerFactory.getLogger(getClass());

    public ActionContext getContext() {
        if(context == null)
        {
            System.out.println("ActionContext");
            context = ActionContext.getContext();
        }
        return context;
    }
    public void setContext(ActionContext context) {
        this.context = context;
    }
    public HttpServletRequest getResquest() {
        if(resquest == null)
        {        
            resquest = (HttpServletRequest) getContext().get(StrutsStatics.HTTP_REQUEST);
        }
        return resquest;
    }
    public HttpServletResponse getResponse() {
        if(response == null)
        {        
            response = (HttpServletResponse)getContext().get(StrutsStatics.HTTP_RESPONSE);
        }
        return response;
    }
    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }
    public void setResquest(HttpServletRequest resquest) {
        this.resquest = resquest;
    }
}


     因为需要接受和传递客户端的信息,就需要HttpServletRequestHttpServletResponse两个对象,通过ActionContext来获取。至于ActionContext,它(com.opensymphony.xwork.ActionContext)Action执行时的上下文,上下文可以看作是一个容器,它存放放的是Action在执行时需要用到的对象。


Bean层包括所有表的映射;

DAO层封装了对Bean的基本操作,如


   DAOImpl层实现了接口定义的具体方法,如:


  Service是Bean的管理接口:


 ServiceImpl实现管理类和接口:


这些是业务逻辑的部分,真正重要的还是applicationContext.xml和struts.xml的配置

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: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="com.mchange.v2.c3p0.ComboPooledDataSource"       
        destroy-method="close">                                                   
        <property name="driverClass">                                             
            <value>com.mysql.jdbc.Driver</value>                                  
        </property>                                                               
        <property name="jdbcUrl">                                                 
            <value>jdbc:mysql://localhost/kumu</value>                            
        </property>                                                               
        <property name="user">                                                    
            <value>root</value>                                                   
        </property>                                                               
        <property name="password">                                                
            <value>root</value>                                                   
        </property>                                                               
        <property name="maxPoolSize">                                             
            <value>80</value>                                                     
        </property>                                                               
        <property name="minPoolSize">                                             
            <value>1</value>                                                      
        </property>                                                               
        <property name="initialPoolSize">                                         
            <value>1</value>                                                      
        </property>                                                               
        <property name="maxIdleTime">                                             
            <value>20</value>                                                     
        </property>                                                               
    </bean>                                                                       
    <!--定义Hibernate的SessionFactory -->                                            
    <!-- SessionFactory使用的数据源为上面的数据源 -->                                          
    <!-- 指定了Hibernate的映射文件和配置信息 -->                                               
    <bean id="sessionFactory"                                                     
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">       
        <property name="dataSource">                                              
            <ref local="dataSource" />                                            
        </property>                                                               
        <property name="mappingResources">                                        
            <list>                                                                
                <value>com/example/bean/User.hbm.xml</value>                      
            </list>                                                               
        </property>                                                               
        <property name="hibernateProperties">                                     
            <props>                                                               
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</
                <!-- 是否根据需要每次自动创建数据库 -->                                          
                <prop key="hibernate.hbm2ddl.auto">update</prop>                  
                <prop key="show_sql">true</prop>                                  
                <!-- 将SQL脚本进行格式化后再输出 -->                                          
                <prop key="hibernate.format_sql">true</prop>                      
                <prop key="hibernate.jdbc.batch_size">20</prop>                   
                <prop key="connection.pool_size">10</prop>                        
                <prop key="current_session_context_class">thread</prop>           
                <prop key="cache.provider_class">org.hibernate.cache.NoCacheProvid
            </props>                                                              
        </property>                                                               
    </bean>                                                                       
                                                                                  
                                                                                  
    <bean id="transactionManager"                                                 
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">   
        <property name="sessionFactory">                                          
            <ref bean="sessionFactory" />                                         
        </property>                                                               
    </bean>                                                                       
                                                                                  
    <bean id="txProxyTemplate" abstract="true"                                    
            class="org.springframework.transaction.interceptor.TransactionProxyFac
        <!-- 注入事物管理器 -->                                                          
            <property name="transactionManager" ref="transactionManager"></propert
            <!-- 配置事物属性 -->                                                       
            <property name="transactionAttributes">                               
                <props>                                                           
                    <prop key="save*">PROPAGATION_REQUIRED</prop>                 
                    <prop key="remove*">PROPAGATION_REQUIRED</prop>               
                    <prop key="update*">PROPAGATION_REQUIRED</prop>               
                    <prop key="change*">PROPAGATION_REQUIRED</prop>               
                    <prop key="*">PROPAGATION_REQUIRED</prop>                     
                </props>                                                          
            </property>                                                           
    </bean>                                                                                                                                              
                                                                                  
      <!-- DAO -->                                                                  
      <bean id="BaseDAO" class="com.example.daoImpl.BaseDAOImpl">                   
          <property name="sessionFactory">                                          
            <ref bean="sessionFactory" />                                         
        </property>                                                               
    </bean>                                                                       
    <bean id="UserDAO" class="com.example.daoImpl.UserDAOImpl" scope="prototype">
        <property name="sessionFactory">                                          
            <ref bean="sessionFactory" />                                         
        </property>                                                               
    </bean>                                                                       
    <bean id="GoodDAO" class="com.example.daoImpl.GoodDAOImpl">                   
        <property name="sessionFactory">                                          
            <ref bean="sessionFactory" />                                         
        </property>                                                               
    </bean>                                                                       
                                                                                  
    <!-- 业务逻辑类 -->                                                                
    <bean id="userService" class="com.example.serviceImpl.UserService">           
        <property name="userDAO">                                                 
            <ref bean="UserDAO" />                                                
        </property>                                                               
    </bean>                                                                       
                                                                                  
    <!-- Action -->                                                               
    <bean id="RegisterAction" class="com.example.user.action.RegisterAction">     
        <property name="userMgr" ref="userService"/>                              
    </bean>                                                                                                                                                   
</beans> 
           

这是Spring带来方便之处,控制反转和依赖注入,同时易于管理。所以不需要用New来新建对象。

Action的ID是访问Action类的关键,在客户端请求是只需要传入这个ID即可,同时要严重注意一点,这个ID就是Struts.xml配置里面的Class ,找了原因找了好久,才发现(大哭) ,同学们不要再踩这个坑。                                                         

struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>                                
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts     
Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>                                                               
    <include file="struts-default.xml"></include>                      
    <package name="example" extends="struts-default" namespace="/" >   
        <action name="register" class="RegisterAction">                
            <result name="input">/Register.jsp</result>                
            <result name="success">/Success.jsp</result>               
            <result name="error">/Fail.jsp</result>                    
        </action>                                                      
    </package>                                                         
</struts>  
 

这些是客户端的配置,关于applicationContext.xml 里面标签的含义,可以查看以下链接

http://wenku.baidu.com/view/8a93c79951e79b89680226e0.html      



接下来是客户端的简单应用,我用的是Android客户端 :



注意:保证网络畅通,并且电脑和手机在同一局域网下(或者手机连接电脑WIFI),可以用手机链接wifi,然后客户端请求地址可以用cmd查看,ipconfig

                                               
                                                                      

这个是查找到IP地址,然后简单写个登陆或者注册界面,用HttpClient或者其他的发出网络请求,我用的是Volley,即可。

到此结束,欢迎指正。。。大笑


最后提醒大家,MyEclipse的net.sf的JSON包貌似用了有问题,所以最好还是自己去官网上下一个,然后替换掉。


















0 0