首先用到两个工厂:
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;
}
}
分享到:
相关推荐
mvc构架.net版集成spring,hibernate ,前端用ext.net,功能丰富,界面华丽,运行速度快。
简单的外文翻译,有关_Spring的web_MVC_构架模式
PHP实例开发源码—简易通讯录php MVC构架.zip PHP实例开发源码—简易通讯录php MVC构架.zip PHP实例开发源码—简易通讯录php MVC构架.zip
使用php开发自己的MVC模式构架.pdf ),新手可以看看,认识下什么是mvc框架,对框架有个底层的认识,也能试着自己开发一个
MVC,Smarty
外国佬的.net MVC构架底层外国佬的.net MVC构架底层外国佬的.net MVC构架底层
关于Spring的web MVC 构架模式Spring的web MVC 构架模式
外文翻译_Spring的web MVC 构架模式.docx外文翻译_Spring的web MVC 构架模式.docx外文翻译_Spring的web MVC 构架模式.docx外文翻译_Spring的web MVC 构架模式.docx外文翻译_Spring的web MVC 构架模式.docx外文翻译_...
jsp订单作品+课设mvc构架 用于jsp课程设计下载。 独自开发,运行良好。
对于 extjs 来说,大客户端程序一直很难写,当你为大客户端程序添加更多的功能和项目的时候,项目的体积往往迅速增长。...织和维持 ,所以,extjs4 配备了一个新的应用程序体系结构,它能结构化你的代码,那就是 extjs4 MVC。
对于 extjs 来说,大客户端程序一直很难写,当你为大客户端程序添加更多的功能和项目的时候,项目的体积往往迅速增长。...织和维持 ,所以,extjs4 配备了一个新的应用程序体系结构,它能结构化你的代码,那就是 extjs4 MVC。
毕设外文文献翻译--Spring的webMVC构架模式 (3).docx毕设外文文献翻译--Spring的webMVC构架模式 (3).docx
毕设 外文文献翻译--Spring的web MVC构架模式 (3).docx毕设 外文文献翻译--Spring的web MVC构架模式 (3).docx
JAVA MVC构架 电子留言簿
外文翻译Spring地web MVC 构架模式.pdf外文翻译Spring地web MVC 构架模式.pdf
简易MVC构架的通讯录PHP源码,是PHP程序员学习MVC的简单范例。 主要功能:增 删 查 改 最方便的是拥有快速的排序功能。可以方便自主选择是调用数据库,还是使用客户端JS排序。
简单的外文翻译,有关_Spring的web_MVC_构架模式
外文翻译_Spring的web MVC 构架模式.pdf外文翻译_Spring的web MVC 构架模式.pdf
简易MVC构架的通讯录PHP源码,是PHP程序员学习MVC的简单范例。 主要功能:增 删 查 改 最方便的是拥有快速的排序功能。可以方便自主选择是调用数据库,还是使用客户端JS排序。
PHP编写的MVC构建,集成了smarty模板和medoo数据库框架