Blog信息 |
blog名称: 日志总数:1304 评论数量:2242 留言数量:5 访问次数:7586245 建立时间:2006年5月29日 |

| |
[DWR(Ajax)]dwr简介--一个例子 软件技术, 电脑与网络
lhwork 发表于 2006/7/10 10:19:17 |
上一篇我主要介绍dwr的概况。这一篇我用dwr做了个可以不刷新页面就更新的表格。500)this.width=500'>运行环境:windows xp pro sp2j2sdk1.2.4_03weblogic8.1struts1.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 -- 登陆界面,这里我也用到了dwrshowtable.jsp --登陆成功会转到这个页面,一个ajax表格。showtable.js -- showtable.jsp中用到的javascriptmain.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_ONEgo/*==============================================================*//* 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
500)this.width=500'>500)this.width=500'>/**//*500)this.width=500'> * Created on 2005-7-29500)this.width=500'> *500)this.width=500'> * TODO To change the template for this generated file go to500)this.width=500'> * Window - Preferences - Java - Code Style - Code Templates500)this.width=500'> */500)this.width=500'>package org.mstar.strutsajax.action;500)this.width=500'>500)this.width=500'>import javax.servlet.http.HttpServletRequest;500)this.width=500'>import javax.servlet.http.HttpServletResponse;500)this.width=500'>500)this.width=500'>import org.apache.struts.action.Action;500)this.width=500'>import org.apache.struts.action.ActionForm;500)this.width=500'>import org.apache.struts.action.ActionForward;500)this.width=500'>import org.apache.struts.action.ActionMapping;500)this.width=500'>import org.mstar.strutsajax.ajax.UserLogic;500)this.width=500'>import org.mstar.strutsajax.form.LoginForm;500)this.width=500'>500)this.width=500'>500)this.width=500'>/**//**500)this.width=500'> * @author matianyi500)this.width=500'> *500)this.width=500'> * TODO To change the template for this generated type comment go to500)this.width=500'> * Window - Preferences - Java - Code Style - Code Templates500)this.width=500'> */500)this.width=500'>500)this.width=500'>public class LoginAction extends Action 500)this.width=500'>{500)this.width=500'>500)this.width=500'>500)this.width=500'> /**//* (non-Javadoc)500)this.width=500'> * @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)500)this.width=500'> */500)this.width=500'> public ActionForward execute(ActionMapping mapping, ActionForm form,500)this.width=500'>500)this.width=500'> HttpServletRequest request, HttpServletResponse response) throws Exception 500)this.width=500'>{500)this.width=500'>500)this.width=500'> if(validateUser((LoginForm)form))500)this.width=500'>{500)this.width=500'> return mapping.findForward("success"); 500)this.width=500'>500)this.width=500'> } else 500)this.width=500'>{500)this.width=500'> return mapping.findForward("failure");500)this.width=500'> } 500)this.width=500'> }500)this.width=500'> 500)this.width=500'>500)this.width=500'> private boolean validateUser(LoginForm form)500)this.width=500'>{500)this.width=500'> UserLogic userLogic = new UserLogic();500)this.width=500'> return userLogic.validate(form.getUsername(),form.getPassword()); 500)this.width=500'> }500)this.width=500'>}UserLogic.java
500)this.width=500'>package org.mstar.strutsajax.ajax;500)this.width=500'>500)this.width=500'>500)this.width=500'>/**//**500)this.width=500'> * @author matianyi500)this.width=500'> *500)this.width=500'> * TODO To change the template for this generated type comment go to500)this.width=500'> * Window - Preferences - Java - Code Style - Code Templates500)this.width=500'> */500)this.width=500'>500)this.width=500'>public class UserLogic 500)this.width=500'>{500)this.width=500'>500)this.width=500'> public boolean validate(String username,String password)500)this.width=500'>{500)this.width=500'>500)this.width=500'> if("mty".equals(username)&&"123".equals(password))500)this.width=500'>{500)this.width=500'> return true;500)this.width=500'>500)this.width=500'> } else 500)this.width=500'>{500)this.width=500'> return false;500)this.width=500'> }500)this.width=500'> }500)this.width=500'>}500)this.width=500'>LoginForm.java
500)this.width=500'>package org.mstar.strutsajax.form;500)this.width=500'>500)this.width=500'>import org.apache.struts.action.ActionForm;500)this.width=500'>500)this.width=500'>500)this.width=500'>/**//**500)this.width=500'> * @author matianyi500)this.width=500'> *500)this.width=500'> * TODO To change the template for this generated type comment go to500)this.width=500'> * Window - Preferences - Java - Code Style - Code Templates500)this.width=500'> */500)this.width=500'>500)this.width=500'>public class LoginForm extends ActionForm 500)this.width=500'>{500)this.width=500'> private String username;500)this.width=500'> private String password;500)this.width=500'>500)this.width=500'>500)this.width=500'> /**//**500)this.width=500'> * @return Returns the password.500)this.width=500'> */500)this.width=500'>500)this.width=500'> public String getPassword() 500)this.width=500'>{500)this.width=500'> return password;500)this.width=500'> }500)this.width=500'>500)this.width=500'> /**//**500)this.width=500'> * @param password The password to set.500)this.width=500'> */500)this.width=500'>500)this.width=500'> public void setPassword(String password) 500)this.width=500'>{500)this.width=500'> this.password = password;500)this.width=500'> }500)this.width=500'>500)this.width=500'> /**//**500)this.width=500'> * @return Returns the username.500)this.width=500'> */500)this.width=500'>500)this.width=500'> public String getUsername() 500)this.width=500'>{500)this.width=500'> return username;500)this.width=500'> }500)this.width=500'>500)this.width=500'> /**//**500)this.width=500'> * @param username The username to set.500)this.width=500'> */500)this.width=500'>500)this.width=500'> public void setUsername(String username) 500)this.width=500'>{500)this.width=500'> this.username = username;500)this.width=500'> }500)this.width=500'>}500)this.width=500'>TableRowBean.java
500)this.width=500'>package org.mstar.strutsajax.form;500)this.width=500'>500)this.width=500'>500)this.width=500'>500)this.width=500'>/**//**500)this.width=500'> * @author matianyi500)this.width=500'> *500)this.width=500'> * TODO To change the template for this generated type comment go to500)this.width=500'> * Window - Preferences - Java - Code Style - Code Templates500)this.width=500'> */500)this.width=500'>500)this.width=500'>public class TableRowBean500)this.width=500'>{500)this.width=500'> private String col1Value;500)this.width=500'> private String col2Value;500)this.width=500'> private String col3Value;500)this.width=500'> 500)this.width=500'>500)this.width=500'>500)this.width=500'> /**//**500)this.width=500'> * @return Returns the col1Value.500)this.width=500'> */500)this.width=500'>500)this.width=500'> public String getCol1Value() 500)this.width=500'>{500)this.width=500'> return col1Value;500)this.width=500'> }500)this.width=500'>500)this.width=500'> /**//**500)this.width=500'> * @param col1Value The col1Value to set.500)this.width=500'> */500)this.width=500'>500)this.width=500'> public void setCol1Value(String col1Value) 500)this.width=500'>{500)this.width=500'> this.col1Value = col1Value;500)this.width=500'> }500)this.width=500'>500)this.width=500'> /**//**500)this.width=500'> * @return Returns the col2Value.500)this.width=500'> */500)this.width=500'>500)this.width=500'> public String getCol2Value() 500)this.width=500'>{500)this.width=500'> return col2Value;500)this.width=500'> }500)this.width=500'>500)this.width=500'> /**//**500)this.width=500'> * @param col2Value The col2Value to set.500)this.width=500'> */500)this.width=500'>500)this.width=500'> public void setCol2Value(String col2Value) 500)this.width=500'>{500)this.width=500'> this.col2Value = col2Value;500)this.width=500'> }500)this.width=500'>500)this.width=500'> /**//**500)this.width=500'> * @return Returns the col3Value.500)this.width=500'> */500)this.width=500'>500)this.width=500'> public String getCol3Value() 500)this.width=500'>{500)this.width=500'> return col3Value;500)this.width=500'> }500)this.width=500'>500)this.width=500'> /**//**500)this.width=500'> * @param col3Value The col3Value to set.500)this.width=500'> */500)this.width=500'>500)this.width=500'> public void setCol3Value(String col3Value) 500)this.width=500'>{500)this.width=500'> this.col3Value = col3Value;500)this.width=500'> }500)this.width=500'>}上面的代码都比较简单,不用说大家也都知道是干什么用的。下面就是主要的内容了。预知后事如何,且听下回分解。 |
|
|