⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 modelviewaction.java

📁 Java/J2EE框架Jdon-Framework系统的Sample
💻 JAVA
字号:
/**
 * Copyright 2005 Jdon.com
 * 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;

import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.jdon.util.Debug;

import com.jdon.controller.model.Model;
import com.jdon.model.ModelForm;
import com.jdon.model.ModelManager;
import com.jdon.controller.WebAppUtil;
import com.jdon.strutsutil.util.CreateViewPage;
import com.jdon.strutsutil.util.EditeViewPage;
import com.jdon.model.ModelManagerRegistry;
import javax.servlet.ServletContext;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.config.FormBeanConfig;
import org.apache.struts.util.RequestUtils;
import com.jdon.controller.ContainerClient;


/**
 * Model内容View类
 * 用于准备修改或删除前的显示,不适合专门显示,没有缓存。
 *
 * 根据调用参数两种情况:
 * 1. 无Action参数,或action参数是create,无其它任何主键:新增;
 * 2. 有Action参数为edit,是编辑;
 *
 *
 *
 * 控制页面输出
 * 根据调用参数action,判断是输出编辑页面还是新增页面
 * struts_config.xml配置如下:
 *
 * <action attribute="productForm" type="com.jdon.strutsutil.ModelViewAction" validate="false" scope="request" path="/admin/productAction">
 *      <forward name="create" path="/customer.jsp" />
 *      <forward name="edit|add|remove" path="/customer.jsp" />
 * </action>
 *
 * 需要设定ApplicationResources中
 * id.notfound =未发现本记录
 *
 * <p>Copyright: Jdon.com Copyright (c) 2003</p>
 * <p></p>
 * @author banq
 * @version 1.0
 */
public class ModelViewAction extends Action {

  private final static String module = ModelViewAction.class.getName();
  private ContainerClient containerClient = new ContainerClient();

  protected ModelManager modelManager;
  protected CreateViewPage createViewPage;
  protected EditeViewPage editeViewPage;


  public ActionForward execute(ActionMapping actionMapping,
                               ActionForm actionForm,
                               HttpServletRequest request,
                               HttpServletResponse response) throws
      Exception {

    if (modelManager == null){
      ServletContext sc = this.getServlet().getServletContext();
      modelManager = (ModelManager)containerClient.getPOJOService(ModelManagerRegistry.MM_NAME, sc);
      createViewPage = new CreateViewPage(modelManager);
      editeViewPage = new EditeViewPage(modelManager);
    }

    ModuleConfig moduleConfig =   RequestUtils.getModuleConfig(request, getServlet().getServletContext());

    String action = request.getParameter("action");
    if ( (action == null) || action.equalsIgnoreCase(ModelForm.CREATE_STR)) {
      Debug.logVerbose("--> enter create process ", module);
      createViewPage.doCreate(actionMapping, actionForm, request, moduleConfig);
      return actionMapping.findForward(ModelForm.CREATE_STR);
    } else if (!FormBeanUtil.validateAction(action, actionMapping))
      throw new Exception("this action:" + action + " is invalid!");
    else {
      Debug.logVerbose("--> enter " + action + " process ", module);
      Model model = editeViewPage.getModelForEdit(actionMapping, actionForm,
                                                request, moduleConfig);
      if (model == null) { //查询失败
        if (actionMapping.findForward(FormBeanUtil.FORWARD_FAILURE_NAME) != null)
          return actionMapping.findForward(FormBeanUtil.FORWARD_FAILURE_NAME);
        else {
          ActionErrors errors = new ActionErrors();
          ActionError error = new ActionError("id.notfound");
          errors.add(ActionErrors.GLOBAL_ERROR, error);
          saveErrors(request, errors);
          return actionMapping.findForward(action);
        }
      } else
        return actionMapping.findForward(action);
    }
  }

}

⌨️ 快捷键说明

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