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

📄 abstractcontroller.java

📁 这是我自己开发的一个MVC框架
💻 JAVA
字号:
package dark.web.frame.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dark.web.frame.Controller;
import dark.web.frame.Helper;
import dark.web.frame.Message;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 * <p>Title:            Servlet前端操作器</p>
 * <p>Description:      用于捕获用户请求,并根据请求命令字调用相应的命令处理程序处理请求</p>
 * <p>Copyright:        Copyright (c) 2004</p>
 * <p>Company:          DIS</p>
 * <p>Create Time:      2004-3-6 0:12:56</p>
 * @author             <a href="mailto:dark_he@hotmail.com">darkhe</a>
 * @version            1.0
 */
public abstract class AbstractController
	extends HttpServlet
	implements Controller
{
	public static final String LOG4J_KEY = "log4j";

	public static final String DWF_KEY = "dwf";

	public static final String MESSAGE_KEY = "message";

	protected static Logger log = Logger.getLogger("DWF");

	protected static PropertiesConfiguration configuration =
		new PropertiesConfiguration();

	private static Message message = new Message();

	/**
	 * @param request
	 * @param response
	 * @throws javax.servlet.ServletException
	 * @throws java.io.IOException
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	protected void doGet(
		HttpServletRequest request,
		HttpServletResponse response)
		throws ServletException, IOException
	{
		doRequest(request, response);
	}

	/**	 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	protected void doPost(
		HttpServletRequest request,
		HttpServletResponse response)
		throws ServletException, IOException
	{
		doRequest(request, response);
	}

	/**
	 * @return
	 */
	public static PropertiesConfiguration getConfiguration()
	{
		return configuration;
	}

	/**
	 * @param configuration
	 */
	public static void setConfiguration(PropertiesConfiguration configuration)
	{
		AbstractController.configuration = configuration;
	}

	/**
	 * 加载dwf初始化参数
	 * @param config servlet configuation
	 */
	protected void initDWF(ServletConfig config) throws ServletException
	{
		ServletContext servletContext = config.getServletContext();

		String file = config.getInitParameter(DWF_KEY);

		if (file == null || file.length() == 0)
		{
			file = servletContext.getInitParameter(DWF_KEY);
		}

		if (file != null)
		{
			String filepath = Helper.getRealPath(servletContext, file);
			InputStream is = Helper.getInputStream(servletContext, filepath);
			try
			{
				configuration.load(is);
			}
			catch (IOException e)
			{
				log.error("加载DWF配置信息出错", e);

				// 因为这个配置信息相当关键,
				// 所以不允许在初始化过程出错,
				// 所以一旦出错则停止初始化过程
				throw new ServletException();
			}
		}
		else
		{
			log.error("加载DWF配置信息出错");

			// 因为这个配置信息相当关键,
			// 所以不允许在初始化过程出错,
			// 所以一旦出错则停止初始化过程			
			throw new ServletException("dwf 配置参数读取失败");
		}
	}

	/**
	 * 初始化initLog4j引擎
	 */
	protected void initLog4j(ServletConfig config)
	{
		ServletContext servletContext = config.getServletContext();

		String file = config.getInitParameter(LOG4J_KEY);

		if (file == null || file.length() == 0)
		{
			file = servletContext.getInitParameter(LOG4J_KEY);
		}

		if (file != null)
		{
			String filepath = Helper.getRealPath(servletContext, file);
			InputStream is = Helper.getInputStream(servletContext, filepath);
			try
			{
				Properties p = new Properties();
				p.load(is);

				String path = p.getProperty("log4j.appender.A2.file");

				if (path != null)
				{
					path = getServletContext().getRealPath(path);
					log.info("log4j.appender.A2.file=" + path);

					p.setProperty("log4j.appender.A2.file", path);
				}

				PropertyConfigurator.configure(p);
			}
			catch (IOException e)
			{
				log.error("加载LOG4J配置信息出错", e);
			}
		}
		else
		{
			log.error("log4j 配置参数读取失败");
		}
	}

	/**
	 * 初始化Message
	 */
	protected void genMessage(ServletConfig config)
	{
		ServletContext servletContext = config.getServletContext();

		String file = config.getInitParameter(MESSAGE_KEY);

		if (file == null || file.length() == 0)
		{
			file = servletContext.getInitParameter(MESSAGE_KEY);
		}

		if (file != null)
		{
			String filepath = Helper.getRealPath(servletContext, file);
			InputStream is = Helper.getInputStream(servletContext, filepath);
			try
			{
				message = new Message(is);
			}
			catch (IOException e)
			{				
				log.error("加载message文件出错,生成一个不能读取消息文件的Message实例", e);
			}
		}
		else
		{
			log.error("message 配置参数读取失败");
		}
	}
	/**
	 * @param servletConfig
	 * @throws javax.servlet.ServletException
	 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
	 */
	public void init(ServletConfig servletConfig) throws ServletException
	{
		super.init(servletConfig);

		initDWF(servletConfig);

		initLog4j(servletConfig);

		genMessage(servletConfig);
	}
	/**
	 * @return
	 */
	public static Message getMessage()
	{
		return message;
	}

	/**
	 * @param message
	 */
	public static void setMessage(Message message)
	{
		AbstractController.message = message;
	}

}

⌨️ 快捷键说明

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