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

📄 storefrontbaseaction.java

📁 《基于Eclipse的开源框架技术与实战》[第5章]随书源码
💻 JAVA
字号:
package com.free.struts.storefront.framework;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;

import com.free.struts.storefront.framework.util.IConstants;
import com.free.struts.storefront.service.IStorefrontService;
import com.free.struts.storefront.service.IStorefrontServiceFactory;

/**
 * <p>Title: Eclipse Plugin Development</p>
 * <p>Description: Free download</p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: Free</p>
 * @author gan.shu.man
 * @version 1.0
 */

abstract public class StorefrontBaseAction extends Action {
	Log log = LogFactory.getLog(this.getClass());

	/**
	 * 通过工厂获得IStorefrontService
	 * */
	protected IStorefrontService getStorefrontService() {
		IStorefrontServiceFactory factory =
			(IStorefrontServiceFactory) getApplicationObject(IConstants
				.SERVICE_FACTORY_KEY);

		IStorefrontService service = null;

		try {
			service = factory.createService();
		} catch (Exception ex) {
			log.error("Problem creating the Storefront Service", ex);
		}
		return service;
	}

	/**
	 * 通过attrName属性获昨Session级别的对象
	 */
	protected Object getSessionObject(
		HttpServletRequest req,
		String attrName) {
		Object sessionObj = null;
		HttpSession session = req.getSession(false);
		if (session != null) {
			sessionObj = session.getAttribute(attrName);
		}
		return sessionObj;
	}

	/**
	 * 获得Application级别的对象
	 */
	protected ApplicationContainer getApplicationContainer() {
		return (ApplicationContainer) getApplicationObject(
			IConstants.APPLICATION_CONTAINER_KEY);
	}

	/**
	 * 通过request对象获得UserContainer对象,或保存到Session中
	 */
	protected UserContainer getUserContainer(HttpServletRequest request) {

		UserContainer userContainer =
			(UserContainer) getSessionObject(request,
				IConstants.USER_CONTAINER_KEY);

		// Create a UserContainer for the user if it doesn't exist already
		if (userContainer == null) {
			userContainer = new UserContainer();
			userContainer.setLocale(request.getLocale());
			HttpSession session = request.getSession(true);
			session.setAttribute(IConstants.USER_CONTAINER_KEY, userContainer);
		}

		return userContainer;
	}

	/**
	 * 通过attrName获得Application级别的对象
	 */
	protected Object getApplicationObject(String attrName) {
		return servlet.getServletContext().getAttribute(attrName);
	}

	/**
	 * 判断用户是否的登录
	 * */
	public boolean isLoggedIn(HttpServletRequest request) {
		UserContainer container = getUserContainer(request);
		if (container.getUserView() != null) {
			return true;
		} else {
			return false;
		}
	}
}

⌨️ 快捷键说明

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