IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> J2EE基础:XML的建模 -> 正文阅读

[Java知识库]J2EE基础:XML的建模

本节知识总结:


目录

本节知识总结:

前言

一、建模由来

二、建模思路

三、建模步骤

练习:?

总结


前言

今天我们来学习与上节不同的一个解析xml配置文件的方法:建模


一、建模由来

建模的由来:将指定的xml字符串当做对象来操作

二、建模思路

思路:

1.要分析需要被建模的文件中有哪几个对象

2.每个对象拥有的行为以及属性

3.定义一个从里到外的对象

4.通过23重设计模式中的工厂模式,解析xml生产指定对象

作用:提高代码反复使用性??

建模方式:由内到外

根据上面思路我们可以通过一个小案例来解析~?


三、建模步骤

1.以面向对象的编程思想,描述xml资源文件

2.将xml文件中的内容封装到model实体对象中

通过案例config.xml解析

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE config[
?? ?<!ELEMENT config (action*)>
?? ?<!ELEMENT action (forward*)>
?? ?<!ELEMENT forward EMPTY>
?? ?<!ATTLIST action
?? ? ?path CDATA #REQUIRED
?? ? ?type CDATA #REQUIRED
?? ?>
?? ?<!ATTLIST forward
?? ? ?name CDATA #REQUIRED
?? ? ?path CDATA #REQUIRED
?? ? ?redirect (true|false) "false"
?? ?>
]>
<!-- config标签:可以包含0~N个action标签 -->
<config>
?? ?<!-- action标签:可以饱含0~N个forward标签 path:以/开头的字符串,并且值必须唯一 非空 type:字符串,非空 -->
?? ?<action path="/regAction" type="test.RegAction">
?? ??? ?<!-- forward标签:没有子标签; name:字符串,同一action标签下的forward标签name值不能相同 ; path:以/开头的字符串?
?? ??? ??? ?redirect:只能是false|true,允许空,默认值为false -->
?? ??? ?<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>

👍根据思路分析config文件

1.要分析需要被建模的文件中有哪几个对象

①config对象②action对象③forward对象

2.每个对象拥有的行为以及属性

在action中有增加(压栈)和获得(弹栈)

3.定义一个从里到外的对象

先forward>action>config

4.通过23重设计模式中的工厂模式,解析xml生产指定对象

分别建立ForwardModel、ActionModel、ConfigModel? ? ? ?

建立工厂:ConfigModelFactory

ForwardModel类:

package com.xiehuiyi.xml;

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类:

package com.xiehuiyi.xml;
/**
 * 在actionmodel里面有增加,获得(查找)
 * @author zjjt
 *
 */

import java.util.HashMap;
import java.util.Map;

public class ActionModel {
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 void push(ForwardModel forwardModel) {
	fMap.put(forwardModel.getName(), forwardModel);
}
//弹栈
public ForwardModel pop(String name) {
	return fMap.get(name);
}
}

ConfigModel类:?

package com.xiehuiyi.xml;

import java.util.HashMap;
import java.util.Map;

public class ConfigModel {
	private Map<String, ActionModel> acMap=new HashMap<String, ActionModel>();
//压栈
	public void push(ActionModel actionModel) {
		acMap.put(actionModel.getPath(), actionModel);
	}
	//弹栈
	public ActionModel pop(String path) {
		return acMap.get(path);
	}
public static void main(String[] args) throws Exception {
	//此时config没值    建模
//	ConfigModel configModel=new ConfigModel();
	

ConfigModel configModel=ConfigModelFactory.build();
	
	
	//案例:2.获得第二个action中的type的值
	ActionModel actionModel=configModel.pop("/loginAction");
	System.out.println(actionModel.getType());
	ForwardModel forwardModel = actionModel.pop("success");
	//3.获得第二个action中所有forword的path的值
	//
	//4.获得第二个action中第二个forword的path的值
	
}
}

ConfigModelFactory类:

package com.xiehuiyi.xml;

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;

/**
 * 建模的思路:
 * 1.将原有的config.xml进行解析
 * 2.对应标签的内容,将其封装赋值给相应的对象
 * 。。。。。。
 * @author zjjt
 *
 */
public class ConfigModelFactory {
 public static ConfigModel build() throws Exception {
	 return build("config.xml");
 }
 public static ConfigModel build(String resourcepath) throws Exception {
	 InputStream in = ConfigModelFactory.class.getResourceAsStream(resourcepath);
	 SAXReader saxReader=new SAXReader();
	 Document doc=saxReader.read(in);
	 ConfigModel configModel=new ConfigModel();
	 List<Element> actionEles=doc.selectNodes("/config/action");
	for (Element actionEle : actionEles) {
		ActionModel actionModel=new ActionModel();
		//将xml文件解析得来的path赋值给actionModel
		actionModel.setPath(actionEle.attributeValue("path"));
		actionModel.setType(actionEle.attributeValue("type"));
		List<Element> forwardEles=actionEle.selectNodes("forward");
		for (Element forwardEle : forwardEles) {
			ForwardModel forwardModel=new ForwardModel();
			forwardModel.setName(forwardEle.attributeValue("name"));
			forwardModel.setPath(forwardEle.attributeValue("path"));
			//Redirect只有在配置文件中赋值false的时候,代表转发,其代表重定向
			forwardModel.setRedirect(!"false".equals(forwardEle.attributeValue("redirect")));
			actionModel.push(forwardModel);
		}
		configModel.push(actionModel);
	}
	 
	 return configModel;
 }
}

练习:?

案例解析web.xml😀

<?xml version="1.0" encoding="UTF-8"?>
<web-app>//根对象
? <servlet>
? ?? ?<servlet-name>jrebelServlet</servlet-name>
? ?? ?<servlet-class>com.zking.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.zking.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>

?*用上面思路分析得知对象有:红色字体

建包:

ServletNameModel类:

package com.xiehuiyi.xml1;

public class ServletNameModel {
	private String context;

	public String getContext() {
		return context;
	}

	public void setContext(String context) {
		this.context = context;
	}
}

ServletClass类:

package com.xiehuiyi.xml1;

public class ServletClassModel {
	private String context;

	public String getContext() {
		return context;
	}

	public void setContext(String context) {
		this.context = context;
	}
}

UrlPatternModel类:

package com.xiehuiyi.xml1;

public class UrlPatternModel {
	private String context;

	public String getContext() {
		return context;
	}

	public void setContext(String context) {
		this.context = context;
	}

}

ServletModel类:

package com.xiehuiyi.xml1;

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;
	}
}

ServletMappingModel类:

package com.xiehuiyi.xml1;

import java.util.ArrayList;
import java.util.List;

public class ServletMappingModel {
	private ServletNameModel servletNameModel;
	private List<UrlPatternModel> urlPatternModels = new ArrayList<UrlPatternModel>();
	public ServletNameModel getServletNameModel() {
		return servletNameModel;
	}
	public void setServletNameModel(ServletNameModel servletNameModel) {
		this.servletNameModel = servletNameModel;
	}
	
	public void pushUrlPatternModel(UrlPatternModel urlPatternModel) {
		urlPatternModels.add(urlPatternModel);
	}
	
	public List<UrlPatternModel> getUrlPatternModels() {
		return urlPatternModels;
	}
}

WebAppModel类:

package com.xiehuiyi.xml1;

import java.util.ArrayList;
import java.util.List;

public class WebAppModel {
	private List<ServletModel> servletModels = new ArrayList<ServletModel>();
	private List<ServletMappingModel> servletMappingModels = new ArrayList<>();

	public void pushServletModel(ServletModel servletModel) {
		servletModels.add(servletModel);
	}
	
	public List<ServletModel> getServletModels() {
		return servletModels;
	}
	
	public void pushServletMappingModel(ServletMappingModel servletMappingModel) {
		servletMappingModels.add(servletMappingModel);
	}
	
	public List<ServletMappingModel> getServletMappingModels() {
		return servletMappingModels;
	}
}

?工厂类WebAppModelFactory类:

package com.xiehuiyi.xml1;

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 buildWebAppModel() {
		String xmlPath = "/web.xml";
		return buildWebAppModel(xmlPath);
	}

	/**
	 * 建模
	 * 
	 */
	public static WebAppModel buildWebAppModel(String xmlPath) {
		InputStream in = WebAppModelFactory.class.getResourceAsStream(xmlPath);
		SAXReader saxReader = new SAXReader();
		WebAppModel webAppModel = new WebAppModel();
		try {
			Document doc = saxReader.read(in);
			/*
			 * 将servlet的标签内容填充进WebApp
			 */
			List<Element> servletEles = doc.selectNodes("/web-app/servlet");
			for (Element servletEle : servletEles) {
				ServletModel servletModel = new ServletModel();

				/*
				 * 给ServletModel填充xml的内容
				 */
				Element servletNameEle = (Element) servletEle.selectSingleNode("servlet-name");
				Element servletClassEle = (Element) servletEle.selectSingleNode("servlet-class");
				ServletNameModel servletNameModel = new ServletNameModel();
				ServletClassModel servletClassModel = new ServletClassModel();
				servletNameModel.setContext(servletNameEle.getText());
				servletClassModel.setContext(servletClassEle.getText());
				
				servletModel.setServletNameModel(servletNameModel);
				servletModel.setservletclass(servletClassModel);

				webAppModel.pushServletModel(servletModel);
			}






			/*
			 * 将servlet-mapping的标签内容填充进WebApp
			 */
			List<Element> servletMappingEles = doc.selectNodes("/web-app/servlet-mapping");
			for (Element servletMappingEle : servletMappingEles) {
				ServletMappingModel servletMappingModel = new ServletMappingModel();

				/*
				 * 给ServletMappingModel填充xml的内容
				 */
				Element servletNameEle = (Element) servletMappingEle.selectSingleNode("servlet-name");
				ServletNameModel servletNameModel = new ServletNameModel();
				servletNameModel.setContext(servletNameEle.getText());
				servletMappingModel.setServletNameModel(servletNameModel);
				
				List<Element> urlPatternEles = servletMappingEle.selectNodes("url-pattern");
				for (Element urlPatternEle : urlPatternEles) {
					UrlPatternModel urlPatternModel = new UrlPatternModel();
					urlPatternModel.setContext(urlPatternEle.getText());
					servletMappingModel.pushUrlPatternModel(urlPatternModel);
				}

				webAppModel.pushServletMappingModel(servletMappingModel);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return webAppModel;
	}
	
	/**
	 * 通过浏览器输入的网址自动找到对应的后台处理类
	 * 
	 */
	public static String getServletClassByUrl(WebAppModel webAppModel, String url) {
		String servletClass = "";
		/*
		 * 找到浏览器网址对应的servlet-name
		 */
		String servletName = "";
		List<ServletMappingModel> servletMappingModels = webAppModel.getServletMappingModels();
		for (ServletMappingModel servletMappingModel : servletMappingModels) {
			List<UrlPatternModel> urlPatternModels = servletMappingModel.getUrlPatternModels();
			for (UrlPatternModel urlPatternModel : urlPatternModels) {
				if(url.equals(urlPatternModel.getContext())) {
					ServletNameModel servletNameModel = servletMappingModel.getServletNameModel();
					servletName = servletNameModel.getContext();
				}
			}
		}
		
		/*
		 * 找到servlet-name对应的后台处理类
		 */
		List<ServletModel> 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) {
		WebAppModel webAppModel = WebAppModelFactory.buildWebAppModel();
		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);
		
	}

总结

XML建模就是利用工厂模式+dom4j+xpath解析Xml配置文件

以上内容就是今天全部内容,希望能够帮到你,今天内容就在这了!我们下期再见!

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-07-31 16:29:19  更:2021-07-31 16:30:32 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/2 5:03:07-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码