dwr简介--一个例子[转]

sql 代码
  1. /*==============================================================*/   
  2. /* DBMS name:      Microsoft SQL Server 2000                    */   
  3. /* Created on:     2005-8-1 13:21:33                            */   
  4. /*==============================================================*/   
  5.   
  6.   
  7. if exists (select 1   
  8.             from  sysobjects   
  9.            where  id = object_id('AJAX_MODEL_ONE')   
  10.             and   type = 'U')   
  11.    drop table AJAX_MODEL_ONE   
  12. go   
  13.   
  14.   
  15. /*==============================================================*/   
  16. /* Table: AJAX_MODEL_ONE                                        */   
  17. /*==============================================================*/   
  18. create table AJAX_MODEL_ONE (   
  19.    col1                 int                  not null,   
  20.    col2                 int                  not null,   
  21.    col3                 int                  not null,   
  22.    constraint PK_AJAX_MODEL_ONE primary key  (col1)   
  23. )   
  24. go   

 

 

java 代码
  1. //接下来是写业务逻辑   
  2.   
  3. Login.java   
  4. /**//*  
  5.  * Created on 2005-7-29  
  6.  *  
  7.  * TODO To change the template for this generated file go to  
  8.  * Window - Preferences - Java - Code Style - Code Templates  
  9.  */  
  10. package org.mstar.strutsajax.action;   
  11.   
  12. import javax.servlet.http.HttpServletRequest;   
  13. import javax.servlet.http.HttpServletResponse;   
  14.   
  15. import org.apache.struts.action.Action;   
  16. import org.apache.struts.action.ActionForm;   
  17. import org.apache.struts.action.ActionForward;   
  18. import org.apache.struts.action.ActionMapping;   
  19. import org.mstar.strutsajax.ajax.UserLogic;   
  20. import org.mstar.strutsajax.form.LoginForm;   
  21.   
  22. /**//**  
  23.  * @author matianyi  
  24.  *  
  25.  * TODO To change the template for this generated type comment go to  
  26.  * Window - Preferences - Java - Code Style - Code Templates  
  27.  */  
  28. public class LoginAction extends Action {   
  29.   
  30.     /**//* (non-Javadoc)  
  31.      * @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)  
  32.      */  
  33.     public ActionForward execute(ActionMapping mapping, ActionForm form,   
  34.             HttpServletRequest request, HttpServletResponse response) throws Exception {   
  35.         if(validateUser((LoginForm)form)){   
  36.             return mapping.findForward("success");               
  37.         } else {   
  38.             return mapping.findForward("failure");   
  39.         }           
  40.     }   
  41.        
  42.     private boolean validateUser(LoginForm form){   
  43.         UserLogic userLogic = new UserLogic();   
  44.         return userLogic.validate(form.getUsername(),form.getPassword());           
  45.     }   
  46. }UserLogic.java package org.mstar.strutsajax.ajax;   
  47.   
  48. /**//**  
  49.  * @author matianyi  
  50.  *  
  51.  * TODO To change the template for this generated type comment go to  
  52.  * Window - Preferences - Java - Code Style - Code Templates  
  53.  */  
  54. public class UserLogic {   
  55.     public boolean validate(String username,String password){   
  56.         if("mty".equals(username)&&"123".equals(password)){   
  57.             return true;   
  58.         } else {   
  59.             return false;   
  60.         }   
  61.     }   
  62. }   
  63. LoginForm.java   
  64. package org.mstar.strutsajax.form;   
  65.   
  66. import org.apache.struts.action.ActionForm;   
  67.   
  68. /**//**  
  69.  * @author matianyi  
  70.  *  
  71.  * TODO To change the template for this generated type comment go to  
  72.  * Window - Preferences - Java - Code Style - Code Templates  
  73.  */  
  74. public class LoginForm extends ActionForm {   
  75.     private String username;   
  76.     private String password;   
  77.   
  78.     /**//**  
  79.      * @return Returns the password.  
  80.      */  
  81.     public String getPassword() {   
  82.         return password;   
  83.     }   
  84.     /**//**  
  85.      * @param password The password to set.  
  86.      */  
  87.     public void setPassword(String password) {   
  88.         this.password = password;   
  89.     }   
  90.     /**//**  
  91.      * @return Returns the username.  
  92.      */  
  93.     public String getUsername() {   
  94.         return username;   
  95.     }   
  96.     /**//**  
  97.      * @param username The username to set.  
  98.      */  
  99.     public void setUsername(String username) {   
  100.         this.username = username;   
  101.     }   
  102. }   
  103. TableRowBean.java   
  104. package org.mstar.strutsajax.form;   
  105.   
  106.   
  107. /**//**  
  108.  * @author matianyi  
  109.  *  
  110.  * TODO To change the template for this generated type comment go to  
  111.  * Window - Preferences - Java - Code Style - Code Templates  
  112.  */  
  113. public class TableRowBean{   
  114.     private String col1Value;   
  115.     private String col2Value;   
  116.     private String col3Value;   
  117.        
  118.   
  119.     /**//**  
  120.      * @return Returns the col1Value.  
  121.      */  
  122.     public String getCol1Value() {   
  123.         return col1Value;   
  124.     }   
  125.     /**//**  
  126.      * @param col1Value The col1Value to set.  
  127.      */  
  128.     public void setCol1Value(String col1Value) {   
  129.         this.col1Value = col1Value;   
  130.     }   
  131.     /**//**  
  132.      * @return Returns the col2Value.  
  133.      */  
  134.     public String getCol2Value() {   
  135.         return col2Value;   
  136.     }   
  137.     /**//**  
  138.      * @param col2Value The col2Value to set.  
  139.      */  
  140.     public void setCol2Value(String col2Value) {   
  141.         this.col2Value = col2Value;   
  142.     }   
  143.     /**//**  
  144.      * @return Returns the col3Value.  
  145.      */  
  146.     public String getCol3Value() {   
  147.         return col3Value;   
  148.     }   
  149.     /**//**  
  150.      * @param col3Value The col3Value to set.  
  151.      */  
  152.     public void setCol3Value(String col3Value) {   
  153.         this.col3Value = col3Value;   
  154.     }   
  155. }   
  156.   
  157. //上面的代码都比较简单,不用说大家也都知道是干什么用的。   

 

java 代码
  1. TableModelBean.java 这是核心业务类,既要被Action使用又要被dwr使用。   
  2. 由于我注释写了一些,所以就不详细介绍了   
  3. public class TableModelBean {   
  4.        
  5.     //表格的第一列   
  6.     public static final int COLUMN_1 = 0;   
  7.        
  8.     //表格的第二列   
  9.     public static final int COLUMN_2 = 1;   
  10.        
  11.     //表格的第三列   
  12.     public static final int COLUMN_3 = 2;   
  13.   
  14.     //每一列的排序升序降序标记 true升序,false降序   
  15.     private boolean[] columnFlags = { falsefalsefalse };   
  16.        
  17.     //表格分页总页面数   
  18.     private int totalPage = 0;   
  19.        
  20.     //表格当前页   
  21.     private int currentPage = 0;   
  22.        
  23.     //表格总行数   
  24.     private int rowsCount = 0;   
  25.   
  26.     //没用   
  27.     private String[] pagers = { "" };   
  28.   
  29.     //存放全体记录的容器   
  30.     private List rows = new ArrayList();   
  31.   
  32.     //存放当前记录的容器   
  33.     private List currentPageRows = new ArrayList();   
  34.   
  35.     //数据库操作类   
  36.     private static ModelOneDAO dao;   
  37.   
  38.     //每页记录数设为20   
  39.     private static final int PAGE_SIZE = 20;   
  40.   
  41.     //初始排序行为第一行   
  42.     private int sortedColumn = 1;   
  43.   
  44.     /**//**  
  45.      *  构造函数  
  46.      */  
  47.     public TableModelBean() {   
  48.         dao = new ModelOneDAO();   
  49.         init();   
  50.     }   
  51.   
  52.     /**//**  
  53.      *  初始化  
  54.      */  
  55.     private void init() {   
  56.         try {   
  57.             rows = dao.getSortedRows(sortedColumn, columnFlags[sortedColumn]);   
  58.             setRowsCount(rows.size());   
  59.             setTotalPage(getTotalPageByRow(rows.size(), PAGE_SIZE));   
  60.             setCurrentPage(1);   
  61.         } catch (SQLException e) {   
  62.             // TODO Auto-generated catch block   
  63.             e.printStackTrace();   
  64.         }   
  65.     }   
  66.   
  67.     /**//**  
  68.      * 返回当前页的内容  
  69.      * @return Returns the currentPage.  
  70.      */  
  71.     public int getCurrentPage() {   
  72.         return currentPage;   
  73.     }   
  74.   
  75.     /**//**  
  76.      * 设置当前页  
  77.      * @param currentPage  
  78.      *            The currentPage to set.  
  79.      */  
  80.     public void setCurrentPage(int currentPage) {   
  81.         this.currentPage = currentPage;   
  82.   
  83.         currentPageRows.clear();   
  84.         int firstIndex = PAGE_SIZE * (currentPage - 1);   
  85.         int lastIndex = (firstIndex + PAGE_SIZE) < rowsCount ? firstIndex   
  86.                 + PAGE_SIZE : rowsCount;   
  87.         for (int i = firstIndex; i < lastIndex; i++) {   
  88.             currentPageRows.add(rows.get(i));   
  89.         }   
  90.     }   
  91.   
  92.     /**//**  
  93.      * 取得所有行  
  94.      * @return Returns the rows.  
  95.      */  
  96.     public List getRows() {   
  97.         return rows;   
  98.     }   
  99.   
  100.   
  101.     /**//**  
  102.      * 取的分页数  
  103.      * @return Returns the totalPage.  
  104.      */  
  105.     public int getTotalPage() {   
  106.         init();   
  107.         return totalPage;   
  108.     }   
  109.   
  110.     /**//**  
  111.      * 设置分页数  
  112.      * @param totalPage  
  113.      *            The totalPage to set.  
  114.      */  
  115.     public void setTotalPage(int totalPage) {   
  116.         this.totalPage = totalPage;   
  117.     }   
  118.   
  119.     /**//**  
  120.      * 取得纪录数  
  121.      * @return Returns the totalRows.  
  122.      */  
  123.     public int getRowsCount() {   
  124.         return rowsCount;   
  125.     }   
  126.   
  127.     /**//**  
  128.      *    设置记录数  
  129.      *  @param totalRows  
  130.      *            The totalRows to set.  
  131.      */  
  132.     public void setRowsCount(int rowsCount) {   
  133.         this.rowsCount = rowsCount;   
  134.     }   
  135.   
  136.     /**//**  
  137.      * 取得当前页中的记录数  
  138.      * @return Returns the currentPageRows.  
  139.      */  
  140.     public List getCurrentPageRows() {   
  141.         return currentPageRows;   
  142.     }   
  143.   
  144.     /**//**  
  145.      * 取得page页中的记录,当page大于totalPage时返回最后页  
  146.      * 因为是上面的getCurrentPageRows函数的重载,所以在dwr中不能正常使用。  
  147.      * 于是出现了getRowsByPageNo方法。  
  148.      * @param page  
  149.      * @return the currentPageRows.  
  150.      */  
  151.     public List getCurrentPageRows(int page) {   
  152.         currentPageRows.clear();   
  153.         int firstIndex = PAGE_SIZE * (page - 1);   
  154.         int lastIndex = (firstIndex + PAGE_SIZE) < rowsCount ? firstIndex   
  155.                 + PAGE_SIZE : rowsCount;   
  156.         for (int i = firstIndex; i < lastIndex; i++) {   
  157.             currentPageRows.add(rows.get(i));   
  158.         }   
  159.         return currentPageRows;   
  160.     }   
  161.   
  162.     /**//**  
  163.      * 取得page页中的记录,当page大于totalPage时返回最后页  
  164.      * @param page  
  165.      * @return 包含当前页记录的List  
  166.      */  
  167.     public List getRowsByPageNo(int page) {   
  168.         init();   
  169.         page = page > totalPage ? totalPage : page;   
  170.         List result = new ArrayList();   
  171.         int firstIndex = PAGE_SIZE * (page - 1);   
  172.         int lastIndex = (firstIndex + PAGE_SIZE) < rowsCount ? firstIndex   
  173.                 + PAGE_SIZE : rowsCount;   
  174.         for (int i = firstIndex; i < lastIndex; i++) {   
  175.             result.add(rows.get(i));   
  176.         }   
  177.         return result;   
  178.     }   
  179.   
  180.     /**//**  
  181.      * 按照某一列进行排序,再返回当前页中的数据  
  182.      * @param currentPage  
  183.      * @param columnNo  
  184.      * @return the Rows of current Page that sorted by columnNo  
  185.      */  
  186.     public List getCurrentPageSortedByColumnRows(int currentPage, int columnNo) {   
  187.         init();   
  188.         sortBy(columnNo);   
  189.         currentPageRows.clear();   
  190.         int firstIndex = 20 * (currentPage - 1);   
  191.         int lastIndex = (firstIndex + 20) < rowsCount ? firstIndex + 20  
  192.                 : rowsCount;   
  193.         for (int i = firstIndex; i < lastIndex; i++) {   
  194.             currentPageRows.add(rows.get(i));   
  195.         }   
  196.         return currentPageRows;   
  197.     }   
  198.   
  199.     /**//**  
  200.      * 返回一个分页数组。用处不太大,客户端用Javascript也可以计算。  
  201.      * @return Returns the pages.  
  202.      */  
  203.     public String[] getPagers() {   
  204.         pagers = new String[totalPage];   
  205.         for (int i = 1; i <= totalPage; i++) {   
  206.             pagers[i - 1] = i + "";   
  207.         }   
  208.         return pagers;   
  209.     }   
  210.   
  211.     /**//**  
  212.      * 按照某一列进行排序  
  213.      * @param columnNo  
  214.      */  
  215.     public void sortBy(int columnNo) {   
  216.         this.sortedColumn = columnNo;   
  217.         columnFlags[columnNo] = (!columnFlags[columnNo]);   
  218.         try {   
  219.             rows = dao.getSortedRows(columnNo, columnFlags[columnNo]);   
  220.         } catch (SQLException e) {   
  221.             // TODO Auto-generated catch block   
  222.             e.printStackTrace();   
  223.         }   
  224.     }   
  225.   
  226.     /**//**  
  227.      * 删除某一列,按照主键(第一列)  
  228.      * @param key  
  229.      * @return  
  230.      */  
  231.     public boolean deleteRow(int key) {   
  232.         try {   
  233.             dao.deleteRow(key);   
  234.         } catch (SQLException e) {   
  235.             e.printStackTrace();   
  236.             return false;   
  237.         }   
  238.         return true;   
  239.     }   
  240.   
  241.     /**//**  
  242.      * 要新增加一个数据前先计算出Id.  
  243.      * 这个例子只是用来演示用的,如果多人访问会出现并发问题  
  244.      * @return  
  245.      */  
  246.     public int getNextId() {   
  247.         try {   
  248.             return dao.getNextId();   
  249.         } catch (SQLException e) {   
  250.             e.printStackTrace();   
  251.             return -1;   
  252.         }   
  253.     }   
  254.   
  255.     /**//**  
  256.      * 增加一行  
  257.      * @param trb  
  258.      * @return  
  259.      */  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值