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配置文件

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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值