createviewpageutil.java

来自「一个非常好的FRAMWRK!是一个外国组织做的!不!」· Java 代码 · 共 133 行

JAVA
133
字号
/**
 * Copyright 2003-2005 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */

package com.jdon.strutsutil.util;

import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;

import com.jdon.util.Debug;

import com.jdon.controller.model.Model;
import com.jdon.model.ModelHandler;
import com.jdon.model.ModelForm;
import com.jdon.model.ModelManager;
import com.jdon.strutsutil.FormBeanUtil;
import org.apache.struts.config.ModuleConfig;


/**
 * prepare for push a  a create/insert view page,
 * we need do some ready works.
 *  
 * 
 * @author banq
 */
public class CreateViewPageUtil {
  private final static String module = CreateViewPageUtil.class.getName();

  private ModelManager modelManager;
  public CreateViewPageUtil(ModelManager modelManager) {
    this.modelManager = modelManager;
  }

  /**
   * create a ModelForm object
   * 
   * two things:
   * 1. create a ModelForm null instance
   * 2. initial ModelForm instance value,
   *    obtain a inital Model instance
   *    copy the Model instance to the ModelForm instance
   * 
   *    obtaining datas two ways:
   *    1.call ModelHandler initForm method; 
   *    2.call Modelhandler initModel method;
   * 
   * 
   * @param actionMapping ActionMapping
   * @param actionForm ActionForm
   * @param request HttpServletRequest
   * @throws Exception
   */
  public void doCreate(ActionMapping actionMapping,
                       ActionForm actionForm,
                       HttpServletRequest request,
                       ModuleConfig moduleConfig) throws Exception {
    Debug.logVerbose(" enter doCreate... ", module);
    String formName = FormBeanUtil.getFormName(actionMapping);
    ModelHandler modelHandler = modelManager.borrowtHandlerObject(formName);
    ModelForm form = null;
    try {
      form = modelHandler.initForm(request);
      if (form == null){        
          Debug.logVerbose("modelHandler no initForm" , module);
          if (actionForm != null)
            form = (ModelForm) actionForm;
          else
            form = FormBeanUtil.createModelFormNow(actionMapping, actionForm, request,
                                                   moduleConfig);
          Model model = modelHandler.initModel(request);
          //copy initial model to form , intial form;
          if (model != null)
             modelHandler.modelCopyToForm(model, form);
      }
      form.setAction(ModelForm.CREATE_STR);      
      FormBeanUtil.saveActionForm(form, actionMapping, request);
    } catch (Exception ex) {
      Debug.logError("please check struts-config.xml or modelHandler.initForm" +
                     ex, module);
      throw new Exception("System error! please call system Admin." + ex);
    } finally {
      modelManager.returnHandlerObject(modelHandler); //返回modelhandler再用
    }
  }

  /**
   * call ModelHandler's initForm
   * and save the ModelForm instance in request/session scope 
   * 
   * @param actionMapping
   * @param request
   * @return
   * @throws java.lang.Exception
   */
  private ModelForm getModelFormFromHandler(ActionMapping actionMapping,
                                          ActionForm actionForm,
                                          HttpServletRequest request) throws
      Exception {
    String formName = FormBeanUtil.getFormName(actionMapping);
    ModelHandler modelHandler = modelManager.borrowtHandlerObject(formName);
    ModelForm form = null;
    try {
      form = modelHandler.initForm(request);
      Debug.logVerbose(" active modelHandler.initForm ", module);
      FormBeanUtil.saveActionForm(form, actionMapping, request);
    } catch (Exception ex) {
      Debug.logError("please check struts-config.xml or modelHandler.initForm" +
                     ex, module);
      throw new Exception("System error! please call system Admin." + ex);
    } finally {
      modelManager.returnHandlerObject(modelHandler); //返回modelhandler再用
    }
    return form;
  }




}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?