자체 개발 복잡한 MVC 아키텍처 3 - 매핑 팩토리 및 반사 팩토리

9869 단어 mvc
우선 두 개의 공장을 사용한다.
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;

	}

}


좋은 웹페이지 즐겨찾기