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

📄 baseaction.java

📁 一段很有意义的源码,看了就知道,不信试一试啊!
💻 JAVA
字号:


package com.wondersgroup.core;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

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

import org.apache.struts.actions.DispatchAction;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.wondersgroup.framework.core.bo.Page;
import com.wondersgroup.framework.resource.connector.provider.GeneralResourceProvider;
import com.wondersgroup.framework.security.bo.SecurityRole;
import com.wondersgroup.framework.security.bo.SecurityUser;
import com.wondersgroup.framework.security.service.ACLResourceService;
import com.wondersgroup.framework.security.service.ACLService;
import com.wondersgroup.framework.security.service.AuthenticationService;
import com.wondersgroup.framework.security.service.RoleService;
import com.wondersgroup.framework.security.service.UserService;
import com.wondersgroup.framework.security.web.action.StrutsActionConstants;

public abstract class BaseAction extends DispatchAction {
	public List serviceList = new ArrayList();

	public GeneralResourceProvider generalResourceProvider;

	public UserService userService;

	public ACLResourceService aclResourceService;

	public RoleService roleService;

	public ACLService aclService;

	public AuthenticationService authenticationService;

	// 返回到页面的分页对象
	public Page page;

	// 返回到页面的结果集
	public List resultList;

	public String pageReturnNormalType = "normal";

	public String pageReturnSpecialType = "special";

	public ActionForward execute(ActionMapping mapping, ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response)  {

    ActionForward af = null;

    /** @todo add check if the current user have priviage */
    /** @todo first step:get current userID */

    /** @todo step2: call check */

    /** @todo end check............. */

    try {
      af = super.execute(mapping, form, request, response);
    }
    catch (Exception ex) {
      System.out.println("ex="+ex);
      af = mapping.findForward("failed");
      request.setAttribute("errors",ex);
    }

    //System.out.println("call execute myown!....");
    return af;

  }


	

	/**
	 * @param userService
	 *            The userService to set.
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
		serviceList.add(userService);
	}

	/**
	 * @param resourceService
	 */
	public void setAclResourceService(ACLResourceService resourceService) {
		this.aclResourceService = resourceService;
		serviceList.add(resourceService);
	}

