Struts快速学习指南19(内部培训教材)-大部分素材来自于《Programming Jakarta Struts》一书

类别:Java 点击:0 评论:0 推荐:

 

1.1  CRUD

CRUD(Create,Retrieve or Read?,Update,Delete)是信息管理系统中经常要执行的操作的简称。接下来我们分别描述这几种操作的执行流程。

1.1.1           查询项目信息

执行项目信息查询列表显示的文件请求处理顺序如下:

1、  ProjectListSearchAction

2、  projectlist.jsp

 

ProjectListSearchAction代码如下:

package com.ort.strutsdemo.simpledemo.controller;

 

/**

 * <p>Title: Struts Training </p>

 * <p>Description: Struts内部培训Demo</p>

 * <p>Copyright: Copyright (c) 2004</p>

 * <p>Company: </p>

 * @author Liuz

 * @version 1.0

 */

import org.apache.struts.action.*;

import javax.servlet.http.*;

import com.ort.strutsdemo.simpledemo.business.BusinessDelegate;

import com.boss.module.operation.object.Project;

import com.ort.strutsdemo.simpledemo.Constants;

import com.ort.strutsdemo.simpledemo.controller.exception.ExceptionBean;

import com.boss.module.operation.object.searchresult.help.ResultSetIterator;

import com.ort.strutsdemo.simpledemo.ui.ProjectForm;

import com.boss.module.operation.object.searchgene.ProjectSearchGene;

 

/**

 *

 * <p>Title: Struts Training </p>

 * <p>Description: 项目查询结果数据的装载</p>

 * <p>Copyright: Copyright (c) 2004</p>

 * <p>Company: ORT</p>

 * @author Liuz

 * @version 1.0

 */

public class ProjectListSearchAction

    extends Action {

    BusinessDelegate businessDelegate = null;

    public ActionForward execute(ActionMapping actionMapping,

                                 ActionForm actionForm,

                                 HttpServletRequest request,

                                 HttpServletResponse response) {

        try {

            DynaActionForm form = (DynaActionForm) actionForm;

            //定义分页所需要变量

            int page= ((Integer)form.get("page")).intValue();

            int pageSize = 5;

            int allSize = 0;

            int pageCount = 0;

            //获取用户输入查询值,并形成查询条件

            String projectCode = (String)form.get("projectCode");

            String projectCodeSign = (String)form.get("projectCodeSign");

            String projectName = (String)form.get("projectName");

            String projectNameSign = (String)form.get("projectNameSign");

            ProjectSearchGene searchGene = new ProjectSearchGene(); //searchGene为一查询精灵,用以处理查询操作,在此不用过多关注,有机会在另文介绍

            searchGene.setProjectCode(projectCode, projectCodeSign);

            searchGene.setProjectName(projectName, projectNameSign);

 

 

            //装载当前页面所需要显示项目列表数据

             BusinessDelegate businessDelegate = BusinessDelegate.getInstance(); //业务层操作,不用关注

             ResultSetIterator projectIterator = businessDelegate.

                 getProjectIterator(searchGene,pageSize);

             java.util.List projects = projectIterator.getElements(page);

             if (projects == null) {

                 projects = new java.util.ArrayList();

             }

             //将项目列表查询结果放置到请求对象中

            request.removeAttribute(Constants.PROJECT_LISTFORM_KEY);

            request.setAttribute(Constants.PROJECT_LISTFORM_KEY, projects);

            //计算总页数

            allSize = projectIterator.getAllSize();

            pageCount = (allSize % pageSize == 0) ? allSize / pageSize :

                allSize / pageSize + 1;

           // System.err.print(pageCount);

            form.set("pageCount", new Integer(pageCount));

 

            //装载当前页面所需要现实小区信息

            ResultSetIterator siteIterator = businessDelegate.getSitesIterator();

            java.util.List sites = siteIterator.getElements(1);

            request.getSession().setAttribute(Constants.SITE_LISTFORM_KEY,

                                              sites);

            //重定向到mapping中配置的input页面

//            return actionMapping.findForward("success");

            return actionMapping.getInputForward();

        }

        catch (Exception ex) {

            com.ipbs.util.Log.println(

                "[ProjectListSearchAction.java][Exception]:" + ex.getMessage());

            ExceptionBean exception = new ExceptionBean();

            exception.setErrorMsg(Constants.getExceptionMsg(ex));

            exception.setReturnPath(actionMapping.findForward("failed.return").

                                    getPath());

            request.setAttribute(Constants.EXCEPTION_BEAN, exception);

            return actionMapping.findForward("failed");

        }

    }

}

在上面的代码中,应用了一个常量类(Constants),为了让大家看得更加明白,将其代码显示如下:

package com.ort.strutsdemo.simpledemo;

 

/**

 * <p>Title: Struts Training </p>

 * <p>Description: Struts内部培训Demo</p>

 * <p>Copyright: Copyright (c) 2004</p>

 * <p>Company: </p>

 * @author Liuz

 * @version 1.0

 */

 

public class Constants {

  public static final String PROJECT_SINGLEFORM_KEY = "projectForm";

  public static final String PROJECT_LISTFORM_KEY = "PROJECTS";

  public static final String PROJECT_SEARCHFORM_KEY = "SEARCHPROJECTS";

  public static final String PROJECT_CONTROLLERFORM_KEY = "PROJECTCONTROLLER";

  public static final String SITE_LISTFORM_KEY = "SITES";

  public static final String SITE_SINGLEFORM_KEY = "SITE";

 

  public static final String EXCEPTION_BEAN = "EXCEPTIONBEAN";

  /**

   * 通过识别异常的基础类型,返回易懂的提示信息

   * @param ex Exception 异常

   * @return String 异常的描述信息

   */

  public static final String getExceptionMsg(Exception ex){

      String returnMsg="";

      if(ex!=null){

          if (ex instanceof com.boss.module.operation.command.exception.AlreadyExistException){

              returnMsg = "数据已存在!";

              return returnMsg;

          }else if (ex instanceof com.boss.module.operation.command.exception.DbException){

              returnMsg = "数据库错误!";

              return returnMsg;

          }else if(ex instanceof com.boss.module.operation.command.exception.InvalidObjectException){

              returnMsg = "无效的数据!";

              return returnMsg;

          }else if(ex instanceof com.boss.module.operation.command.exception.InvalidPkException){

              returnMsg = "无效的主键!";

              return returnMsg;

          }else if(ex instanceof com.boss.module.operation.command.exception.NotFoundException){

              returnMsg = "数据不存在!";

              return returnMsg;

          }else if(ex instanceof com.boss.module.operation.command.exception.UnAuthorizationException){

              returnMsg = "无权限!";

              return returnMsg;

          }else if(ex instanceof com.boss.module.operation.object.searchresult.exception.IteratorException){

              returnMsg = "获取列表数据异常!";

              return returnMsg;

          }

      }

    return returnMsg;

  }

}

 

本文地址:http://com.8s8s.com/it/it16526.htm