利用spring管理struts

SSH部分
第一:在做SSH配置之前,一定要把jar包文件夹下的所有JAR包COPY到工程中,否则报错误.

第二:在做Service类的时候首先要定义一个调用Dao的接口并添加上Set和Get方法
例:
public class ServiceClass implements ServiceInterface
{
 //这个是定义的接口
 private DaoInterface mydao;  
 //这个是Set和Get方法

  public  DaoInterface getMydao() 
 
{
  
return mydao;
 }


 
public   void  setMydao(DaoInterface mydao) 
 
{
  
this.mydao = mydao; 
 }

 
 
public  List findAll() 
 

  
return mydao.findAll();
 }

 
public   void  insert(Sshtable transientInstance) 
 
{
  mydao.insert(transientInstance);
 }

 
public   void  delete( int  persistentInstance) 
 
{
  mydao.delete(persistentInstance);
 }

}


第三:如果出现STRUTS乱码问题
在JSP页头写

 

<% @ page language="java" import="java.util.*" contentType="text ml;charset=utf-8" pageEncoding="GBK" %>


后台转换:

New String(str.getBytes(“iso - 8859 - 1 ”),”utf - 8 ”);


就可以了


在数据库连接的地方:

< property name = " url "  value = " jdbc:mysql://localhost:3306/ssh?characterEncoding=utf-8&amp;useUnicode=true " >
</ property >


第四:在配置数据库DAO的HBM文件中,自增主建的时候 一般用native或者assigned(这个不确定或者是其它,哪个好用就用哪一个),
例:

< hibernate-mapping >
< class  name ="org.yuanyuan.database.Yuan01"  table ="yuan01"  catalog ="loveyuanyuan" >
        
< id  name ="id"  type ="java.lang.Integer" >
            
< column  name ="id"   />
            
< generator  class ="assigned"   />
        
</ id >
       
//或者
< hibernate-mapping >
    
< class  name ="Hibernate.Sshtable"  table ="sshtable"  catalog ="ssh" >
        
< id  name ="name"  type ="java.lang.Integer" >
            
< column  name ="name"   />
            
< generator  class ="native"   />
        
</ id >


 第五:在applicationContext.xml中配置以下红色字的地方就可以在控制台中,打印出hibernate的中的SQL语句

 

< bean  id ="SessionFactory"
  class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
  
  
< property  name ="dataSource" >
   
< ref  bean ="DataSource"   />
  
</ property >
  
  
< property  name ="hibernateProperties" >  
   
< props >  
    
< prop  key ="hibernate.show_sql" > true </ prop >  
    
< prop  key ="hibernate.dialect" > org.hibernate.dialect.MySQLDialect </ prop >
   
</ props >
  
</ property >  
  
  
< property  name ="mappingResources" >
   
< list >
    
< value > Hibernate/Sshtable.hbm.xml <  alue >
   
<  st >  
  
</ property >
</ bean >

 

第六 struts+spring整和的时候在struts-config.xml中的 下添加(必须是这个位置)

< message-resources  parameter ="com.ssh.struts.ApplicationResources"   />< plug-in  className ="org.springframework.web.struts.ContextLoaderPlugIn" >
  
< set-property  property ="contextConfigLocation"  value ="/WEB-INF/classes/applicationContext.xml"   />
 
</ plug-in >



然后把所有struts-config.xml里action中的type=的内容修改成type="org.springframework.web.struts.DelegatingActionProxy">


以上为struts-config.xml的配置其中注意
value="/WEB-INF/classes/applicationContext.xml"的路径以TOMCAT中路径为标准.
在applicationContext.xml中添加对应struts-config.xml里的action的bean 就可以了
<bean name="/StrutsAction" class="StrutsAction.StrutsAction"></bean>
其中name="/StrutsAction"与action中的path="/StrutsAction"对应
 class="StrutsAction.StrutsAction" 就是
type="org.springframework.web.struts.DelegatingActionProxy">替换掉的哪一部分.
例子.
Struts的action

 

< action  path ="/StrutsAction"  
  type
=" StrutsAction.StrutsAction "
>
< forward  name ="finish"  path ="/index.jsp"   />
</ action >

//修改成
< action  path ="/StrutsAction"  
  type
="org.springframework.web.struts.DelegatingActionProxy"
>
< forward  name ="finish"  path ="/index.jsp"   />
</ action >


在applicationContext.xml新建立的BEAN为

< bean  name ="/StrutsAction"  class ="StrutsAction.StrutsAction" ></ bean >


这样就OK了

 

 

 

赶快试试吧!~~~
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值