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
java 代码
- //接下来是写业务逻辑
- 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;
- }
- }
- //上面的代码都比较简单,不用说大家也都知道是干什么用的。
java 代码
- TableModelBean.java 这是核心业务类,既要被Action使用又要被dwr使用。
- 由于我注释写了一些,所以就不详细介绍了
- public class TableModelBean {
- //表格的第一列
- public static final int COLUMN_1 = 0;
- //表格的第二列
- public static final int COLUMN_2 = 1;
- //表格的第三列
- public static final int COLUMN_3 = 2;
- //每一列的排序升序降序标记 true升序,false降序
- private boolean[] columnFlags = { false, false, false };
- //表格分页总页面数
- private int totalPage = 0;
- //表格当前页
- private int currentPage = 0;
- //表格总行数
- private int rowsCount = 0;
- //没用
- private String[] pagers = { "" };
- //存放全体记录的容器
- private List rows = new ArrayList();
- //存放当前记录的容器
- private List currentPageRows = new ArrayList();
- //数据库操作类
- private static ModelOneDAO dao;
- //每页记录数设为20
- private static final int PAGE_SIZE = 20;
- //初始排序行为第一行
- private int sortedColumn = 1;
- /**//**
- * 构造函数
- */
- public TableModelBean() {
- dao = new ModelOneDAO();
- init();
- }
- /**//**
- * 初始化
- */
- private void init() {
- try {
- rows = dao.getSortedRows(sortedColumn, columnFlags[sortedColumn]);
- setRowsCount(rows.size());
- setTotalPage(getTotalPageByRow(rows.size(), PAGE_SIZE));
- setCurrentPage(1);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**//**
- * 返回当前页的内容
- * @return Returns the currentPage.
- */
- public int getCurrentPage() {
- return currentPage;
- }
- /**//**
- * 设置当前页
- * @param currentPage
- * The currentPage to set.
- */
- public void setCurrentPage(int currentPage) {
- this.currentPage = currentPage;
- currentPageRows.clear();
- int firstIndex = PAGE_SIZE * (currentPage - 1);
- int lastIndex = (firstIndex + PAGE_SIZE) < rowsCount ? firstIndex
- + PAGE_SIZE : rowsCount;
- for (int i = firstIndex; i < lastIndex; i++) {
- currentPageRows.add(rows.get(i));
- }
- }
- /**//**
- * 取得所有行
- * @return Returns the rows.
- */
- public List getRows() {
- return rows;
- }
- /**//**
- * 取的分页数
- * @return Returns the totalPage.
- */
- public int getTotalPage() {
- init();
- return totalPage;
- }
- /**//**
- * 设置分页数
- * @param totalPage
- * The totalPage to set.
- */
- public void setTotalPage(int totalPage) {
- this.totalPage = totalPage;
- }
- /**//**
- * 取得纪录数
- * @return Returns the totalRows.
- */
- public int getRowsCount() {
- return rowsCount;
- }
- /**//**
- * 设置记录数
- * @param totalRows
- * The totalRows to set.
- */
- public void setRowsCount(int rowsCount) {
- this.rowsCount = rowsCount;
- }
- /**//**
- * 取得当前页中的记录数
- * @return Returns the currentPageRows.
- */
- public List getCurrentPageRows() {
- return currentPageRows;
- }
- /**//**
- * 取得page页中的记录,当page大于totalPage时返回最后页
- * 因为是上面的getCurrentPageRows函数的重载,所以在dwr中不能正常使用。
- * 于是出现了getRowsByPageNo方法。
- * @param page
- * @return the currentPageRows.
- */
- public List getCurrentPageRows(int page) {
- currentPageRows.clear();
- int firstIndex = PAGE_SIZE * (page - 1);
- int lastIndex = (firstIndex + PAGE_SIZE) < rowsCount ? firstIndex
- + PAGE_SIZE : rowsCount;
- for (int i = firstIndex; i < lastIndex; i++) {
- currentPageRows.add(rows.get(i));
- }
- return currentPageRows;
- }
- /**//**
- * 取得page页中的记录,当page大于totalPage时返回最后页
- * @param page
- * @return 包含当前页记录的List
- */
- public List getRowsByPageNo(int page) {
- init();
- page = page > totalPage ? totalPage : page;
- List result = new ArrayList();
- int firstIndex = PAGE_SIZE * (page - 1);
- int lastIndex = (firstIndex + PAGE_SIZE) < rowsCount ? firstIndex
- + PAGE_SIZE : rowsCount;
- for (int i = firstIndex; i < lastIndex; i++) {
- result.add(rows.get(i));
- }
- return result;
- }
- /**//**
- * 按照某一列进行排序,再返回当前页中的数据
- * @param currentPage
- * @param columnNo
- * @return the Rows of current Page that sorted by columnNo
- */
- public List getCurrentPageSortedByColumnRows(int currentPage, int columnNo) {
- init();
- sortBy(columnNo);
- currentPageRows.clear();
- int firstIndex = 20 * (currentPage - 1);
- int lastIndex = (firstIndex + 20) < rowsCount ? firstIndex + 20
- : rowsCount;
- for (int i = firstIndex; i < lastIndex; i++) {
- currentPageRows.add(rows.get(i));
- }
- return currentPageRows;
- }
- /**//**
- * 返回一个分页数组。用处不太大,客户端用Javascript也可以计算。
- * @return Returns the pages.
- */
- public String[] getPagers() {
- pagers = new String[totalPage];
- for (int i = 1; i <= totalPage; i++) {
- pagers[i - 1] = i + "";
- }
- return pagers;
- }
- /**//**
- * 按照某一列进行排序
- * @param columnNo
- */
- public void sortBy(int columnNo) {
- this.sortedColumn = columnNo;
- columnFlags[columnNo] = (!columnFlags[columnNo]);
- try {
- rows = dao.getSortedRows(columnNo, columnFlags[columnNo]);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**//**
- * 删除某一列,按照主键(第一列)
- * @param key
- * @return
- */
- public boolean deleteRow(int key) {
- try {
- dao.deleteRow(key);
- } catch (SQLException e) {
- e.printStackTrace();
- return false;
- }
- return true;
- }
- /**//**
- * 要新增加一个数据前先计算出Id.
- * 这个例子只是用来演示用的,如果多人访问会出现并发问题
- * @return
- */
- public int getNextId() {
- try {
- return dao.getNextId();
- } catch (SQLException e) {
- e.printStackTrace();
- return -1;
- }
- }
- /**//**
- * 增加一行
- * @param trb
- * @return
- */