Struts2+spring2+hibernate3整合方案


最近闲来无事可做,于是开始学习struts2。

整合原理以spring为容器,管理hibernate的DAO和Struts2的Action。


一、 准备工作
Struts2.06+spring2.5+hibernate3.2+jdk6.0+myeclipse6.0+tomcat5.5+mysql5.0
以上是整合的原料。下面以一个注册登陆的例子来开始我们的整合过程。
这个例子很简单,下面是它的sql脚本内容:
CREATE TABLE `user` (
  `userid` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) NOT NULL,
  `password` varchar(16) NOT NULL,
  `email` varchar(30) NOT NULL,
  PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf-8;
二、 开始行动
包结构可以参考下图
 Struts2+spring2+hibernate3整合方案_第1张图片
图一
Struts2+spring2+hibernate3整合方案_第2张图片
 图二
Struts2+spring2+hibernate3整合方案_第3张图片
图三
1) Struts部分:建立struts.xml和struts.properties
Struts.xml内容如下:

 

<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"  >
< struts >
    
< package  name ="user_curd"  extends ="struts-default"   >
        
< global-results >
            
<!--  下面定义的结果对所有的Action都有效  -->
            
< result  name ="exception" > /error.jsp </ result >
        
</ global-results >

        
< global-exception-mappings >
            
<!--  指Action抛出Exception异常时,转入名为exception的结果。  -->
            
< exception-mapping  exception ="java.lang.Exception"  result ="exception" />
        
</ global-exception-mappings >

        
< action  name ="Login"  class ="LoginAction" >
            
< result  name ="success" > /success.jsp </ result >     
            
< result  name ="input" > /login.jsp </ result >
        
</ action >
        
< action  name ="Regist"  class ="RegistAction" >
            
< result  name ="success" > /success.jsp </ result >     
            
< result  name ="input" > /regist.jsp </ result >
        
</ action >
    
</ package >  
            
</ struts >


Struts.properties内容如下:

 

struts.devMode = false
struts.enable.DynamicMethodInvocation
= true
struts.i18n.reload
= true
struts.ui.theme
=xhtml

struts.locale
= zh_CN
struts.i18n.encoding
= UTF- 8
struts.objectFactory
= spring
struts.objectFactory.spring.autoWire
= name

struts.serve.static.browserCache
= false
struts.url.includeParams
= none


2) 建立User.java和User.hbm.xml、jdbc.properties:
User.java内容如下:

 

/** 
 * 
 * 
@author <a href="mailto:[email protected]">flustar</a>
 * 
@version 1.0 
 * Creation date: Dec 23, 2007 1:55:28 PM
 
*/

package  com.firstssh.model;

import  java.io.Serializable;

public   class  User  implements  Serializable  {
    
private int id;
    
private String username;
    
private String password;
    
private String email;

    
public int getId() {
        
return id;
    }


    
public void setId(int id) {
        
this.id = id;
    }


    
public String getUsername() {
        
return username;
    }


    
public void setUsername(String username) {
        
this.username = username;
    }


    
public String getPassword() {
        
return password;
    }


    
public void setPassword(String password) {
        
this.password = password;
    }


    
public String getEmail() {
        
return email;
    }


    
public void setEmail(String email) {
        
this.email = email;
    }

}


User.hbm.xml内容:

 

<? xml version="1.0" ?>
<! DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
< hibernate-mapping 
    
package ="com.firstssh.model" >

    
< class  name ="User"  table ="User" >
        
< id  name ="id"  column ="userid" >
            
< generator  class ="identity"   />
        
</ id >     
        
< property  name ="username"
                  column
="username"
                  not-null
="true"
                  length
="20"
        
/>
        
< property  name ="password"
                  column
="password"
                  not-null
="true"
                  length
="16"   />
        
< property  name ="email"
                  column
="email"
                  not-null
="true"
                  length
="30" />
    
</ class >
    
</ hibernate-mapping >


jdbc.properties内容如下:

 

datasource.type = mysql
datasource.driverClassName
= com.mysql.jdbc.Driver
datasource.url
= jdbc:mysql://localhost: 3306 /test?useUnicode = true&characterEncoding = UTF- 8

datasource.username
= root
datasource.password
= 123456

datasource.maxActive
= 10
datasource.maxIdle
= 2
datasource.maxWait
= 120000
datasource.whenExhaustedAction
= 1
datasource.validationQuery
= select  1  from dual
datasource.testOnBorrow
= true
datasource.testOnReturn
= false

c3p0.acquireIncrement
= 3
c3p0.initialPoolSize
= 3
c3p0.idleConnectionTestPeriod
= 900
c3p0.minPoolSize
= 2
c3p0.maxPoolSize
= 50
c3p0.maxStatements
= 100
c3p0.numHelperThreads
= 10
c3p0.maxIdleTime
= 600

hibernate.dialect
= org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect
= org.hibernate.dialect.MySQLMyISAMDialect

hibernate.jdbc.batch_size
= 25
hibernate.jdbc.fetch_size
= 50
hibernate.show_sql
= true
hibernate.connection.release_mode
= after_transaction


3) Spirng部分:为了清晰把Spring的配置文件拆分成以下几部分applicationContext-dao.xml、appliationContext-service.xml、applicationContext-hibernate.xml、action-servlet.xml。
applicationContext-hibernate.xml内容:

 

<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" >
< beans >
< bean  id ="propertyConfigurer"
    class
="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
    
< property  name ="locations" >
        
< list >
            
<!--   <value>WEB-INF/mail.properties</value> -->
            
< value > WEB-INF/jdbc.properties </ value >
            
<!--   <value>WEB-INF/oscache.properties</value> -->
        
</ list >
    
</ property >
</ bean >
<!--  MailSender used by EmailAdvice <%
0
0
 
 

你可能感兴趣的:(spring,exception,Hibernate,struts,c3p0,MyEclipse)