dwr简介--一个例子

上一篇我主要介绍dwr的概况。这一篇我用dwr做了个可以不刷新页面就更新的表格。
screen.jpg
运行环境:
windows xp pro sp2
j2sdk1.2.4_03
weblogic8.1
struts1.2.4
开发工具eclipse3.0
其实dwr和struts没有什么关系,只不过最近我们项目组在用struts作东西。我就顺便用把我的程序建立在Struts上。
主要文件。
dwr.jar--dwr的类库包
struts的类库包,具体我不说了,这东西谁都知道。
jdts0.9.jar--数据库SQLServer的驱动程序包。
以上jar包放在WebContent\WEB-INF\lib下
web.xml--谁都知道这东西干嘛用的吧。
struts-config.xml --这个也不说了。
dwr.xml -- dwr的配置文件
weblogic.xml -- weblogic模块配置文件。
还有一个struts的tld就不说了
以上文件放在WebContent\WEB-INF下面。
login.jsp -- 登陆界面,这里我也用到了dwr
showtable.jsp --登陆成功会转到这个页面,一个ajax表格。
showtable.js -- showtable.jsp中用到的javascript
main.css -- 不说了
还有 *.gif界面要到的图片
以上文件放在WebContent下
剩下的就是java类了。
LoginAction.java --Struts的Action,负责登陆
TableAction.java --Struts的Action,负责表格内容初始化
UserLogic.java --负责验证用户
TableRowConverter.java -- 继承于dwr的BeanConverter,负责将一个对象转成javascript能用的东西。
LoginForm.java --Struts的Form,负责登陆信息
TableModelBean.java --TableModel一部分给struts用一部分给dwr用。
TableRowBean.java 用户存放行信息的Bean。
ModelOneDAO.java --随便取的名字,有点恶(三声)。负责从数据库操作的。

这个例子还需要一个数据库,我用的是SQLServer。
下面是建表的SQL语句。输入数据的SQL就不贴了太长了。我会弄个源码下载的。
/* ============================================================== */
/*  DBMS name:      Microsoft SQL Server 2000                     */
/*  Created on:     2005-8-1 13:21:33                             */
/* ============================================================== */


if   exists  ( select   1
            
from   sysobjects
           
where   id  =   object_id ( ' AJAX_MODEL_ONE ' )
            
and    type  =   ' U ' )
   
drop   table  AJAX_MODEL_ONE
go


/* ============================================================== */
/*  Table: AJAX_MODEL_ONE                                         */
/* ============================================================== */
create   table  AJAX_MODEL_ONE (
   col1                 
int                    not   null ,
   col2                 
int                    not   null ,
   col3                 
int                    not   null ,
   
constraint  PK_AJAX_MODEL_ONE  primary   key   (col1)
)
go
接下来是写业务逻辑
Login.java
ExpandedBlockStart.gif ContractedBlock.gif /**/ /*
InBlock.gif * Created on 2005-7-29
InBlock.gif *
InBlock.gif * TODO To change the template for this generated file go to
InBlock.gif * Window - Preferences - Java - Code Style - Code Templates
ExpandedBlockEnd.gif 
*/

None.gifpackage org.mstar.strutsajax.action;
None.gif
None.gifimport javax.servlet.http.HttpServletRequest;
None.gifimport javax.servlet.http.HttpServletResponse;
None.gif
None.gifimport org.apache.struts.action.Action;
None.gifimport org.apache.struts.action.ActionForm;
None.gifimport org.apache.struts.action.ActionForward;
None.gifimport org.apache.struts.action.ActionMapping;
None.gifimport org.mstar.strutsajax.ajax.UserLogic;
None.gifimport org.mstar.strutsajax.form.LoginForm;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /**
InBlock.gif * @author matianyi
InBlock.gif *
InBlock.gif * TODO To change the template for this generated type comment go to
InBlock.gif * Window - Preferences - Java - Code Style - Code Templates
ExpandedBlockEnd.gif 
*/

