자체 개발 복잡한 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;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
클린 아키텍처의 Presenter를 이해하기 어려운 것은 MVC 2가 아니기 때문에클린 아키텍처에는 구체적인 클래스 구성 예를 보여주는 다음 그림이 있습니다. 이 그림 중에서 Presenter와 Output Boundary(Presenter의 인터페이스)만 구체 구현을 이미지하는 것이 매우 어렵다...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.