上一篇我主要介绍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;
    
private String password;

    
/**
     * @return Returns the password.
     
*/

    
public String getPassword() {
        
return password;
    }

    
/**
     * @param password The password to set.
     
*/

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

    
/**
     * @return Returns the username.
     
*/

    
public String getUsername() {
        
return username;
    }

    
/**
     * @param username The username to set.
     
*/

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

}

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


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

public class TableRowBean{
    
private String col1Value;
    
private String col2Value;
    
private String col3Value;
    

    
/**
     * @return Returns the col1Value.
     
*/

    
public String getCol1Value() {
        
return col1Value;
    }

    
/**
     * @param col1Value The col1Value to set.
     
*/

    
public void setCol1Value(String col1Value) {
        
this.col1Value = col1Value;
    }

    
/**
     * @return Returns the col2Value.
     
*/

    
public String getCol2Value() {
        
return col2Value;
    }

    
/**
     * @param col2Value The col2Value to set.
     
*/

    
public void setCol2Value(String col2Value) {
        
this.col2Value = col2Value;
    }

    
/**
     * @return Returns the col3Value.
     
*/

    
public String getCol3Value() {
        
return col3Value;
    }

    
/**
     * @param col3Value The col3Value to set.
     
*/

    
public void setCol3Value(String col3Value) {
        
this.col3Value = col3Value;
    }

}
上面的代码都比较简单,不用说大家也都知道是干什么用的。
下面就是主要的内容了。预知后事如何,且听下回分解。