ModelMapping主要是XML配置文件的Java映射,类似Spring中的XmlBeanFactory,没它那么复杂。
//ModelMapping Title: A framework project. Description: Copyright: Copyright (c) 2004 . Company:
package com.jdon.strutsutil;
/
*
*
*
*
* @author Written by liyong.
* @version 1.0
*
*
* key="itemId"
* model="com.jdon.estore.model.Item"
* handler = "com.jdon.estore.web.catalog.ItemHandler" />
*
*/
public class ModelMapping {
private String key;
private String model;
private String handler;
public ModelMapping() { }
public void setKeyValue(String key) {
this.key = key;
}
public String getKeyValue() {
return this.key;
}
public String getHandler() {
return handler;
}
public void setHandler(String handler) {
this.handler = handler;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
}
//ModelPoolFactory
package com.jdon.strutsutil;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import com.jdon.controller.model.Model;
/
*
Title: A framework project.
*
Description:
*
Copyright: Copyright (c) 2004 .
*
Company:
* @author Written by liyong.
* @version 1.0
*
*
* key="itemId"
* model="com.jdon.estore.model.Item"
* handler = "com.jdon.estore.web.catalog.ItemHandler" />
*
*/
public class ModelPoolFactory {
private static final String MODEL_MAPPING_FILE = "/modelmapping.xml";
private static final String DEFAULT_MODEL_MAPPINGS = "modelmappings";
private static final String DEFAULT_MODEL_MAPPING = "modelmapping";
private static final String ATTRIBUTE_FORMNAME = "formName";
private static final String ATTRIBUTE_KEY = "key";
private static final String ATTRIBUTE_MODEL = "model";
private static final String ATTRIBUTE_HANDLER = "handler";
private static ModelPoolFactory instance = new ModelPoolFactory();
private Map modelMap = new HashMap();
/
* Initialization.
* Read configuration from file : /modelmapping.xml.
*/
private ModelPoolFactory() {
try {
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(getClass().getResourceAsStream(MODEL_MAPPING_FILE));
Element root = doc.getRootElement();
List list = root.getChildren(DEFAULT_MODEL_MAPPING);
for (int i=0; i
ModelMapping mapping = new ModelMapping();
mapping.setKeyValue(child.getAttribute(ATTRIBUTE_KEY).getValue());
mapping.setModel(child.getAttribute(ATTRIBUTE_MODEL).getValue());
mapping.setHandler(child.getAttribute(ATTRIBUTE_HANDLER).getValue());
String key = child.getAttribute(ATTRIBUTE_FORMNAME).getValue();
modelMap.put(key, mapping);
//System.out.println(child.getAttribute(ATTRIBUTE_FORMNAME).getValue());
//System.out.println(child.getAttribute(ATTRIBUTE_KEY).getValue());
//System.out.println(child.getAttribute(ATTRIBUTE_MODEL).getValue());
//System.out.println(child.getAttribute(ATTRIBUTE_HANDLER).getValue());
}
} catch (Exception e) {
e.printStackTrace();
}
}
/
* Return instance of ModelPoolFactory.
* Singleton pattern.
* @return
*/
public static ModelPoolFactory getInstance() {
return instance;
}
/
* Get Handler Object.
* @param formName
* @return
*/
public ModelHandler getHandlerObject(String formName){
ModelHandler modelHandler = null;
try {
ModelMapping mapping = (ModelMapping)modelMap.get(formName);
String className = mapping.getHandler();
modelHandler = (ModelHandler) Class.forName(className).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return modelHandler;
}
/
* Get Model Object.
* @param formName
* @return
*/
public Model getModelObject(String formName){
Model model = null;
try {
ModelMapping mapping = (ModelMapping)modelMap.get(formName);
String className = mapping.getModel();
model = (Model) Class.forName(className).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return model;
}
/
* Return the model mapping.
* @return
*/
public Map getModelMappings() {
return this.modelMap;
}
/
* Test entry.
* @param args
*/
public static void main(String[] args) {
ModelPoolFactory mpf = ModelPoolFactory.getInstance();
}
}
随着开发项目的增加,越来越觉得重复的事情做得越来越多,经常性的为了一个小小的代码问题而耗费精力。自然,编写代码也变得越来越是个痛苦的事。幸而,拜读了彭老师的书,使我有了豁然开朗的感觉,于是就想按书上的思想去实现它,当然,因自己水平的关系,代码只能在不断地完善和提炼。