当前位置:网站首页>XML建模
XML建模
2022-07-23 05:40:00 【_Leaf1217】
目录
一、学会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>我们先来分析一下这个xml文件:
1) 分析标签
一个根标签:config
一个子标签:action
action内有子标签:forward
2) 分析属性
action:path、type
forward:name、path、redirect
根据上面分析我们可以知道此配置文件一共有三个不同的标签:
config:有子标签、没有属性
action:有子标签
forward:没有子标签
因此我们开始我们建模的第一步,
利用面向对象编程思想分别为不同的标签建设实体类:
ForwardModel:
package com.leaf.mode;
/**
* forward标签
* @author Leaf
*
* 2022年6月15日 上午8:40:48
*/
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;
}
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;
/**
* action标签
* @author Leaf
*
* 2022年6月15日 上午8:41:08
*/
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;
}
public ActionModel() {
// TODO Auto-generated constructor stub
}
public ActionModel(String path, String type) {
super();
this.path = path;
this.type = type;
}
/**
* 两个行为:增加forward对象|查找forward对象
* @param forward对象
*/
//将一个新的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;
/**
* config标签
* @author Leaf
*
* 2022年6月15日 上午8:41:23
*/
import java.util.HashMap;
import java.util.Map;
public class ConfigModel {
private Map<String, ActionModel> aMap = new HashMap<String, ActionModel>();
public void push(ActionModel am) {
aMap.put(am.getPath(), am);
}
public ActionModel pop(String path) {
return aMap.get(path);
}
}二、23种建模方式之工厂模式
但我们为各个标签建好实体类并且写好对应的行为方法后,
就需要建立一个工厂模式类:ConfigModelFactory
首先看一下有关这个工厂模式类的注释:
/**
* 23种设计模式之工厂模式
* ConfigModelFactory就是用来生产ConfigModel对象的
* 生产出来的ConfigModel对象就包含了Config.xml中的配置内容
*
* 此地生产ConfigModel有配置信息
* 1、解析Config.xml中的配置信息
* 2、将相应的配置信息分别加载进行不同的模型对象中
* @author Leaf
*
* 2022年6月15日 上午9:00:42
*/
接着我就直接放上工厂类的代码啦:
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;
/**
* 23种设计模式之工厂模式
* ConfigModelFactory就是用来生产ConfigModel对象的
* 生产出来的ConfigModel对象就包含了Config.xml中的配置内容
*
* 此地生产ConfigModel有配置信息
* 1、解析Config.xml中的配置信息
* 2、将相应的配置信息分别加载进行不同的模型对象中
* @author Leaf
*
* 2022年6月15日 上午9:00:42
*/
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 cm = new ConfigModel();
for (Element actionEle : actionEles) {
ActionModel am = new ActionModel();
am.setPath(actionEle.attributeValue("path"));
am.setType(actionEle.attributeValue("type"));
//将forwardmodel赋值并且添加到Actionmodel中
List<Element> 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
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>jrebelServlet</servlet-name>
<servlet-class>com.leaf.xml.JrebelServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jrebelServlet</servlet-name>
<url-pattern>/jrebelServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>jrebelServlet2</servlet-name>
<servlet-class>com.leaf.xml.JrebelServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jrebelServlet2</servlet-name>
<url-pattern>/jrebelServlet2</url-pattern>
<url-pattern>/jrebelServlet3</url-pattern>
</servlet-mapping>
</web-app>接着我们还是来分析一下整个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;
/**
* WebApp工厂类
* @author Leaf
*
* 2022年6月15日 上午11:34:52
*/
public class WebAppFactory {
/**
* 建模
* @param path xml名
* @return
*/
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<Element> 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);
}
/*
* 将servlet-mapping的标签内容填充进web_app
*/
List<Element> servletMappingEles = doc.selectNodes("/web-app/servlet-mapping");
for (Element servletMappingEle : servletMappingEles) {
ServletMappingModel servletMappingModel = new ServletMappingModel();
/*
* 给servletmapingmodel填充xml的内容
*/
Element servletNameEle = (Element) servletMappingEle.selectSingleNode("servlet-name");
ServletNameModel servletNameModel = new ServletNameModel();
servletNameModel.setNr(servletNameEle.getText());
servletMappingModel.setSnm(servletNameModel);
List<Element> 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;
}
/**
* 通过浏览器输入的网址自动找到对应的后台处理类
* @param webAppModel 建模后的实体类
* @param url 浏览器访问的网址
* @return
*/
public static String getServletClassByUrl(WebAppModel webAppModel, String url) {
String servletClass = "";
/*
* 找到浏览器网址对应的servlet-name
*/
String servletName = "";
List<ServletMappingModel> servletMappingModels = webAppModel.pop();
for (ServletMappingModel servletMappingModel : servletMappingModels) {
List<UrlPatternModel> urlPatternModels = servletMappingModel.getUrlPatternModels();
for (UrlPatternModel urlPatternModel : urlPatternModels) {
if(url.equals(urlPatternModel.getNr())) {
ServletNameModel servletNameModel = servletMappingModel.getSnm();
servletName = servletNameModel.getNr();
}
}
}
/*
* 找到servlet-name对应的后台处理类
*/
List<ServletModel> 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带来的学习笔记分享就到这里啦,我们下次再见!!!
边栏推荐
猜你喜欢

简单实现矩形面积块

my_ Implementation of strcpy (classic, simple, practical, collection)
![[Hudi]hudi的编译及hudi&spark和hudi&flink的简单使用](/img/6f/e6f5ef79c232d9b27a8334cd8ddaa5.png)
[Hudi]hudi的编译及hudi&spark和hudi&flink的简单使用

牛客刷题记录--Mysql
![[python flask notes 5] blueprint is easy to use](/img/0a/00b259f42e2fa83d4871263cc5f184.png)
[python flask notes 5] blueprint is easy to use

The super simple face recognition API can realize face recognition in just a few lines of code

Spark常见面试问题整理

Web server failed to start. Port 8080 was already in use.
[email protected] ‘] failed with code 1"/>NPM init vite app < project name > error install for[‘ [email protected] ‘] failed with code 1

TypeScript 高级类型
随机推荐
高阶函数的应用:手写Promise源码(二)
3. Threads in flask
Niuke question brushing record -- MySQL
Spectral clustering | Laplace matrix
js的事件执行机制(Event loop)
Web server failed to start. Port 8080 was already in use.
The problem that GBK codec cannot decode may be caused by the file name
高阶函数的应用:手写Promise源码(三)
systemctl-service服务添加环境变量及模板
混入视图基类
js的call、apply、bind
TypeScript 常用类型
[untitled]
大厂面试机器学习算法(5)推荐系统算法
[Doris]配置和基本使用contens系统(有时间继续补充内容)
第一篇博客
Copy a project /project in idea
some、every、find、findIndex的用法
uni-app小程序中v-show与display:flex一起使用时v-show不生效!
Application of higher-order functions: handwritten promise source code (I)