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

自己开发复杂的MVC构架3——映射工厂和反射工厂

 
阅读更多

首先用到两个工厂:

ControllerMappingFactory:功能是通过读取配置文件controllerMapping.xml来构造ControllerMapping

ReflectFactory:功能是通过ControllerMapping的内容来构造ActionBean并把表单信息填入Bean属性

代码如下

异常NotFindForwardException:

package exception;

/**

 * @author lingxiao

 */

public class NotFindForwardException extends Exception {

	private String forwardName="";

	public NotFindForwardException() {

		super();

	}

	public NotFindForwardException(String msg) {

		this.forwardName=msg;

	}

	public NotFindForwardException(String msg, Throwable cause) {

		super();

	}

	public NotFindForwardException(Throwable cause) {

		super();

	}

	public String exceptionInfo(){

		return new String("没有找到相对应的forward ["+forwardName+"]");

	}

}

异常NotFindMappingException:

package exception;

/**

 * @author lingxiao

 */

public class NotFindMappingException extends Exception {

	private String controllerURL="";

	public NotFindMappingException() {

		super();

	}

	public NotFindMappingException(String msg) {

		this.controllerURL=msg;

	}

	public NotFindMappingException(String msg, Throwable cause) {

		super(msg, cause);

	}

	public NotFindMappingException(Throwable cause) {

		super(cause);

	}

	public String exceptionInfo(){

		return new String("没有找到与actionName相对应的ControllerData ["+controllerURL+"]");

	}

}

BaseControllerMappingFactory:

package controller.baseInterfice;



import java.io.File;

import java.io.IOException;



import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;



import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.xml.sax.SAXException;

/**

 * @author lingxiao

 */

public class BaseControllerMappingFactory {

	public String path = "webRoot//WEB-INF//controllerMapping.xml";

	public BaseControllerMapping controllerMapping;

	public String getPath() {

		return path;

	}

	public void setPath(String path) {

		this.path = path;

	}

	public BaseControllerMapping getControllerMapping() {

		return controllerMapping;

	}

	public void setControllerMapping(BaseControllerMapping controllerMapping) {

		this.controllerMapping = controllerMapping;

	}

	BaseControllerMapping buildingMapping(){

		return null;

	}

	/**

	 * 打开配置文件

	 * @return

	 * @throws ParserConfigurationException

	 * @throws SAXException

	 * @throws IOException

	 */

	protected Element getRootElement() throws ParserConfigurationException, SAXException, IOException{

		DocumentBuilderFactory factory = DocumentBuilderFactory 

		.newInstance(); 

		DocumentBuilder builder = factory.newDocumentBuilder(); 

		Document document = builder.parse(new File(path)); 

		return document.getDocumentElement(); 

	}

	

}

ControllerMappingFactory:

package controller;



import java.io.File;

import java.io.IOException;



import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;



import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;



import controller.baseInterfice.BaseControllerMapping;

import controller.baseInterfice.BaseControllerMappingFactory;



/**

 * @author lingxiao

 */

public class ControllerMappingFactory extends BaseControllerMappingFactory{

	public ControllerMappingFactory(String path,BaseControllerMapping controllerMapping){

		this.path = path;

		this.controllerMapping = controllerMapping;

	}

	public void mappingDef(){

		this.controllerMapping = new ControllerMapping();

	}

	/**

	 * 读取XMl文件,并将其元素放到controllerMapping中

	 */

	public BaseControllerMapping buildingMapping() throws ParserConfigurationException, SAXException, IOException{

		if(this.controllerMapping == null)

			this.mappingDef();

		Element rootElement = this.getRootElement();

		NodeList list = rootElement.getElementsByTagName("controller"); 

		for(int i = 0 ; i < list.getLength() ; i++){

			ControllerData tempData = new ControllerData();

			Element controller = (Element) list.item(i);

			tempData.setControllerName(controller.getAttribute("name"));

			tempData.setControllerURL(controller.getAttribute("url"));

			tempData.setControllerBean(controller.getAttribute("class"));

			NodeList chilList = controller.getChildNodes();

			for(int j=0 ; j<chilList.getLength() ; j++){

				Object obj= chilList.item(j); 

					if(obj instanceof Element){ 

						Element e =(Element) obj;

						tempData.addForwardMapping(e.getAttribute("name"), e.getAttribute("value"));

				}

			}

			controllerMapping.addControllerData(tempData.getControllerURL(), tempData);

		}

		return controllerMapping;

	}

}

ReflectFactory:

package controller;



import java.lang.reflect.Field;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;



import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.w3c.dom.Element;

/**

 * @author lingxiao

 */

public class ReflectFactory {

	private HttpServletRequest request;

	private Object obj;

	public ReflectFactory(HttpServletRequest request,Object obj){

		this.request = request;

		this.obj = obj;

	}

	/**

	 * 遍历对象的属性,并将对应的表单元素填入

	 * @throws InvocationTargetException 

	 * @throws NoSuchMethodException 

	 * @throws SecurityException 

	 */

	public Object building() throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchMethodException, InvocationTargetException{

		Field[] fields = obj.getClass().getDeclaredFields();

		for(int i=0 ; i<fields.length ; i++)

			this.setField(fields[i]);

		return this.obj;

	}

	/**

	 * 遍历表单元素找到对应的对象的属性,执行填入

	 * @throws InvocationTargetException 

	 * @throws NoSuchMethodException 

	 * @throws SecurityException 

	 */

	public void setField(Field field) throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchMethodException, InvocationTargetException{

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

		Enumeration parameterNames = request.getParameterNames();

		while(parameterNames.hasMoreElements()){

			String parameterName = (String)parameterNames.nextElement();

			if(field.getName().equals(parameterName)){

				log.debug("注入表单属性["+parameterName+"]......");

				obj = this.intoAttribute(obj, this.convertString(parameterName), request.getParameter(parameterName));

			}

		}

	}

	/**

	 * 根据属性名称自动生成set方法名称

	 * @param s 属性名称

	 * @return set方法名称

	 */

	public String convertString(String s){

		return "set" + s.substring(0,1).toUpperCase() + s.substring(1, s.length());

	}

	/**

	 * 

	 * @param obj 需要调用方法的对像

	 * @param sefunctionName 调用方法的名称

	 * @param value 方法的参数值

	 * @return 调用方法的对像

	 */

	public Object intoAttribute(Object obj,String sefunctionName,String value) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{

		Class cls = obj.getClass();

		Class[] partypes = new Class[1]; 

		partypes[0] = String.class;

		Method meth = cls.getMethod(sefunctionName, partypes); 

		Object arglist[] = new Object[1]; 

		arglist[0] = new String(value);

		meth.invoke(obj, arglist);

		return obj;

	}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics