目录
一、什么是XML建模
将XML配置文件中的元素、属性、文本信息转换成对象的过程叫做XML建模
二、XML建模
根据xml元素节点创建实体类
建模是由里到外
将xml里的标签当成一个对象,分析他们的行为和属性
案例:
我们分析一下 config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<action path="/regAction" type="test.RegAction">
<forward name="failed" path="/reg.jsp" redirect="false" />
<forward name="success" path="/login.jsp" redirect="true" />
</action>
<action path="/loginAction" type="test.LoginAction">
<forward name="failed" path="/login.jsp" redirect="false" />
<forward name="success" path="/main.jsp" redirect="true" />
</action>
</config>
action建模分析action标签行为和属性
package com.zwc.model;
import java.util.HashMap;
import java.util.Map;
/**
* 对应action标签
* @author Administrator
*
*/
public class ActionModel {
// <action path="/loginAction" type="test.LoginAction">
private String path;
private String type;
private Map<String, ForwardModel> fmap = new HashMap<String, ForwardModel>();
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;
}
//两个行为 增加fowwardModel对象 查找fowwardModel对象
// 将一个新的forward标签对象加入容器
public void push(ForwardModel forwardModel) {
fmap.put(forwardModel.getName(), forwardModel);
}
public ForwardModel pop(String name) {
return fmap.get(name);
}
}
config根元素,分析它的行为
package com.zwc.model;
import java.util.HashMap;
import java.util.Map;
/**
* 根标签对应的对象
* @author Administrator
*
*/
public class ConfigModel {
private Map<String, ActionModel> amap = new HashMap<>();
public void push(ActionModel actionModel) {
amap.put(actionModel.getPath(), actionModel);
}
public ActionModel pop(String path) {
return amap.get(path);
}
}
forward标签,分析它的属性和行为
package com.zwc.model;
/**
* 对应forward标签
* @author Administrator
*
*/
public class ForwardModel {
// <forward name="success" path="/main.jsp" redirect="true" />
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;
}
}
工厂类:
23种设计模式之工厂模式
ConfigModelFactory就是用来configmodel对象的
生产出来的configmodel对象就包含config.xml的配置内容
代码如下:
package com.zwc.model;
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;
/**
* 23种设计模式之工厂模式
* sessionfactory
* ConfigModelFactory就是用来configmodel对象的
* 生产出来的configmodel对象就包含config.xml的配置内容
*
* 此地生产congifmodel有配置信息?
* 1、解析config.xml中的配置信息
* 2、将对应的配置信息加载进不同的模型对象中
* @author Administrator
*
*/
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<Element> actionEles = doc.selectNodes("/config/action");
ConfigModel configmodel = new ConfigModel();
for (Element actionELe : actionEles) {
ActionModel actionModel = new ActionModel();
actionModel.setPath(actionELe.attributeValue("path"));
actionModel.setType(actionELe.attributeValue("type"));
// 京forwardmodel赋值并且添加到actionmodel中
List<Element> forwardEles = actionELe.selectNodes("forward");
for (Element element : forwardEles) {
ForwardModel forwardModel = new ForwardModel();
forwardModel.setName(element.attributeValue("name"));
forwardModel.setPath(element.attributeValue("path"));
forwardModel.setRedirect("true".equals(element.attributeValue("redirect")));
actionModel.push(forwardModel);
}
configmodel.push(actionModel);
}
return configmodel;
}
public static ConfigModel bulid() throws Exception {
String defaultPath = "/config.xml";
return bulid(defaultPath);
}
}
运行结果
找到第二个action属性type内容,及第二个action中第二个forward里的path属性内容
三、案例
对web.xml进行建模
通过url-pattern读取到servlet-class
下面是关于web.xml分析所建的类
web-app根元素分析它的行为
package com.zwc.web;
import java.util.ArrayList;
import java.util.List;
public class WebAppModel {
private List<ServletModel> list = new ArrayList<ServletModel>();
private List<ServletMappingModel> lis = new ArrayList<ServletMappingModel>();
public void push(ServletModel servletmodel) {
list.add(servletmodel);
}
public List<ServletModel> getAllServlet() {
return list;
}
public void push(ServletMappingModel servletMappingModel) {
lis.add(servletMappingModel);
}
public List<ServletMappingModel> getAllMap() {
return lis;
}
}
servlet标签
package com.zwc.web;
public class ServletModel {
private ServletNameModel name;
private ServletClassModel Serlvetclass;
public ServletNameModel getName() {
return name;
}
public void setName(ServletNameModel name) {
this.name = name;
}
public ServletClassModel getSerlvetclass() {
return Serlvetclass;
}
public void setSerlvetclass(ServletClassModel serlvetclass) {
Serlvetclass = serlvetclass;
}
}
servlet-mapping标签
package com.zwc.web;
import java.util.ArrayList;
import java.util.List;
public class ServletMappingModel {
private ServletNameModel model;
private List<UrlPatternModel> UrlModel = new ArrayList<UrlPatternModel>();
public ServletNameModel getModel() {
return model;
}
public void setModel(ServletNameModel model) {
this.model = model;
}
public void pushUrl(UrlPatternModel rlModel) {
UrlModel.add(rlModel);
}
public List<UrlPatternModel> getUrl() {
return UrlModel;
}
}
servlet-name标签
package com.zwc.web;
public class ServletNameModel {
private String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
selrvet-class标签
package com.zwc.web;
public class ServletClassModel {
private String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
url-pattern标签
package com.zwc.web;
public class UrlPatternModel {
private String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
工厂类
WebAppModelFactory生产webAppModel对象:
package com.zwc.web;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
/**
* WebAppModelFactory生产webAppModel对象
* @author liuruojuan
*/
public class WebAppModelFactory {
/**
* 建模
*
* @param xmlPath
* @return
*/
public static WebAppModel bulid() throws Exception {
String defalut = "/web.xml";
InputStream in = WebAppModelFactory.class.getResourceAsStream(defalut);
SAXReader sr = new SAXReader();
Document doc = sr.read(in);
/*
* 将servlet的标签内容填充进WebApp
*/
List<Element> servletEles = doc.selectNodes("/web-app/servlet");
WebAppModel WebAppModel = new WebAppModel();
for (Element servletEle : servletEles) {
/*
* 给ServletModel填充xml的内容
*/
ServletModel servletmodel = new ServletModel();
Element serlvetname = (Element) servletEle.selectSingleNode("servlet-name");
Element serlvetClass = (Element) servletEle.selectSingleNode("servlet-class");
ServletNameModel servletNameModel = new ServletNameModel();
ServletClassModel servletClassModel = new ServletClassModel();
servletNameModel.setContext(serlvetname.getText());
servletClassModel.setContext(serlvetClass.getText());
servletmodel.setName(servletNameModel);
servletmodel.setSerlvetclass(servletClassModel);
WebAppModel.push(servletmodel);
}
/*
* 将servlet-mapping的标签内容填充进WebApp
*/
List<Element> serlvetMapping = doc.selectNodes("web-app/servlet-mapping");
for (Element element : serlvetMapping) {
ServletMappingModel servletMappingModel = new ServletMappingModel();
Element serlvetName = (Element) element.selectSingleNode("servlet-name");
ServletNameModel servletName = new ServletNameModel();
servletName.setContext(serlvetName.getText());
servletMappingModel.setModel(servletName);
List<Element> urlpattern = element.selectNodes("url-pattern");
for (Element urlpatternEle : urlpattern) {
UrlPatternModel urlpattern1 = new UrlPatternModel();
urlpattern1.setContext(urlpatternEle.getText());
servletMappingModel.pushUrl(urlpattern1);
}
WebAppModel.push(servletMappingModel);
}
return WebAppModel;
}
/**
* 通过浏览器输入的网址自动找到对应的后台处理类
* @param webAppModel 建模后的实体类
* @param url 浏览器访问的网址
* @return
*/
public static String getServletClassByUrl(WebAppModel webAppModel, String url) {
String servletClass = "";
String servletName = "";
/*
* 找到浏览器网址对应的servlet-name
*/
List<ServletMappingModel> servletMappingModels = webAppModel.getAllMap();
for (ServletMappingModel servletMappingModel : servletMappingModels) {
List<UrlPatternModel> urlPatternModels = servletMappingModel.getUrl();
for (UrlPatternModel urlPatternModel : urlPatternModels) {
if(url.equals(urlPatternModel.getContext())) {
ServletNameModel servletNameModel = servletMappingModel.getModel();
servletName = servletNameModel.getContext();
}
}
}
/*
* 找到servlet-name对应的后台处理类
*/
List<ServletModel> servletModels = webAppModel.getAllServlet();
for (ServletModel servletModel : servletModels) {
ServletNameModel servletNameModel = servletModel.getName();
if(servletName.equals(servletNameModel.getContext())) {
ServletClassModel servletClassModel = servletModel.getSerlvetclass();
servletClass = servletClassModel.getContext();
}
}
return servletClass;
}
public static void main(String[] args) throws Exception {
WebAppModel web = WebAppModelFactory.bulid();
String res1 = getServletClassByUrl(web, "/jrebelServlet");
String res2 = getServletClassByUrl(web, "/jrebelServlet2");
System.out.println(res1);
System.out.println(res2);
}
}
运行结果: