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知识库 -> xml建模 -> 正文阅读

[Java知识库]xml建模

1.什么是XML建模
将XML配置文件中的元素、属性、文本信息转换成对象的过程叫做XML建模

2. XML建模的步骤
建模分两步:
1、以面向对象的编程思想,描述xml资源文件
2、将xml文件中内容封装进model实体对象

ConfigModel

ActionModel

ForwardModel

首先config.xml里面的元素是从里(①forward? ?②action? ?③config)到外取出

<?xml version="1.0" encoding="UTF-8"?>
?? ?<!--
?? ??? ?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>

? ?A.config节点下有多个子action节点,无节点属性
? ?B.action节点下有多个子forward节点,有节点属性
? ?C.forward下无子节点,有节点属性

?

ForwardModel

package com.hmf;

import java.io.Serializable;

/**
?* config点xmlforwar节点对应的建模实体类ForwardModel
?* @author T440s
?*
?*/
public class ForwardModel implements Serializable{
?? ?//forwar节点对应的name属性
?? ?private String name;
?? ?//forwar节点对应的path属性
?? ?private String path;
?? ?//forwar节点对应的redirect属性
?? ?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.hmf;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
?* 将xml放入到action实体类
?* @author T440s
?*
?*/
public class ActionModel implements Serializable{
?? ?private String path;
?? ?private String type;
?? ?
?? ?//在同一个action下的name?
?? ?//key 代表forward节点的name属性,唯一
?? ?//value,将整个forward节点所对应的建模实体类ForwardModel作为value的值
?? ?private Map<String, ForwardModel> forwards=new HashMap<String, ForwardModel>();
?? ?
?? ?//压栈
?? ?public void push(ForwardModel ForwardModel1) {
?? ??? ?forwards.put(ForwardModel1.getName(),ForwardModel1);
?? ?}

?? ?
?? ?//弹栈
?? ?public ForwardModel get(String name) {
?? ??? ?return forwards.get(name);
?? ?}

?? ?
?? ?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;
?? ?}
?? ?@Override
?? ?public String toString() {
?? ??? ?return "ActionModel [path=" + path + ", type=" + type + "]";
?? ?}
?? ?
?? ?public ActionModel(String path, String type) {
?? ??? ?super();
?? ??? ?this.path = path;
?? ??? ?this.type = type;
?? ?}
?? ?public ActionModel() {
?? ??? ?// TODO Auto-generated constructor stub
?? ?}
}
?

ConfigModel

package com.hmf;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class ConfigModel implements Serializable{
?? ?
?? ?private Map<String, ActionModel> actions=new HashMap<String, ActionModel>();
?? ?
?? ?public Map<String, ActionModel> getAction(){
?? ??? ?return actions;
?? ?}
?? ?
?? ?public void setAction (Map<String, ActionModel> actions){
?? ??? ?this.actions=actions;
?? ?}
?? ?
?? ?@Override
?? ?public String toString() {
?? ??? ?return "ConfigModel [actions=" + actions + "]";
?? ?}
?? ?
?? ?public ConfigModel() {
?? ??? ?// TODO Auto-generated constructor stub
?? ?}

?? ?public ConfigModel(Map<String, ActionModel> actions) {
?? ??? ?super();
?? ??? ?this.actions = actions;
?? ?}
?? ?
?? ?//压栈
?? ?public void push(ActionModel actionModel) {
?? ??? ?actions.put(actionModel.getPath(),actionModel);
?? ?}
?? ?//弹栈
?? ?public ActionModel get(String path) {
?? ??? ?return actions.get(path);

? ? }

????public static void main(String[] args) {
?? ??? ?????????//ConfigModel对象里没有xml内容
?? ??? ?????????ConfigModel configModel=new ConfigModel();
????????????? }
?? ?}

ConfigModelFactory

package com.hmf;

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.对应标签内容,将其封装赋值给相应的对象
?* ?forward标签值 赋值给forwardModel对象
?* ?action标签值 赋值给ActionModel对象
?* ?config标签值 赋值给ConModel对象
?* @author T440s
?*
?*/
public class ConfigModelFactory {
?? ?public static ConfigModel build() throws DocumentException {
?? ??? ?return build("config.xml");
?? ?}
?? ?
?? ?public static ConfigModel build(String resourcepath) throws DocumentException {
?? ??? ?InputStream in = ConfigModelFactory.class.getResourceAsStream(resourcepath);
?? ??? ?SAXReader sax=new SAXReader();
?? ??? ?Document doc = sax.read(in);
?? ??? ?ConfigModel config=new ConfigModel();
?? ??? ?List<Element> acList = doc.selectNodes("/config/action");
?? ??? ?for (Element actEle : acList) {
?? ??? ??? ?ActionModel actionModel=new ActionModel();
?? ??? ??? ?//将xml文件解析的来的path值赋值给actionModel对象中的path属性
?? ??? ??? ?actionModel.setPath(actEle.attributeValue("path"));
?? ??? ??? ?actionModel.setType(actEle.attributeValue("type"));
?? ??? ??? ?List<Element> forwardEles = actEle.selectNodes("forwardEles");
?? ??? ??? ?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")));
?? ??? ??? ??? ?config.push(actionModel);
?? ??? ??? ?}
?? ??? ??? ?config.push(actionModel);
?? ??? ?}
?? ??? ?return config;
?? ?}
}
?

例如:

创建一个servlet进行建模

通过url-pattern读取到servlet-class的值

<?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>

package com.hmf01;

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() throws Exception{
?? ??? ?return buildWebAppModel("/web.xml");
?? ?}

?? ?
?? ?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);
?? ??? ??? ?
?? ??? ??? ?List<Element> servletEles = doc.selectNodes("/web-app/servlet");
?? ??? ??? ?for (Element servletEle : servletEles) {
?? ??? ??? ??? ?ServletModel servletModel = new ServletModel();

?? ??? ??? ??? ?
?? ??? ??? ??? ?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.setServletClassModel(servletClassModel);

?? ??? ??? ??? ?webAppModel.pushServletModel(servletModel);
?? ??? ??? ?}

?? ??? ??? ?
?? ??? ??? ?List<Element> 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.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 = "";
?? ??? ?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();
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?
?? ??? ?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) throws Exception {
?? ??? ?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);
?? ?}
}
?

好了今天到这里了?

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-07-31 16:29:19  更:2021-07-31 16:31:31 
 
开发: 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/5 8:00:03-

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