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

📄 informationaction.java

📁 本例采用sql server数据库。基于JBuilder的tomcat上运行。访问数据库基于struts的datasources.里面有详细的操作和设置说明。
💻 JAVA
字号:
//---------------------------------------------------------
// Application: Gsm of Application
// Author     : esingle
// File       : InformationAction.java
//
// Copyright 2004 landsoft corp
// Generated at Wed Mar 10 15:35:57 CST 2004
// created by 曹广鑫
// mailto:gxcao@mail.tsinghua.edu.cn
//---------------------------------------------------------


package com.landsoft.gsm.controller;

import javax.sql.*;
import java.util.*;

import org.apache.commons.logging.LogFactory;


import javax.servlet.http.*;
import javax.servlet.*;

import org.apache.struts.action.*;
import org.apache.struts.util.*;

import com.landsoft.gsm.model.*;
import com.landsoft.gsm.dao.*;
import com.landsoft.gsm.util.*;

public class InformationAction extends Action {
  private org.apache.commons.logging.Log __log = LogFactory.getFactory().getInstance(this.getClass());
  private static final int PAGE_LENGTH = 20;

  public ActionForward execute(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response) {
    ActionForward myforward = null;
    String myaction = mapping.getParameter();

    if (isCancelled(request)) {
      if (__log.isInfoEnabled()) {
        __log.info(" [Information] " + mapping.getAttribute() + " - action was cancelled");
      }
      return mapping.findForward("cancel");
    }
    if (__log.isInfoEnabled()) {
      __log.info(" [Information] action: "+myaction);
    }
    if ("".equalsIgnoreCase(myaction)) {
      myforward = mapping.findForward("failure");
    } else if ("VIEW".equalsIgnoreCase(myaction)) {
      myforward = performView(mapping, form, request, response);
    } else if ("EDIT".equalsIgnoreCase(myaction)) {
      myforward = performEdit(mapping, form, request, response);
    } else if ("ADD".equalsIgnoreCase(myaction)) {
      myforward = performAdd(mapping, form, request, response);
    } else if ("SAVE".equalsIgnoreCase(myaction)) {
      myforward = performSave(mapping, form, request, response);
    } else if ("REMOVE".equalsIgnoreCase(myaction)) {
      myforward = performRemove(mapping, form, request, response);
    } else if ("TRASH".equalsIgnoreCase(myaction)) {
      myforward = performTrash(mapping, form, request, response);
    } else if ("LIST".equalsIgnoreCase(myaction)) {
      myforward = performList(mapping, form, request, response);
    } else if ("NEWQUERY".equalsIgnoreCase(myaction)) {
      myforward = performnewQuery(mapping, form, request, response);
    } else {
      myforward = mapping.findForward("failure");
    }
    return myforward;
  }


  private ActionForward performList(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
    try {
      DataSource ds = (DataSource) servlet.getServletContext().getAttribute(Action.DATA_SOURCE_KEY);

      InformationDAO informationDAO = new InformationDAO(ds);

      int offset;
      int length = PAGE_LENGTH;
      String pageOffset = request.getParameter("pager.offset");
      if (pageOffset == null || pageOffset.equals("")) {
        offset = 0;
      } else {
        offset = Integer.parseInt(pageOffset);
      }

      List informations = informationDAO.list(offset, length);

      String[] objKeys = {"Information", "list"};
      String objKey = CacheManager.createKey(objKeys);
      Integer size = (Integer)SizeCacheManager.getCache(objKey);
      if(size == null) {
        size = new Integer(informationDAO.getSize("information", ""));
        SizeCacheManager.putCache(size, objKey, 1);
      }

      String url = request.getContextPath()+"/"+mapping.getPath()+".do";
      String pagerHeader = Pager.generate(offset, size.intValue(), length, url);
      request.setAttribute("pagerHeader", pagerHeader);

      request.setAttribute("INFORMATIONS", informations);
    } catch (Exception e) {
      generalError(request, e);
      return mapping.findForward("failure");
    }

    return mapping.findForward("success");
  }

  private ActionForward performView(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
    InformationForm form = (InformationForm) actionForm;
    try {
      DataSource ds = (DataSource) servlet.getServletContext().getAttribute(Action.DATA_SOURCE_KEY);

      InformationDAO informationDAO = new InformationDAO(ds);

      long infoid = Long.parseLong(request.getParameter("infoid"));
      Information information = informationDAO.retrieve(infoid);
      if (information == null) {
        ActionErrors aes = new ActionErrors();
        aes.add(aes.GLOBAL_ERROR, new ActionError("error.object.notfound", "Information"));
        saveErrors(request, aes);
        if (__log.isErrorEnabled()) {
          __log.error(" [Information] Object not found");
        }
      } else {
        org.apache.commons.beanutils.BeanUtils.populate(form, org.apache.commons.beanutils.BeanUtils.describe(information));
      }
    } catch (Exception e) {
      generalError(request, e);
      return mapping.findForward("failure");
    }
    return mapping.findForward("success");
  }

  private ActionForward performSave(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
    InformationForm form = (InformationForm) actionForm;

    try {
      DataSource ds = (DataSource) servlet.getServletContext().getAttribute(Action.DATA_SOURCE_KEY);

      InformationDAO informationDAO = new InformationDAO(ds);

      Information information = new Information();
      org.apache.commons.beanutils.BeanUtils.populate(information, org.apache.commons.beanutils.BeanUtils.describe(form));
      int strutsAction = form.getStrutsAction();
      if (strutsAction == InformationForm.ADD) {
        long infoid = information.getInfoid();
        if (informationDAO.retrieve(infoid) == null) {
          informationDAO.insert(information);
        } else {
          sqlDuplicateError(request, "Information");
          return mapping.findForward("failure");
        }
      } else if (strutsAction == InformationForm.EDIT) {
        informationDAO.update(information);
      }
    } catch (Exception e) {
      generalError(request, e);
      return mapping.findForward("failure");
    }
    return mapping.findForward("success");
  }

  private ActionForward performEdit(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
    InformationForm form = (InformationForm) actionForm;
    form.setStrutsAction(InformationForm.EDIT);
    try {
      DataSource ds = (DataSource) servlet.getServletContext().getAttribute(Action.DATA_SOURCE_KEY);

      InformationDAO informationDAO = new InformationDAO(ds);

      long infoid = Long.parseLong(request.getParameter("infoid"));

      Information information = informationDAO.retrieve(infoid);
      org.apache.commons.beanutils.BeanUtils.populate(form, org.apache.commons.beanutils.BeanUtils.describe(information));
    } catch (Exception e) {
      generalError(request, e);
      return mapping.findForward("failure");
    }
    return mapping.findForward("success");
  }

  private ActionForward performAdd(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
    InformationForm form = (InformationForm) actionForm;
    form.setStrutsAction(InformationForm.ADD);
    return mapping.findForward("success");
  }
  private ActionForward performRemove(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
    return performView(mapping, actionForm, request, response);
  }

  private ActionForward performTrash(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
    InformationForm form = (InformationForm) actionForm;
    try {
      DataSource ds = (DataSource) servlet.getServletContext().getAttribute(Action.DATA_SOURCE_KEY);

      InformationDAO informationDAO = new InformationDAO(ds);

      long infoid = Long.parseLong(request.getParameter("infoid"));
      informationDAO.delete(infoid);
    } catch (Exception e) {
      generalError(request, e);
      return mapping.findForward("failure");
    }
    return mapping.findForward("success");
  }
  private ActionForward performnewQuery(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
    InformationForm form = (InformationForm) actionForm;
    try {
      DataSource ds = (DataSource) servlet.getServletContext().getAttribute(Action.DATA_SOURCE_KEY);

      InformationDAO informationDAO = new InformationDAO(ds);
      int offset;
      int length = PAGE_LENGTH;
      String pageOffset = request.getParameter("pager.offset");
      if (pageOffset == null || pageOffset.equals("")) {
        offset = 0;
      } else {
        offset = Integer.parseInt(pageOffset);
      }

      String mobile = form.getMobile();
      String productcode = form.getProductcode();
      String[] objKeys = {"Information", "newQuery", String.valueOf(mobile), String.valueOf(productcode)};
      String objKey = CacheManager.createKey(objKeys);
      Integer size = (Integer)SizeCacheManager.getCache(objKey);
      if(size == null) {
        size = new Integer(informationDAO.getSize("information", ""));
        SizeCacheManager.putCache(size, objKey, 1);
      }
      List informations = informationDAO.newQuery(offset, length, mobile, productcode);
      String url = request.getContextPath()+"/"+mapping.getPath()+".do"+"?mobile="+mobile+"&productcode="+productcode;
      String pagerHeader = Pager.generate(offset, size.intValue(), length, url);
      request.setAttribute("pagerHeader", pagerHeader);

      request.setAttribute("INFORMATIONS", informations);
    } catch (Exception e) {
      generalError(request, e);
      return mapping.findForward("failure");
    }

    return mapping.findForward("success");
  }



  private void sqlDuplicateError(HttpServletRequest request, String objName) {
    ActionErrors aes = new ActionErrors();
    aes.add(aes.GLOBAL_ERROR, new ActionError("errors.database.duplicate", objName));
    saveErrors(request, aes);
    if (__log.isErrorEnabled()) {
      __log.error(" [Information] Duplicate key Error - " + objName);
    }
  }

  private void generalError(HttpServletRequest request, Exception e) {
    ActionErrors aes = new ActionErrors();
    aes.add(aes.GLOBAL_ERROR, new ActionError("error.general", e.getMessage()));
    saveErrors(request, aes);
    e.printStackTrace();
    if (__log.isErrorEnabled()) {
      __log.error(" [Information] Error - " + e.getMessage());
    }
  }
}

⌨️ 快捷键说明

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