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
/**/ /*
 * Created on 2005-7-29
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 
*/

package org.mstar.strutsajax.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.mstar.strutsajax.ajax.UserLogic;
import org.mstar.strutsajax.form.LoginForm;

/**/ /**
 * @author matianyi
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 
*/

public   class  LoginAction extends Action  {

    
/**//* (non-Javadoc)
     * @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)
     
*/

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

    
    
private boolean validateUser(LoginForm form){
        UserLogic userLogic 
= new UserLogic();
        
return userLogic.validate(form.getUsername(),form.getPassword());        
    }

}
UserLogic.java
package org.mstar.strutsajax.ajax;

/**/ /**
 * @author matianyi
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 
*/

public   class  UserLogic  {
    
public boolean validate(String username,String password){
        
if("mty".equals(username)&&"123".equals(password)){
            
return true;
        }
 else {
            
return false;
        }

    }

}

LoginForm.java
package org.mstar.strutsajax.form;

import org.apache.struts.action.ActionForm;

/**/ /**
 * @author matianyi
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 
*/

public   class  LoginForm extends ActionForm  {
    
private String username;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值