上一篇我主要介紹dwr的概況。這一篇我用dwr做了個可以不刷新頁面就更新的表格。

運(yùn)行環(huán)境: windows xp pro sp2 j2sdk1.2.4_03 weblogic8.1 struts1.2.4 開發(fā)工具eclipse3.0 其實dwr和struts沒有什么關(guān)系,只不過最近我們項目組在用struts作東西。我就順便用把我的程序建立在Struts上。 主要文件。 dwr.jar--dwr的類庫包 struts的類庫包,具體我不說了,這東西誰都知道。 jdts0.9.jar--數(shù)據(jù)庫SQLServer的驅(qū)動程序包。 以上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 --登陸成功會轉(zhuǎn)到這個頁面,一個ajax表格。 showtable.js -- showtable.jsp中用到的javascript main.css -- 不說了 還有 *.gif界面要到的圖片 以上文件放在WebContent下 剩下的就是java類了。 LoginAction.java --Struts的Action,負(fù)責(zé)登陸 TableAction.java --Struts的Action,負(fù)責(zé)表格內(nèi)容初始化 UserLogic.java --負(fù)責(zé)驗證用戶 TableRowConverter.java -- 繼承于dwr的BeanConverter,負(fù)責(zé)將一個對象轉(zhuǎn)成javascript能用的東西。 LoginForm.java --Struts的Form,負(fù)責(zé)登陸信息 TableModelBean.java --TableModel一部分給struts用一部分給dwr用。 TableRowBean.java 用戶存放行信息的Bean。 ModelOneDAO.java --隨便取的名字,有點(diǎn)惡(三聲)。負(fù)責(zé)從數(shù)據(jù)庫操作的。
這個例子還需要一個數(shù)據(jù)庫,我用的是SQLServer。 下面是建表的SQL語句。輸入數(shù)據(jù)的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
接下來是寫業(yè)務(wù)邏輯 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#e xecute(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
|
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;
????}
}上面的代碼都比較簡單,不用說大家也都知道是干什么用的。
下面就是主要的內(nèi)容了。預(yù)知后事如何,且聽下回分解。
posted on 2006-05-18 13:48
飛鳥 閱讀(1000)
評論(0) 編輯 收藏 所屬分類:
AJAX