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

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

界面显示效果如下:

图6.3.2.1 修改项目图

图6.3.2.2 添加项目图

图6.3.2.3 删除确认图

最后,信息新增、修改或删除确认后,需要提交给ProjectFormSaveAction进行,ProjectFormSaveAction内容如下:

package com.ort.strutsdemo.simpledemo.controller;

 

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.boss.module.operation.object.Site;

import com.ort.strutsdemo.simpledemo.Constants;

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

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

import com.ipbs.util.web.ParamUtils;

/**

 *

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

 * <p>Description: 项目管理功能页面的导向,以及页面所需要数据的初始化,同时,处理删除操作</p>

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

 * <p>Company: </p>

 * @author Liuz

 * @version 1.0

 */

public class ProjectFormSaveAction

    extends Action {

  BusinessDelegate businessDelegate = null;

  public ActionForward execute(ActionMapping actionMapping,

                               ActionForm actionForm,

                               HttpServletRequest request,

                               HttpServletResponse response) {

    businessDelegate = BusinessDelegate.getInstance();

    ProjectForm form = (ProjectForm)actionForm;

    ActionForward forward = null;

    String action = form.getAction();

    if (this.isCancelled(request)) {

        return (actionMapping.findForward("cancel"));

    }

    if("modify".equals(action)){

        forward = modifyProject(actionMapping, actionForm,

                                           request, response);

    }

    else if ("create".equals(action)) {

      forward = createProject(actionMapping, actionForm,request, response);

    }else if("delete".equals(action)){

       forward = deleteProject(actionMapping, actionForm,request, response);

    }

    return forward;

  }

 

 

  public ActionForward modifyProject(ActionMapping actionMapping,

                                     ActionForm actionForm,

                                    HttpServletRequest request,

                                    HttpServletResponse response

                                    )

{

     ProjectForm form = (ProjectForm) actionForm;

     String projectCode = form.getProjectCode();

     String projectName = form.getProjectName();

     String siteCode = form.getSiteCode();

     double discount = form.getDiscount();

      try{

          Site site = businessDelegate.getSite(siteCode);

          Project project = new Project();

          project.setProjectCode(projectCode);

          project.setProjectName(projectName);

          project.setDiscount(discount);

          project.setSite(site);

          businessDelegate.modifyProject(project);

          return actionMapping.findForward("success");

      }catch(Exception ex){

          com.ipbs.util.Log.println(

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

          ExceptionBean exception = new ExceptionBean();

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

          exception.setReturnPath(actionMapping.getPath());

          request.setAttribute(Constants.EXCEPTION_BEAN, exception);

          return actionMapping.findForward("failed");

      }

  }

 

  public ActionForward createProject(ActionMapping actionMapping,

                                     ActionForm actionForm,

                                    HttpServletRequest request,

                                    HttpServletResponse response

                                    ) {

     ProjectForm form = (ProjectForm) actionForm;

     String projectCode = form.getProjectCode();

     String projectName = form.getProjectName();

     String siteCode = form.getSiteCode();

     double discount = form.getDiscount();

     try{

         Site site = businessDelegate.getSite(siteCode);

         Project project = new Project();

         project.setProjectCode(projectCode);

         project.setProjectName(projectName);

         project.setDiscount(discount);

         project.setSite(site);

         businessDelegate.createProject(project);

         return actionMapping.findForward("success");

     }catch(Exception ex){

         com.ipbs.util.Log.println(

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

          ExceptionBean exception = new ExceptionBean();

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

          exception.setReturnPath(actionMapping.getPath());

          request.setAttribute(Constants.EXCEPTION_BEAN, exception);

          return actionMapping.findForward("failed");

     }

  }

 

  public ActionForward deleteProject(ActionMapping actionMapping,

                                     ActionForm actionForm,

                                    HttpServletRequest request,

                                    HttpServletResponse response

                                    ) {

     ProjectForm form = (ProjectForm)actionForm;

     String projectCode = form.getProjectCode();

     try{

         businessDelegate.deleteProject(projectCode);

         request.removeAttribute(Constants.PROJECT_SINGLEFORM_KEY);

         return actionMapping.findForward("success");

     }catch(Exception ex){

         com.ipbs.util.Log.println(

             "[ProjectControllerAction.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");

 

     }

  }

 

}

至此为止,该示例项目所使用的绝大部分内容已经介绍完了,因为时间问题,没有对示例程序进行很详细的一一讲解,代码上有些简单的注释,希望你能够看明白,如果大家对以上代码有疑问会发现什么问题,可以发邮件和我沟通([email protected]),另外,我保证上面的代码是真实可运行的,如果用心看,也是可以看明白的(当然只有文章中所列的代码是无法通过编译的)。

个人觉得,做Struts项目非常重要的一个环节就是请求处理流程设计,这也是本示例强调的重点,弄清楚了这一点,看代码也会容易很多。

1.1.1           修改项目信息

请求处理流程如下,具体文件内容请查看前述内容:

1、  ProjectListSearchAction

2、  projectlist.jsp

3、  ProjectFormLoadAction

4、  projectform.jsp

5、  ProjectFormSaveAction

6、  success.jsp or failed.jsp

7、  ProjectListSearchAction

 

1.1.2           删除项目信息

请求处理流程如下,具体文件内容请查看前述内容:

1、  ProjectListSearchAction

2、  projectlist.jsp

3、  ProjectFormLoadAction

4、  projectform.jsp

5、  ProjectFormSaveAction

6、  success.jsp or failed.jsp

7、  ProjectListSearchAction

 

2.     附录

无。

3.     索引

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