`
v5qqbrowser
  • 浏览: 358527 次
文章分类
社区版块
存档分类
最新评论

自己开发复杂的MVC构架4——前端控制器

 
阅读更多

首先是配置web,xml文件

<servlet>

    <servlet-name>ControllerServlet</servlet-name>

    <servlet-class>controller.ControllerServlet</servlet-class>

  </servlet>

  <servlet-mapping>

    <servlet-name>ControllerServlet</servlet-name>

    <url-pattern>*.action</url-pattern>

  </servlet-mapping>

ControllerServlet 用到servlet的doGet,doPost和init方法

这里将doGet,doPost的处理过程统一交给doAction来处理

init将实现ControllerMapping的构造

代码如下:

package controller;



import java.io.IOException;

import java.io.PrintWriter;

import java.lang.reflect.InvocationTargetException;

import java.util.Enumeration;



import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.xml.sax.SAXException;



import exception.NotFindMappingException;



import action.BaseAction;

import action.LoginAction;

/**

 * @author lingxiao

 */

public class ControllerServlet extends HttpServlet {

	private static String defForward="SYSTEMERROR";

	private String actionName;

	private HttpSession session;

	private ControllerMapping mapping;

	/**

	 * 请求统一交给doAction处理

	 */

	public void doGet(HttpServletRequest request, HttpServletResponse response)

			throws ServletException, IOException {

		try {

			try {

				this.doAction(request, response);

			} catch (ParserConfigurationException e) {

				e.printStackTrace();

			} catch (SAXException e) {

				e.printStackTrace();

			}

		} catch (ClassNotFoundException e) {

			e.printStackTrace();

		}

	}

	/**

	 * 请求统一交给doAction处理

	 */

	public void doPost(HttpServletRequest request, HttpServletResponse response)

			throws ServletException, IOException {

			try {

				try {

					this.doAction(request, response);

				} catch (ParserConfigurationException e) {

					e.printStackTrace();

				} catch (SAXException e) {

					e.printStackTrace();

				}

			} catch (ClassNotFoundException e) {

				e.printStackTrace();

			}

	}

	/**

	 * 构建ControllerMapping

	 */

	public void init() throws ServletException {

		super.init();

		Log log = LogFactory.getLog(getClass());

		ControllerMappingFactory factory = new ControllerMappingFactory(this.getServletContext().getRealPath("/") + "//WEB-INF//controllerMapping.xml",new ControllerMapping());

		//根据controllerMapping.xml构建ControllerMapping 

		try {

			log.debug("构建ControllerMapping......");

			mapping = (ControllerMapping) factory.buildingMapping();

			log.debug("构建ControllerMapping成功。");

		} catch (ParserConfigurationException e) {

			log.error("构建ControllerMapping失败。"+e.getMessage());

			e.printStackTrace();

		} catch (SAXException e) {

			log.error("构建ControllerMapping失败。"+e.getMessage());

			e.printStackTrace();

		} catch (IOException e) {

			log.error("构建ControllerMapping失败。"+e.getMessage());

			e.printStackTrace();

		}



		// Put your code here

	}

	/**

	 * ControllerData为一个控制器,将会根据其属性controllerBean来加载一个类并创建对象

	 */

	private String doBaseAction(HttpServletRequest request, HttpServletResponse response,ControllerData data) throws ClassNotFoundException{

		Log log = LogFactory.getLog(getClass());

		try {

			Object obj = Class.forName(data.getControllerBean()).newInstance();

			log.debug("根据URL:"+actionName+"获得bean对象:"+obj.toString());

			ReflectFactory factory = new ReflectFactory(request,obj);

			log.debug("正在注入表单属性......");

			BaseAction action = null;

			try {

				action = (BaseAction)factory.building();

			} catch (IllegalArgumentException e) {

				log.error("注入表单属性失败:"+e.getMessage());

				e.printStackTrace();

			} catch (SecurityException e) {

				log.error("注入表单属性失败:"+e.getMessage());

				e.printStackTrace();

			} catch (NoSuchMethodException e) {

				log.error("注入表单属性失败:"+e.getMessage());

				e.printStackTrace();

			} catch (InvocationTargetException e) {

				log.error("注入表单属性失败:"+e.getMessage());

				e.printStackTrace();

			}

			log.debug("正在注入表单属性完毕。");

			//使用反射机制将表单数据写入对象的属性中。

			return action.doOperate(request,response,session);

			//直接调用doOperate方法

		} catch (InstantiationException e) {

			log.error("根据URL:"+actionName+"获得bean对象失败:"+e.getMessage());

			e.printStackTrace();

		} catch (IllegalAccessException e) {

			log.error("根据URL:"+actionName+"获得bean对象失败:"+e.getMessage());

			e.printStackTrace();

		}

		return this.defForward;

	}

	/**

	 * 将请求对应ControllerMapping转发给相应的bean

	 * 通过bean的返回值根据ControllerMapping Forward的值转向下一个地址

	 */

	public void doAction(HttpServletRequest request, HttpServletResponse response)

	throws ServletException, IOException, ClassNotFoundException, ParserConfigurationException, SAXException {

		Log log = LogFactory.getLog(getClass());

		//session比较常用,所以在这里直接获得session吧session传递给bean

		session = request.getSession(true);

		

		//解析URL,将getServletPath()前边"/"的路径,和后面的.action去掉

		String[] path = request.getServletPath().split("/");

		String action = request.getServletPath();

		if(path.length>0)

			action = path[path.length-1];

		actionName = action.split("//.")[0];

		



		//通过actionName来对应bean

		ControllerData data = null;

		try {

			data = (ControllerData)mapping.getControllerData(actionName);

			//通过actionName找到对应的ControllerData数据

		} catch (NotFindMappingException e) {

			log.error(e.exceptionInfo());

			e.printStackTrace();

		}

		String forwardName = this.doBaseAction(request, response, data);

		//通过ControllerData加载类,生成对象,返回forward值

		if(forwardName.equals(this.defForward))

			response.sendRedirect("/errorPage/systemError.jsp");

		//缺省的forward值,返回一个系统错误页面

		String rstring = new String();

		try {

			rstring = data.getForwardValue(forwardName);

			//根据配置文件返回地址

		} catch (NotFindMappingException e) {

			log.error(e.exceptionInfo());

			e.printStackTrace();

		}

		request.getRequestDispatcher(rstring).forward(request, response);

	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics