目录
一.XML建模
二.工厂模式
三.案例
1.对web.xml进行建模
2.写一个servlet
3.通过url-pattern读取到servlet-class的值
一.XML建模
建模的原理:以面向对象的思想操作
优先从里 进行编码,从里之外
config.xml表的代码块:
所以的表:
ForwardModel表的代码块:只有属性,没有方法,因为它是空标签
package com.jiangwenjuan.model;
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;
}
}
ActionModel表:有属性也也两中方法
怎么样才能够新增的思路:
这个就想到与一个容器:你去config表里action里面加一个forward就相当于加了一个空的容器
private MapfMap = new HashMap ();
ActionModel的代码块:
package com.jiangwenjuan.model;
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;
}
// 两个行为:增加forwardModel对象,查找forwardModel对象
// 将一个新的forward标签对象加入容器
public void push(ForwardModel forwardModel) {
//加到这个容器里面去
fMap.put(forwardModel.getName(), forwardModel);
}
public ForwardModel pop(String name) {
return fMap.get(name);
}
}
代码块的理解:
对于最外面的ConfigModel表: ConfigModel没有属性只有方法
万一path有相同的怎么办:所以前面有说值必须是唯一的,如果是相同,当你在压榨的时候,相同的P,你放个不同P,此时你先放进去的就会被覆盖掉。
ConfigModel代码块:
package com.jiangwenjuan.model;
import java.util.HashMap;
import java.util.Map;
public class ConfigModel {
private Map aMap = new HashMap();
// 两个方法:新增
public void push(ActionModel actionModel) {
aMap.put(actionModel.getPath(), actionModel);
}
public ActionModel pop(String path) {
return aMap.get(path);
}
}
现在通过configmodel拿这个action对象,拿到action对象,在拿里面的type的。
为什么会报空指针异常:因为目前为止现在就只键了一个空壳而已。就会报第三出错.
怎么把configModel和config.xml关联起来,这才是重点 ,刚刚我们只是以面向对象思想去分析和去封装config.xml,但是分析完了之后,你要把config.xml里的内容塞到哪个空壳里面去。那么这一个过程就是把我们的配置文件中的内容封装到实体里面去,这个就称为建模,在这个过程中用到了23种设计模式之工厂模式.
二.工厂模式
Factory:工厂模式的意思。工厂工厂就是用来生产东西的。工厂有个特点:这个工厂类都会以Factory结尾。
ConfigModelFactory代码块:
package com.jiangwenjuan.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;
public class ConfigModelFactory {
public static ConfigModel bulid() throws Exception {
String defaultPath = "/config.xml";
InputStream in = ConfigModelFactory.class.getResourceAsStream(defaultPath);
SAXReader sr = new SAXReader();
Document doc = sr.read(in);
// 拿到这里面所有的内容
List actionEles = doc.selectNodes("/config/action");
ConfigModel configModel = new ConfigModel();
for (Element actionEle : actionEles) {//目前只有actionEle里面有值
// 加之前先构建一个actionmodel,这里面还没有值
ActionModel actionModel = new ActionModel();
actionModel.setPath(actionEle.attributeValue("path"));
actionModel.setType(actionEle.attributeValue("type"));
// 还要塞forward,将forwardmodel辅助并且添加到Actionmodel中,怎么拿,去actionEle里面拿
List forwardEles = actionEle.selectNodes("forward");
for (Element element : forwardEles) {
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")));
actionModel.push(forwardModel);
}
// 塞到configmodel里面去
configModel.push(actionModel);
}
return configModel;
}
}
通过这个就可以查找到config.xml里面的内容:
package com.jiangwenjuan.model;
public class Demo1 {
public static void main(String[] args) throws Exception {
// ConfigModel configModel = new ConfigModel();
ConfigModel configModel = ConfigModelFactory.bulid();
ActionModel actionModel = configModel.pop("/loginAction");
System.out.println(actionModel.getType());
}
}
案例:拿到config.xml里面第二个action里面的第二个forward里面的path值
不管config.xml有多么复杂,往后只要找东西 ,只要构建对象就可以了,先要去读框架的配置文件,建模。对于很多框架而言不是上面那样写的,是下面这样写的.
这有什么好处呢?你的配置文件变了名字,我可以直接调用它的方法就可以了,并不一定去调用最下面那个方法里面的名字。
package com.jiangwenjuan.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;
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 configModel = new ConfigModel();
for (Element actionEle : actionEles) {//目前只有actionEle里面有值
// 加之前先构建一个actionmodel,这里面还没有值
ActionModel actionModel = new ActionModel();
actionModel.setPath(actionEle.attributeValue("path"));
actionModel.setType(actionEle.attributeValue("type"));
// 还要塞forward,将forwardmodel辅助并且添加到Actionmodel中,怎么拿,去actionEle里面拿
List forwardEles = actionEle.selectNodes("forward");
for (Element element : forwardEles) {
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")));
actionModel.push(forwardModel);
}
// 塞到configmodel里面去
configModel.push(actionModel);
}
return configModel;
}
public static ConfigModel bulid() throws Exception {
String defaultPath = "/config.xml";
return bulid(defaultPath);
}
}
三.案例
1.对web.xml进行建模
web.xml表代码块:
jrebelServlet com.jiangwenjuan.xml.JrebelServlet jrebelServlet /jrebelServlet jrebelServlet2 com.jiangwenjuan.xml.JrebelServlet2 jrebelServlet2 /jrebelServlet2 /jrebelServlet3
2.写一个servlet
3.通过url-pattern读取到servlet-class的值
ServletClassModel表:
package com.jiangwenjuan.serlvet;
public class ServletClassModel {
//没有属性,有文本内容
// com.jiangwenjuan.xml.JrebelServlet2
private String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
ServletMappingModel表:
package com.jiangwenjuan.serlvet;
import java.util.ArrayList;
import java.util.List;
public class ServletMappingModel {
private ServletNameModel servletNameModel;
private List urlPatternModels = new ArrayList<>();
public ServletNameModel getServletNameModel() {
return servletNameModel;
}
public void setServletNameModel(ServletNameModel servletNameModel) {
this.servletNameModel = servletNameModel;
}
public void pushUrlPatternModel(UrlPatternModel urlPatternModel) {
urlPatternModels.add(urlPatternModel);
}
public List getUrlPatternModels() {
return urlPatternModels;
}
}
ServletModel表:
package com.jiangwenjuan.serlvet;
public class ServletModel {
private ServletNameModel servletNameModel;
private ServletClassModel servletClassModel;
public ServletNameModel getServletNameModel() {
return servletNameModel;
}
public void setServletNameModel(ServletNameModel servletNameModel) {
this.servletNameModel = servletNameModel;
}
public ServletClassModel getServletClassModel() {
return servletClassModel;
}
public void setServletClassModel(ServletClassModel servletClassModel) {
this.servletClassModel = servletClassModel;
}
}
ServletNameModel表:
package com.jiangwenjuan.serlvet;
public class ServletNameModel {
//没有属性,有文本内容
// jrebelServlet2
private String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
UrlPatternModel表:
package com.jiangwenjuan.serlvet;
public class UrlPatternModel {
//没有属性,有文本内容
// jrebelServlet2
private String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
WebAppModel表:
package com.jiangwenjuan.serlvet;
import java.util.ArrayList;
import java.util.List;
public class WebAppModel {
private List servletModels = new ArrayList<>();
private List servletMappingModels = new ArrayList<>();
public void pushServletModel(ServletModel servletModel) {
servletModels.add(servletModel);
}
public List getServletModels() {
return servletModels;
}
public void pushServletMappingModel(ServletMappingModel servletMappingModel) {
servletMappingModels.add(servletMappingModel);
}
public List getServletMappingModels() {
return servletMappingModels;
}
}
WebAppModelFactory表:
package com.jiangwenjuan.serlvet;
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;
public class WebAppModelFactory {
public static WebAppModel bulid() throws Exception {
String xmlPath = "/web.xml";
return bulid(xmlPath);
}
public static WebAppModel bulid(String path) throws Exception {
InputStream in = WebAppModelFactory.class.getResourceAsStream(path);
SAXReader sr = new SAXReader();
WebAppModel webAppModel = new WebAppModel();
Document doc = sr.read(in);
//拿到所有的内容
List servletEles = doc.selectNodes("/web-app/servlet");
for (Element serlvetEle : servletEles) {
ServletModel selvetModel = new ServletModel();
Element servletnameEle = (Element)serlvetEle.selectSingleNode("servlet-name");
Element serlvetclassEle = (Element)serlvetEle.selectSingleNode("servlet-class");
ServletNameModel servletNameModel = new ServletNameModel();
ServletClassModel servletClassModel = new ServletClassModel();
servletNameModel.setContext(servletnameEle.getText());
servletClassModel.setContext(serlvetclassEle.getText());
selvetModel.setServletClassModel(servletClassModel);
selvetModel.setServletNameModel(servletNameModel);
webAppModel.pushServletModel(selvetModel);
}
List servletmappingEles = doc.selectNodes("/web-app/serlvet-mapping");
for (Element serlvetmappingEle : servletmappingEles) {
ServletMappingModel servletMappingModel = new ServletMappingModel();
Element servletnameEle = (Element)serlvetmappingEle.selectSingleNode("servlet-name");
ServletNameModel servletNameModel = new ServletNameModel();
servletNameModel.setContext(servletnameEle.getText());
List urlpatternEles = serlvetmappingEle.selectNodes("url-pattern");
for (Element urlpatternEle : servletmappingEles) {
UrlPatternModel urlPatternModel = new UrlPatternModel();
urlPatternModel.setContext(urlpatternEle.getText());
servletMappingModel.pushUrlPatternModel(urlPatternModel);
}
webAppModel.pushServletMappingModel(servletMappingModel);
}
return webAppModel;
}
public static String getServletClassByUrl(WebAppModel webAppModel,String url) {
String servletClass = "";
String servletName = "";
List servletMappingModels = webAppModel.getServletMappingModels();
for (ServletMappingModel servletMappingModel : servletMappingModels) {
List urlPatternModels = servletMappingModel.getUrlPatternModels();
for (UrlPatternModel urlPatternModel : urlPatternModels) {
if(url.equals(urlPatternModel.getContext())) {
ServletNameModel servletNameModel = new ServletNameModel();
servletName = servletNameModel.getContext();
}
}
}
List servletModels = webAppModel.getServletModels();
for (ServletModel servletModel : servletModels) {
ServletNameModel servletNameModel = servletModel.getServletNameModel();
if(servletName.equals(servletNameModel.getContext())) {
ServletClassModel servletClassModel = servletModel.getServletClassModel();
servletClass = servletClassModel.getContext();
}
}
return servletClass;
}
public static void main(String[] args) throws Exception {
WebAppModel webAppModel = WebAppModelFactory.bulid();
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);
}
}
最终结果:



