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

📄 baseaction.java

📁 j2ee源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.leeman.wkexs.web.base;

import java.util.Map;
import java.util.HashMap;
import java.util.Enumeration;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

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

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.apache.struts.util.MessageResources;
import org.apache.struts.Globals;
import java.util.Locale;

import com.leeman.common.util.Formatter;
import com.leeman.common.web.ui.ScreenMessage;
import com.leeman.common.web.ui.ScreenMessageHelper;
import com.leeman.common.web.ui.ScreenOption;
import com.leeman.common.entity.ObjectTokenVO;
import com.leeman.common.entity.SysXlatItemVO;
import com.leeman.wkexs.common.ClientInfo;
import com.leeman.wkexs.logon.web.LogonForm;
import com.leeman.wkexs.common.security.WebGuard;
import com.leeman.wkexs.web.base.BaseConstants;

public abstract class BaseAction extends Action {

 	protected HashMap methods = new HashMap();
 	protected Class clazz = this.getClass();
 	protected Class[] types = { ActionMapping.class,
							  ActionForm.class,
							  HttpServletRequest.class,
							  HttpServletResponse.class };
		
	public ActionForward execute(ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response)
		throws Exception
	{	
		String name = getMethodName(request);
		
		if ("execute".equals(name) || "perform".equals(name)){
	  		throw new ServletException();
  		}
		beforeExecute(mapping,form, request, response);
  		ActionForward af = dispatchMethod(mapping, form, request, response, name);
  		afterExecute(mapping, form, request, response);
  		return af;
	}
	
	private void beforeExecute(ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) throws Exception{
		doBeforeExecute(mapping, form, request, response);		
	}
	
	protected void doBeforeExecute(ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) throws Exception{
		if(form == null || !form.getClass().toString().equals(new LogonForm().getClass().toString()))
			checkLogin(request,response);
	}
	
	private void afterExecute(ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) throws Exception{
		doAfterExecute(mapping, form, request, response);
	}
	
	protected void doAfterExecute(ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) throws Exception{
			
		//Get Page Title
		if(getCollectionCache(request) != null){
			HttpSession session = request.getSession();
			String pageTitle = getPageTitle(request);
			session.setAttribute(BaseConstants.SESSION_PAGE_TITLE, pageTitle);
		}
	}
		
	protected ActionForward dispatchMethod(ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response,
		String name)
		throws Exception {

		if (name == null) {
			return this.unspecified(mapping, form, request, response);
		}

		Method method = null;

		try {
			method = getMethod(name);
		} catch(NoSuchMethodException nsme) {
			throw nsme;
		}

		ActionForward forward = null;

		try {
			Object args[] = {mapping, form, request, response};
			forward = (ActionForward) method.invoke(this, args);
		} catch(ClassCastException cce) {
			throw cce;
		} catch(IllegalAccessException iae) {
			throw iae;
		} catch(InvocationTargetException ite) {
			Throwable t = ite.getTargetException();
			if (t instanceof Exception) {
				throw ((Exception) t);
			} else {
				throw new ServletException(t);
			}
		}
		return (forward);
	}
	 
	protected Method getMethod(String name) throws NoSuchMethodException {
		synchronized(methods) {
			Method method = (Method) methods.get(name);

			if (method == null) {
				method = clazz.getMethod(name, types);
				methods.put(name, method);
			}
			return (method);
		 }
	}
	  
	protected ActionForward unspecified(ActionMapping mapping,
										ActionForm form,
										HttpServletRequest request,
										HttpServletResponse response)
		throws Exception {
		  throw new ServletException();
	}
  	
	public static String getCmdName(HttpServletRequest request) {
		//Directly select command by command parameter
		boolean cmdFound = false;
		String cmdName = null;
		if (request.getParameter("action") != null && !request.getParameter("action").equals("")){
			String command = request.getParameter("action");
			if (!isDispatchedCmd(request, (String)command)){
				cmdName = (String)command;
				cmdFound = true;
			}
		}
		//Find command in the request
		if (cmdFound)
		{
			return cmdName;
		}
		else
		{
			String buttonValue = null;
			Enumeration enum = request.getParameterNames();		
			while(enum.hasMoreElements()) {
				buttonValue = (String)enum.nextElement();
				if(buttonValue.indexOf("cmd.") >= 0) {
					if (!isDispatchedCmd(request, buttonValue)){
						cmdName = buttonValue;
						break;
					}
				}
			}
			if (cmdName != null){
				int lastDot = cmdName.lastIndexOf(".");
				if (lastDot != cmdName.indexOf("cmd.") + 3){
					return cmdName.substring(cmdName.indexOf("cmd.") + 4, lastDot);
				}
				else{
					return cmdName.substring(cmdName.indexOf("cmd.") + 4, cmdName.length());		
				}
			}
			else
			{
				return null;
			}
		}
	}
	
	private static boolean isDispatchedCmd(HttpServletRequest request, String cmd)
	{
		Object o = request.getAttribute("dispatchedCmdMap");
		if (o == null)
		{
			HashMap map = new HashMap();
		  	map.put(cmd, "1");
		  	request.setAttribute("dispatchedCmdMap", map);
		  	return false;
	  	}
	  	else
	  	{
		  	HashMap h =(HashMap)o;	
		  	if ("1".equals(h.get(cmd)))
		 	{
				return true;
		  	}
		  	else
		  	{
				return false;
		  	}
	  	}
  	}	

  	public String getMethodName(HttpServletRequest request)
  	{
		String cmdName = getCmdName(request);
	  	return (String)getCmdMethodMap().get(cmdName);
 	}

  	protected ActionForward cancelled(ActionMapping mapping,
									  ActionForm form,
									  HttpServletRequest request,
									  HttpServletResponse response)
		throws Exception {
	  	return null;
  	}
  
  	protected abstract Map getCmdMethodMap();
  	
	//	Session Management
	protected BaseCollectionCache getCollectionCache(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
		return (BaseCollectionCache)session.getAttribute(BaseConstants.SESSION_COLLECTION_CACHE);
	}
	
	protected void setCollectionCache(HttpServletRequest request, BaseCollectionCache collectionCache)
  	{
		HttpSession session = request.getSession();
	 	session.setAttribute(BaseConstants.SESSION_COLLECTION_CACHE, collectionCache);
  	}
  	
	protected ClientInfo getClientInfo(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
		ClientInfo ci = (ClientInfo)session.getAttribute(BaseConstants.SESSION_CLIENT_INFO);
		return ci;
	}
	
	protected String getCompanyId(HttpServletRequest request)
	{
		return getClientInfo(request).getCompany_id();
	}
	
	protected String getLangId(HttpServletRequest request)
	{
		return getClientInfo(request).getLang_id();
	}
	
	protected void setMode(ActionForm form, HttpServletRequest request)
	{
		setMode(form, request, getMode(request));		
	}
	
	protected void setMode(ActionForm form, HttpServletRequest request, String mode)
	{

⌨️ 快捷键说明

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