目录
一、学会XML建模
二、23种建模方式之工厂模式
三、练习案例
一、学会XML建模
今天我们来看看如何XML建模,首先还是先把建模的XML文件【config.xml】放在这里:
我们先来分析一下这个xml文件:
1) 分析标签
一个根标签:config
一个子标签:action
action内有子标签:forward
2) 分析属性
action:path、type
forward:name、path、redirect
根据上面分析我们可以知道此配置文件一共有三个不同的标签:
config:有子标签、没有属性
action:有子标签
forward:没有子标签
因此我们开始我们建模的第一步, 利用面向对象编程思想分别为不同的标签建设实体类:ForwardModel:
package com.leaf.mode;
public class ForwardModel {
//
private String name;
private String path;
private boolean redirect;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public boolean isRedirect() {
return redirect;
}
public void setRedirect(boolean redirect) {
this.redirect = redirect;
}
public ForwardModel() {
}
public ForwardModel(String name, String path, boolean redirect) {
this.name = name;
this.path = path;
this.redirect = redirect;
}
@Override
public String toString() {
return "ForwardMode [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
}
}
ActionModel:有子标签,需要两个行为方法:
1)添加forward标签
2)查询forward标签
package com.leaf.mode;
import java.util.HashMap;
import java.util.Map;
public class ActionModel {
//
private String path;
private String type;
private Map fMap = new HashMap();
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public ActionModel() {
// TODO Auto-generated constructor stub
}
public ActionModel(String path, String type) {
super();
this.path = path;
this.type = type;
}
//将一个新的forward标签对象加入容器
public void push(ForwardModel f) {
fMap.put(f.getName(), f);
}
//查询forward标签
public ForwardModel pop(String name) {
return fMap.get(name);
}
@Override
public String toString() {
return "ActionMode [path=" + path + ", type=" + type + "]";
}
}
ConfigModel:没有属性、有子标签、需要两个行为方法:
1)添加action标签
2)查询action标签
package com.leaf.mode;
import java.util.HashMap;
import java.util.Map;
public class ConfigModel {
private Map aMap = new HashMap();
public void push(ActionModel am) {
aMap.put(am.getPath(), am);
}
public ActionModel pop(String path) {
return aMap.get(path);
}
}
二、23种建模方式之工厂模式
但我们为各个标签建好实体类并且写好对应的行为方法后,
就需要建立一个工厂模式类:ConfigModelFactory
首先看一下有关这个工厂模式类的注释:
接着我就直接放上工厂类的代码啦:
package com.leaf.mode;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ConfigModelFactory {
public static ConfigModel bulid(String path) throws Exception {
InputStream in = ConfigModelFactory.class.getResourceAsStream(path);
SAXReader sr = new SAXReader();
Document doc = sr.read(in);
List actionEles = doc.selectNodes("/config/action");
ConfigModel cm = new ConfigModel();
for (Element actionEle : actionEles) {
ActionModel am = new ActionModel();
am.setPath(actionEle.attributeValue("path"));
am.setType(actionEle.attributeValue("type"));
//将forwardmodel赋值并且添加到Actionmodel中
List forwardEle = actionEle.selectNodes("forward");
for (Element element : forwardEle) {
ForwardModel forwardModel = new ForwardModel();
forwardModel.setName(element.attributeValue("name"));
forwardModel.setPath(element.attributeValue("path"));
//redirect:只能是false|true,允许空,默认值为false
forwardModel.setRedirect("true".equals(element.attributeValue("redirect")));
am.push(forwardModel);
}
cm.push(am);
}
return cm;
}
//这里仅测试,可以在使用的地方直接调用bulid("文件路径")的;
public static ConfigModel bulid() throws Exception {
String defaultPath = "config.xml";
return bulid(defaultPath);
}
}
当这些全部建好了后我们就可以另起一个类调用测试使用一下啦:
public static void main(String[] args) throws Exception {
//调用工厂类的方法获取跟标签config
ConfigModel cm = ConfigModelFactory.bulid();
//调用config的方法查询
ActionModel am = cm.pop("/loginAction");
//打印测试
System.out.println("Type:t"+am.getType()+"n");
//查询
ForwardModel fm = am.pop("success");
//打印测试
System.out.println("Path:t"+fm.getPath());
}
运行结果:
三、练习案例
案例:对web.xml进行建模,写一个servlet通过url-pattern读取到servlet-class的值;
首先还是放上XML文件:web.xml
jrebelServlet com.leaf.xml.JrebelServlet jrebelServlet /jrebelServlet jrebelServlet2 com.leaf.xml.JrebelServlet2 jrebelServlet2 /jrebelServlet2 /jrebelServlet3
接着我们还是来分析一下整个XML文件的结构:
1) 分析标签
一个根标签:web-app
两个子标签:servlet、servlet-mapping
servlet内有子标签:servlet-name、servlet-class
servlet-mapping内有子标签:servlet-name、url-pattern
根据上面分析我们可以知道此配置文件一共有六个不同的标签:
servlet-name、servlet-class、url-pattern、servlet、servlet-mapping、web-app
除了servlet、servlet-mapping、web-app之外,其他标签都有值;
因此,老规矩,咱们先把六个不同的标签分别给它们用面向对象思想建好实体类:
ServletNameModel、ServletClassModel、UrlPatternModel、ServletModel、ServletMappingModel、WebAppModel;
上面已经有过例子,这里就不过多的放置实体类的代码了;
我们建好实体类后就需要建立一个工厂类了,这里就放上工厂类的代码:WebAppFactory
package com.leaf.modezuoye;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class WebAppFactory {
public static WebAppModel build(String path) {
InputStream in = WebAppFactory.class.getResourceAsStream(path);
SAXReader sr = new SAXReader();
WebAppModel wam = new WebAppModel();
try {
Document doc = sr.read(in);
//将servlet的标签内容填充进web-app
List servletEles = doc.selectNodes("/web-app/servlet");
for (Element servletEle : servletEles) {
ServletModel servletModel = new ServletModel();
//给servlet 填充xml的内容
Element servletNameEle = (Element) servletEle.selectSingleNode("servlet-name");
Element servletClassEle = (Element) servletEle.selectSingleNode("servlet-class");
ServletNameModel servletNameModel = new ServletNameModel();
ServletClassModel servletClassModel = new ServletClassModel();
servletNameModel.setNr(servletNameEle.getText());
servletClassModel.setNr(servletClassEle.getText());
servletModel.setSnm(servletNameModel);
servletModel.setScm(servletClassModel);
wam.push(servletModel);
}
List servletMappingEles = doc.selectNodes("/web-app/servlet-mapping");
for (Element servletMappingEle : servletMappingEles) {
ServletMappingModel servletMappingModel = new ServletMappingModel();
Element servletNameEle = (Element) servletMappingEle.selectSingleNode("servlet-name");
ServletNameModel servletNameModel = new ServletNameModel();
servletNameModel.setNr(servletNameEle.getText());
servletMappingModel.setSnm(servletNameModel);
List urlPatternEles = servletMappingEle.selectNodes("url-pattern");
for (Element urlPatternEle : urlPatternEles) {
UrlPatternModel urlPatternModel = new UrlPatternModel();
urlPatternModel.setNr(urlPatternEle.getText());
servletMappingModel.push(urlPatternModel);
}
wam.push(servletMappingModel);
}
} catch (Exception e) {
e.printStackTrace();
}
return wam;
}
public static String getServletClassByUrl(WebAppModel webAppModel, String url) {
String servletClass = "";
String servletName = "";
List servletMappingModels = webAppModel.pop();
for (ServletMappingModel servletMappingModel : servletMappingModels) {
List urlPatternModels = servletMappingModel.getUrlPatternModels();
for (UrlPatternModel urlPatternModel : urlPatternModels) {
if(url.equals(urlPatternModel.getNr())) {
ServletNameModel servletNameModel = servletMappingModel.getSnm();
servletName = servletNameModel.getNr();
}
}
}
List servletModels = webAppModel.getServletModels();
for (ServletModel servletModel : servletModels) {
ServletNameModel servletNameModel = servletModel.getSnm();
if(servletName.equals(servletNameModel.getNr())) {
ServletClassModel servletClassModel = servletModel.getScm();
servletClass = servletClassModel.getNr();
}
}
return servletClass;
}
//测试
public static void main(String[] args) {
WebAppModel webAppModel = WebAppFactory.build("web.xml");
String res = getServletClassByUrl(webAppModel, "/jrebelServlet");
String res2 = getServletClassByUrl(webAppModel, "/jrebelServlet2");
String res3 = getServletClassByUrl(webAppModel, "/jrebelServlet3");
System.out.println(res);
System.out.println(res2);
System.out.println(res3);
}
}
运行结果:
OK,今天Leaf带来的学习笔记分享就到这里啦,我们下次再见!!!



