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

📄 baseaction.java

📁 有关医院方向的开发
💻 JAVA
字号:
package com.dc.common;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;


/**
 * Action 基类
 * 
 * @author tangsz
 */
public class BaseAction extends DispatchAction
{
	private static Logger logger = Logger.getLogger(BaseAction.class);

	public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)
	{
		ActionForward forwardPage = null;
		try
		{
//			request.setCharacterEncoding("gb2312");
			forwardPage = super.execute(mapping,form,request,response);
		}
		catch (ServiceException e)
		{
			logger.error(e.toString());
			forwardPage = processServiceException(request,mapping,e);
		}
		catch (BusiException e)
		{
			logger.error(e.toString());
			forwardPage = processBusiExceptions(request,mapping,e);
		}
		
		catch (Throwable t)
		{
			logger.error(t.toString());
			StringBuffer sb = new StringBuffer();
			StackTraceElement[] ste = t.getStackTrace();
			for (int i = 0; i < ste.length; i++)
			{
				sb.append("\t").append(ste[i]).append("\n");
			}
			logger.error(sb.toString());
			forwardPage = processError(request,mapping,t);
		}
		return forwardPage;
	}

	protected ActionForward processError(HttpServletRequest request,ActionMapping mapping,Throwable t)
	{
		ActionForward forward = null;
		ActionErrors errors = new ActionErrors();
		ActionError newActionError = null;

		StringBuffer sb = new StringBuffer();
		StackTraceElement[] ste = t.getStackTrace();
		for (int i = 0; i < ste.length; i++)
		{
			sb.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;").append(ste[i]).append("<br>");
		}
		newActionError = new ActionError("sys.error",new String[]{t.toString(),sb.toString()});

		errors.add("org.apache.struts.action.GLOBAL_ERROR",newActionError);

		saveErrors(request,errors);

		forward = mapping.findForward("sys_error");

		return forward;
	}
	
	
	protected ActionForward processServiceException(HttpServletRequest request,ActionMapping mapping,ServiceException ex)
	{
		ActionForward forward = null;
		ActionErrors errors = new ActionErrors();
		ActionError newActionError = null;
		List args = new ArrayList();
		if (ex.getErrmsg() == null || ex.getErrmsg().trim().length() == 0)
		{
			if (ex.getErrdetail() == null || ex.getErrdetail().trim().length() == 0)
				args.add(ex.getErrcode());
			else
				args.add(ex.getErrdetail());
		}
		else
			args.add(ex.getErrmsg());

		newActionError = new ActionError("service.error",args.toArray());

		errors.add("org.apache.struts.action.GLOBAL_ERROR",newActionError);

		saveErrors(request,errors);

		if (ex.getDisplayType() == BaseException.ERROR_PAGE)
		{
			forward = mapping.findForward("service_error");
		}
		else if (ex.getDisplayType() == BaseException.RETURN_PAGE)
		{
			forward = mapping.findForward(ex.getReturnForward());
		}
		else if (ex.getDisplayType() == BaseException.RETURN_BYPAGE)
		{
			forward = new ActionForward(ex.getReturnForward());
			forward.setRedirect(true);
		}
		else
		{
			forward = mapping.findForward("service_error");
		}

		return forward;
	}
	
	protected ActionForward processBusiExceptions(HttpServletRequest request,ActionMapping mapping,BusiException ex)
	{
		ActionErrors errors = new ActionErrors();
		ActionForward forward = null;

		processBusiException(errors,ex);

		List exceptions = ex.getExceptions();
		if (exceptions != null && !exceptions.isEmpty())
		{
			Iterator iter = exceptions.iterator();

			while (iter.hasNext())
			{
				BusiException subException = (BusiException) iter.next();

				processBusiException(errors,subException);
			}
		}

		saveErrors(request,errors);

		if (ex.getDisplayType() == BaseException.ERROR_PAGE)
		{
			forward = mapping.findForward("app_error");
		}
		else if (ex.getDisplayType() == BaseException.RETURN_PAGE)
		{
			forward = mapping.findForward(ex.getReturnForward());
		}
		else if (ex.getDisplayType() == BaseException.RETURN_BYPAGE)
		{
			forward = new ActionForward(ex.getReturnForward());
			forward.setRedirect(true);
		}
		else
		{
			forward = mapping.findForward("app_error");
		}

		return forward;
	}

	protected void processBusiException(ActionErrors errors,BusiException ex)
	{
		ActionError newActionError = null;

		String msgkey = ex.getMessageKey();
		List args = ex.getMessageArgs();

	
	  newActionError = new ActionError("business.error",args.toArray());

		errors.add("org.apache.struts.action.GLOBAL_ERROR",newActionError);
	}
	
	
	protected boolean isTimeout(HttpServletRequest request) {
		if (request.getSession().getAttribute(SysConstant.LOGINVO) == null) {
			return true;
		} else {
			return false;
		}
	}
	
	/*
	 * check if obj exists in session
	 */
	protected boolean isExistSession(HttpServletRequest request, String key) {
		if (request.getSession().getAttribute(key) != null) {
			return true;
		} else {
			return false;
		}
	}
}

⌨️ 快捷键说明

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