第一个struts+hibernate实验

第一个struts+hibernate实验

参考原文地址 http://homepage.mac.com/edahand/projects/java/example1.html

1  在E盘建立一个目录sandh, 然后把那个空的struts-blank.war解压到这下面作为struts模板,把hibernate3.1里面的jar文件拷贝到/sandh/web-inf/lib下面,ehcache.xml拷贝到/sand/web-inf/src/java下面。

2  在tomcat下配置datasource命名为jdbc/aix,然后建立一个新的context名字为sand,在sand下建一个新的资源连接jdbc/aix,作为hibernate.cfg.xml中hibernate.connection.datasouce引用值。

3  在sybase中新建一个表item

create table dbo.item (
id  numeric(
18 0 )  identity,
name    varchar(
32 ) not  null ,
description text    
null ,
constraint PK_ITEM PRIMARY KEY  NONCLUSTERED ( id )
)


4  在/sandh/web-inf/src/java下面建立以下文件
log4j.properties 
hibernate.cfg.xml
Item.hbm.xml
HibernateUtil.java
Item.java
AddItemAction.java
ItemService.java
在/sandh/web-inf/src下面新建build.xml文件,然后ant compile。
在/sandh/pages下新建AddItem.jsp文件

5   修改/sandh/web-inf/struts-config.xml文件,内容如下:

< struts - config >
    
< form - beans >
        
< form - bean name = " addItemForm "  
                   type
= " org.apache.struts.validator.DynaValidatorForm " >
                   
< form - property name = " name "  type = " java.lang.String " />
                   
< form - property name = " description "  type = " java.lang.String " />
        
</ form - bean >
    
</ form - beans >

    
< global - forwards >
        
< forward name = " welcome "  path = " /items.do " />
    
</ global - forwards >
    
    
< action - mappings >
        
< action
        path
= " /items "
        type
= " org.apache.struts.actions.ForwardAction "
        parameter
= " /pages/AddItem.jsp " />
     
        
< action
           path
= " /addItem "
           type
= " com.fan.hibernate.AddItemAction "
           name
= " addItemForm "
           scope
= " request "
           validate
= " true "
           input
= " /pages/AddItem.jsp " >
           
< forward name = " success "  path = " /pages/AddItem.jsp "   />
         
</ action >
    
</ action - mappings >
    
    
< message - resources parameter = " MessageResources "   null = " false " />
    
    
< plug - in className = " org.apache.struts.validator.ValidatorPlugIn " >
       
< set - property property = " pathnames "  value = " /WEB-INF/validator-rules.xml,/WEB-INF/validation.xml "   />
    
</ plug - in >     
</ struts - config >



修改/sandh/web-inf/validation.xml文件,为addItemForm增加动态验证,内容如下:

     < formset >
        
< form name = " addItemForm " >
            
< field property = " name "  depends = " required " >
                
< arg key = " name "  resource = " false " />
            
</ field >
            
< field property = " description "  depends = " required " >
                    
< arg key = " description "  resource = " false " />
            
</ field >
        
</ form >
    
</ formset >


6   在tomcat下面测试一下吧。http://127.0.0.1:8080/sand

7   测试的时候发现2个问题:
首先,如果在/sandh/web-inf/classes下面没有ehcache.xml这个文件,会报这个错误,No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath:.....这个文件是hibernate缓存配置文件,以后在学习。
另外在log文件中一直发现这个警告:Could not bind factory to JNDI javax.naming.NamingException: Context is read only..... 网上查找后发现只要在hibernate.cfg.xml文件的<session-factory name="java:/hibernate/HibernateFactory"> 中把name=....去掉就好了,否则会自动把name注册进jndi中失败而发出警告。

8   所有文件打包在这里 http://www.blogjava.net/Files/luckyrobbie/sandh.rar 里面lib下面的文件自己添进去吧。



你可能感兴趣的:(第一个struts+hibernate实验)