ExpandedBlockStart.gifContractedBlock.gif
public   class  LoginAction extends Action  dot.gif {
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//* (non-Javadoc)
InBlock.gif     * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
ExpandedSubBlockEnd.gif     
*/

InBlock.gif    
public ActionForward execute(ActionMapping mapping, ActionForm form,
ExpandedSubBlockStart.gifContractedSubBlock.gif            HttpServletRequest request, HttpServletResponse response) throws Exception 
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if(validateUser((LoginForm)form))dot.gif{
InBlock.gif            
return mapping.findForward("success");            
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 else dot.gif{
InBlock.gif            
return mapping.findForward("failure");
ExpandedSubBlockEnd.gif        }
        
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private boolean validateUser(LoginForm form)dot.gif{
InBlock.gif        UserLogic userLogic 
= new UserLogic();
InBlock.gif        
return userLogic.validate(form.getUsername(),form.getPassword());        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
UserLogic.java
None.gif package org.mstar.strutsajax.ajax;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /**
InBlock.gif * @author matianyi
InBlock.gif *
InBlock.gif * TODO To change the template for this generated type comment go to
InBlock.gif * Window - Preferences - Java - Code Style - Code Templates
ExpandedBlockEnd.gif 
*/

ExpandedBlockStart.gifContractedBlock.gif
public   class  UserLogic  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public boolean validate(String username,String password)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if("mty".equals(username)&&"123".equals(password))dot.gif{
InBlock.gif            
return true;
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 else dot.gif{
InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
LoginForm.java
None.gif package org.mstar.strutsajax.form;
None.gif
None.gifimport org.apache.struts.action.ActionForm;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /**
InBlock.gif * @author matianyi
InBlock.gif *
InBlock.gif * TODO To change the template for this generated type comment go to
InBlock.gif * Window - Preferences - Java - Code Style - Code Templates
ExpandedBlockEnd.gif 
*/

ExpandedBlockStart.gifContractedBlock.gif
public   class  LoginForm extends ActionForm  dot.gif {
InBlock.gif    
private String username;
InBlock.gif    
private String password;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**
InBlock.gif     * @return Returns the password.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public String getPassword() dot.gif{
InBlock.gif        
return password;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**
InBlock.gif     * @param password The password to set.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setPassword(String password) dot.gif{
InBlock.gif        
this.password = password;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**
InBlock.gif     * @return Returns the username.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public String getUsername() dot.gif{
InBlock.gif        
return username;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**
InBlock.gif     * @param username The username to set.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setUsername(String username) dot.gif{
InBlock.gif        
this.username = username;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
TableRowBean.java
None.gif package org.mstar.strutsajax.form;
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /**
InBlock.gif * @author matianyi
InBlock.gif *
InBlock.gif * TODO To change the template for this generated type comment go to
InBlock.gif * Window - Preferences - Java - Code Style - Code Templates
ExpandedBlockEnd.gif 
*/

ExpandedBlockStart.gifContractedBlock.gif
public   class  TableRowBean dot.gif {
InBlock.gif    
private String col1Value;
InBlock.gif    
private String col2Value;
InBlock.gif    
private String col3Value;
InBlock.gif    
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**
InBlock.gif     * @return Returns the col1Value.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public String getCol1Value() dot.gif{
InBlock.gif        
return col1Value;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**
InBlock.gif     * @param col1Value The col1Value to set.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setCol1Value(String col1Value) dot.gif{
InBlock.gif        
this.col1Value = col1Value;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**
InBlock.gif     * @return Returns the col2Value.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public String getCol2Value() dot.gif{
InBlock.gif        
return col2Value;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**
InBlock.gif     * @param col2Value The col2Value to set.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setCol2Value(String col2Value) dot.gif{
InBlock.gif        
this.col2Value = col2Value;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**
InBlock.gif     * @return Returns the col3Value.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public String getCol3Value() dot.gif{
InBlock.gif        
return col3Value;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**
InBlock.gif     * @param col3Value The col3Value to set.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setCol3Value(String col3Value) dot.gif{
InBlock.gif        
this.col3Value = col3Value;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
上面的代码都比较简单,不用说大家也都知道是干什么用的。
下面就是主要的内容了。预知后事如何,且听下回分解。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值