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

📄 pubaction.java

📁 struts+hibernate BBS mysql数据库 功能基本齐全
💻 JAVA
字号:
package com.elan.forum.actions;

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 com.elan.forum.actions.util.ForumRole;
import com.elan.forum.elf.El;

/*对moduleId,
 * pieceId,
 * topicId
 * action 
 * position
 * TopicSessionContainer
 * Check Session
 * Check Admin
 * 进行的相关INIT操作和给出get/set方法
 * 
 * 子类需要实现executeAction方法,实现逻辑操作
 * 
 * 
 */
public abstract class PubAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		/*HttpSession session = null;
		El el = null;
		String action = request.getParameter("action");
		String currentRequestUrl = null;
		Integer moduleId = null;
		Integer pieceId = null;
		Integer topicId = null;*/
		//初始化一下对象
		/*init(request, session, el, currentRequestUrl, action, moduleId,
				pieceId, topicId);*/
		if (!processPreprocess(mapping, form, request, response)) {
			return null;
		}
		//构造position字符窜应该放到其子类的实例中去构造,即叶子类图的叶子节点
		return executeAction(mapping, form, request, response);
	}


	protected abstract ActionForward executeAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) ;


	protected boolean processPreprocess(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// 扩散函数,比如修改一些属性
		return true;
	}	
	
	protected boolean checkIsLogin(HttpServletRequest request) {
		if (!ForumRole.CheckIsLogin(request)) {
			this.setPosition(request);
			return false;
		}
		return true;
	}
	/**
	 * 设置上一次的URL
	 * @param request
	 */
	protected void setPosition(HttpServletRequest request) {
		String queryString = request.getQueryString();
		request.setAttribute("url", this.getCurrentRequestUrl(request) + (queryString.trim().length() > 0 ? ("?" + queryString) : ""));
		System.out.println("url:" + request.getAttribute("url"));
	}


	protected HttpSession getSession(HttpServletRequest request) {
		return request.getSession();
	}

	@SuppressWarnings("unused")
	protected HttpSession getSession(HttpServletRequest request, boolean b) {
		return request.getSession(b);
	}

	protected String getAction(HttpServletRequest request) {
		String action = request.getParameter("action");
		if (request.getAttribute("action") != null) {
			action = (String) request.getAttribute("action");
		}
		request.setAttribute("action", action);
		return action;
	}

	protected Integer getModuleId(HttpServletRequest request) {
		String moduleIdStrPt = request.getParameter("moduleId");
		Integer moduleIdStrRq =  (Integer) request.getAttribute("moduleId");
		String moduleIdStrSs = (String) getSession(request).getAttribute(
				"moduleId");
		Integer moduleId = null;
		try {
			if(moduleIdStrPt != null && !"".equals(moduleIdStrPt)) {
				moduleId = Integer.valueOf(moduleIdStrPt); 
			}
			if (moduleIdStrRq != null) {
				moduleId = Integer.valueOf(moduleIdStrRq);
			}
			if (moduleIdStrSs != null && !"".equals(moduleIdStrRq)) {
				moduleId = Integer.valueOf(moduleIdStrSs);
			}
		} catch (NumberFormatException nfe) {
			throw nfe;
		}
		return moduleId;
	}

	protected Integer getPieceId(HttpServletRequest request) {
		String pieceIdStrPt = request.getParameter("pieceId");
		Integer pieceIdStrRq = (Integer) request.getAttribute("pieceId");
		String pieceIdStrSs = (String) getSession(request).getAttribute(
				"pieceId");
		Integer pieceId = null;
		try {
			if(pieceIdStrPt != null && !"".equals(pieceIdStrPt)) {
				pieceId = Integer.valueOf(pieceIdStrPt);
			}
			if(pieceIdStrRq != null) {
				pieceId = Integer.valueOf(pieceIdStrRq);
			}
			if (pieceIdStrSs != null && !"".equals(pieceIdStrRq)) {
				pieceId = Integer.valueOf(pieceIdStrSs);
			}
		} catch (NumberFormatException nfe) {
			throw nfe;
		}
		return pieceId;
	}

	protected Integer getTopicId(HttpServletRequest request) {
		String topicIdStrPt = request.getParameter("topicId");
		Integer topicIdStrRq =  (Integer) request.getAttribute("topicId");
		String topicIdStrSs = (String) getSession(request).getAttribute(
				"topicId");
		Integer topicId = null;
		try {
			if(topicIdStrPt != null && !"".equals(topicIdStrPt)) {
				topicId = Integer.valueOf(topicIdStrPt);
			} else if(topicIdStrRq != null) {
				topicId = Integer.valueOf(topicIdStrRq);
			} else if (topicIdStrSs != null && !"".equals(topicIdStrRq)) {
				topicId = Integer.valueOf(topicIdStrSs);
			}
		} catch (NumberFormatException nfe) {
			throw nfe;
		}
		return topicId;
	}

	private String getCurrentRequestUrl(HttpServletRequest request) {
		return request.getRequestURI();
	}

	/*protected void init(HttpServletRequest request, HttpSession session, El el,
		 String action, String currentRequestUrl,
			Integer moduleId, Integer pieceId, Integer topicId) {
		el = El.getEl();
		session = getSession(request);
		currentRequestUrl = request.getRequestURI();
		action = this.getAction(request);
		moduleId = this.getModuleId(request);
		pieceId = this.getPieceId(request);
		topicId = this.getTopicId(request);
	}*/
	
	protected void setAttribute(HttpServletRequest request,String name, Object value) {
		request.setAttribute(name, value);
	}
	
	protected void setAttribute(HttpSession session,String name, Object value) {
		session.setAttribute(name, value);
	}
	
	protected Integer getAuthorId(HttpServletRequest request) {
		String authorId = null;
		authorId = request.getParameter("authorId");
		if(null != request.getAttribute("authorId")) {
			authorId = (String) request.getAttribute("authorId");
		}
		if(authorId != null) {
			return Integer.valueOf(authorId) < 1 ? null : Integer.valueOf(authorId);
		}
		return null;
	}
}

⌨️ 快捷键说明

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