	/**
	 * @param roleService
	 */
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
		serviceList.add(roleService);
	}

	/**
	 * @param aclService
	 */
	public void setAclService(ACLService aclService) {
		this.aclService = aclService;
		serviceList.add(aclService);
	}

	/**
	 * @param generalResourceProvider
	 */
	public void setGeneralResourceProvider(GeneralResourceProvider generalResourceProvider) {
		this.generalResourceProvider = generalResourceProvider;
		serviceList.add(generalResourceProvider);
	}

	/**
	 * @param authenticationService
	 *            The authenticationService to set.
	 */
	public void setAuthenticationService(AuthenticationService authenticationService) {
		this.authenticationService = authenticationService;
		serviceList.add(authenticationService);
	}

	public String getRequestParameter(HttpServletRequest request, String attributeName) {
		return request.getParameter(attributeName);
	}

	public String[] getRequestParameterValues(HttpServletRequest request, String attributeName) {
		return request.getParameterValues(attributeName);
	}

	public void setRequestAttribute(HttpServletRequest request, String attributeName, Object attributeValue) {
		request.setAttribute(attributeName, attributeValue);
	}

	public Object getRequestAttribute(HttpServletRequest request, String attributeName) {
		return request.getAttribute(attributeName);
	}

	/* ================================Abstract method for Action======================================= */

	/**
	 * 判断登陆用户是否为Administrator
	 * 
	 * @param request
	 * @return
	 */
	public boolean judgeLoginUser(HttpServletRequest request) {
		boolean isAdministrator = false;
		Object obj = getLoginName(request);
		if (obj != null) {
			String loginName = (String) obj;
			SecurityUser securityUser = userService.getUserByLoginName(loginName);
			SecurityRole[] securityRoles = roleService.loadAllSpecialRoleFromUser(securityUser);
			for (int i = 0; i < securityRoles.length; i++) {
				if (securityRoles[i].getName().equals("Administrator"))
					isAdministrator = true;
			}
		}
		return isAdministrator;
	}

	/**
	 * 得到登陆用户的默认(自己)角色
	 * 
	 * @param request
	 * @return
	 */
	public SecurityRole getLoginUserDefaultRole(HttpServletRequest request) {
		Object obj = getLoginName(request);// request.getSession().getAttribute("loginName");
		if (obj != null) {
			String loginName = (String) obj;
			SecurityUser securityUser = userService.getUserByLoginName(loginName);
			return roleService.loadDefaultRoleWithUser(securityUser);
		}
		return null;
	}

	/**
	 * 得到登陆的用户对象
	 * @param request
	 * @return
	 */
	public SecurityUser getLoginUser(HttpServletRequest request){
		Object obj = getLoginName(request);// request.getSession().getAttribute("loginName");
		if (obj != null) {
			String loginName = (String) obj;
			return userService.getUserByLoginName(loginName);
		}
		return null;
	}
	
	/**
	 * 判断登陆的用户是否就是选择的用户
	 * 
	 * @param request
	 * @param selectedloginName
	 * @return
	 */
	public boolean isloginNameEqualsSelectUser(HttpServletRequest request, String selectedloginName) {
		boolean isloginNameEqualsSelectUser = false;
		Object obj = getLoginName(request);// request.getSession().getAttribute("loginName");
		if (obj != null) {
			String loginName = (String) obj;
			if (loginName.equals(selectedloginName))
				isloginNameEqualsSelectUser = true;
		}
		return isloginNameEqualsSelectUser;
	}

	private String getLoginName(HttpServletRequest request){
		String loginName = null;
		Object obj = request.getSession().getAttribute("loginName");
		if(obj != null)
			loginName = (String)obj;
		else{
			loginName = getLoginNameByCookie(request);
		}
		return loginName;
	}
	
	private String getLoginNameByCookie(HttpServletRequest request) {
		String loginName = null;
		Cookie[] cookies = request.getCookies();
		for (int i = 0; i < cookies.length; i++) {
			if (cookies[i].getName().equals("loginName")) {
				loginName = (String) cookies[i].getValue();
				break;
			}
			else
				continue;
		}
		return loginName;
	}

	/**
	 * Action通用处理翻页方法
	 * 
	 * @param request
	 * @param service
	 *            调用service的实例
	 * @param methodName
	 *            调用service的method名称
	 * @param inputParamArray
	 *            调用参数值数组
	 * @param inputParamType
	 *            调用参数类型数组
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @throws ClassNotFoundException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public void handlePageReturn(HttpServletRequest request, Object service, String methodName,
			Object[] inputParamArray, Object[] inputParamType) throws SecurityException, NoSuchMethodException,
			ClassNotFoundException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		int pagenumber = (getRequestParameter(request, StrutsActionConstants.PAGE_NUM) != null) ? Integer
				.parseInt(getRequestParameter(request, StrutsActionConstants.PAGE_NUM)) : Integer.parseInt("1");
		Object selservice = serviceList.get(serviceList.indexOf(service));
		Class selClass = Class.forName(selservice.getClass().getName());
		Object[] overAllParamValues = new Object[inputParamArray.length + 1];
		System.arraycopy(inputParamArray, 0, overAllParamValues, 0, inputParamArray.length - 1);
		overAllParamValues[inputParamArray.length - 1] = Integer.valueOf(String.valueOf(pagenumber));
		overAllParamValues[inputParamArray.length] = inputParamArray[inputParamArray.length - 1];

		Class[] paramTypes = new Class[inputParamType.length + 1];
		System.arraycopy(inputParamType, 0, paramTypes, 0, inputParamType.length - 1);
		paramTypes[inputParamType.length - 1] = int.class;
		paramTypes[inputParamType.length] = (Class) inputParamType[inputParamType.length - 1];

		Method method = selClass.getMethod(methodName, paramTypes);
		page = (Page) method.invoke(selservice, overAllParamValues);
		resultList = new ArrayList();
		if (page.getResult() instanceof ArrayList) {
			resultList = (List) page.getResult();
		}
	}
	
	public Object getService(String serviceName,HttpServletRequest request){
		ApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
		return context.getBean(serviceName);
		
		
	}
	
}

⌨️ 快捷键说明

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