1、关于XML建模:
为什么需要XML建模:当有多人需要获取同一个资源文件的内容时,为了节省时间提高效率
什么是XML建模:就是将指定的XML字符串当成对象来操作。
好处:提高代码的复用性
建模分析:
①分析需要被建模的文件中有那几个对象
②每个对象拥有的行为以及属性
③定义对象从小到大(从里到外)
④通过23种的设计模式中的工厂模式,解析xml生产出指定对象
建模分两步:
1、以面向对象的编程思想,描述xml资源文件
2、将xml文件中内容封装进model实体对象。
1、尝试解析一个XML文件(描述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>
解析思路:
从小到大:forward>action>config
各自拥有的属性以及行为:
forward拥有的属性:name、path、redirect
Action的属性:path、type
action的行为:新增(forward)、查询
Config的行为:新增(action)、查询
2、将xml文件中内容封装进model实体对象。
ForwardModel类:
package com.songwanxi.Model;
public class ForwardModel {
private String name;
private String path;
private boolean redirect = true;
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.songwanxi.Model;
import java.util.HashMap;
import java.util.Map;
public class ActionModel {
private String path;
private String type;
private Map<String, ForwardModel> fmod = new HashMap<>();//因为forward的name是唯一的,使用使用name做key
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 puth (ForwardModel forwardModel) {
fmod.put(forwardModel.getName(), forwardModel);
}
public ForwardModel pop(String name) {
return fmod.get(name);
}
}
ConfigModel类:
package com.songwanxi.Model;
import java.util.HashMap;
import java.util.Map;
public class ConfigModel {
private Map<String, ActionModel> amap = new HashMap<>();
/***
* 新增
*/
public void push (ActionModel actionModel) {
amap.put(actionModel.getPath(), actionModel);
}
/**
* 查询
*/
public ActionModel pop (String path) {
return amap.get(path);
}
}
接着我们要建立一个工厂来自动解析xml文件:
ConfigModelFactory 类:
package com.songwanxi.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;
/**
* 工厂模式:为了生产重复需求对象,提高代码的复用性
* @author 10570
*
*/
public class ConfigModelFactory {
/**
* 默认资源文件调用
* @return
* @throws Exception
*/
public static ConfigModel build() throws Exception {
return build("config.xml");
}
/**
* 当资源文件需要手动改变位置的情况下,那么就调用该方法,修改xmlpath
* @param path
* @return
* @throws DocumentException
*/
public static ConfigModel build(String xmlpath) throws Exception{
ConfigModel configModel = new ConfigModel();
ActionModel actionModel = null;
ForwardModel forwardModel = null;
InputStream in = ConfigModelFactory.class.getResourceAsStream(xmlpath);
SAXReader reader = new SAXReader();
Document doc = reader.read(in);
List<Element> actionEles = doc.selectNodes("config/action");//查询action节点
for (Element actionEle : actionEles) {
actionModel = new ActionModel();
// 填充actionModel
actionModel.setPath(actionEle.attributeValue("path"));
actionModel.setType(actionEle.attributeValue("type"));
List<Element> forwardEles = actionEle.selectNodes("forward");//查询forward节点
for (Element forwardEle : forwardEles) {
forwardModel = new ForwardModel();
// 填充forwardModel
forwardModel.setName(forwardEle.attributeValue("name"));
forwardModel.setPath(forwardEle.attributeValue("path"));
forwardModel.setRedirect(!"false".equals(forwardEle.attributeValue("redirect")));
actionModel.push(forwardModel);
}
configModel.push(actionModel);
}
return configModel;
}
public static void main(String[] args) throws Exception {
ConfigModel configModel = ConfigModelFactory.build();
ActionModel actionModel = configModel.pop("/loginAction");
System.out.println(actionModel.getType());
System.out.println(actionModel.pop("success").getPath());
}
}
运行结果:
另外一个案例:
整体:
写一个web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>jrebelServlet</servlet-name>
<servlet-class>com.songwanxi.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.songwanxi.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>
开始建模:
1、servlet-name:
package com.songwanxi.Model;
public class ServletNameModel {
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
2、url-pattern
package com.songwanxi.Model;
public class UrlPatternModel {
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
3、servlet-class
package com.songwanxi.Model;
public class ServletClassModel {
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
4、Servlet
package com.songwanxi.Model;
public class ServletModel {
private ServletNameModel servletNameModel;
private ServletClassModel classModel;
public ServletNameModel getServletNameModel() {
return servletNameModel;
}
public void setServletNameModel(ServletNameModel servletNameModel) {
this.servletNameModel = servletNameModel;
}
public ServletClassModel getClassModel() {
return classModel;
}
public void setClassModel(ServletClassModel classModel) {
this.classModel = classModel;
}
}
5、servlet-mapping
package com.songwanxi.Model;
import java.util.ArrayList;
import java.util.List;
public class ServletMappingModel {
private ServletNameModel servletNameModel;
private List<UrlPatternModel> listurl = new ArrayList<>();
public ServletNameModel getServletNameModel() {
return servletNameModel;
}
public void setServletNameModel(ServletNameModel servletNameModel) {
this.servletNameModel = servletNameModel;
}
public List<UrlPatternModel> getListurl() {
return listurl;
}
public void setListurl(UrlPatternModel urlPatternModel) {
listurl.add(urlPatternModel);
}
}
6、web-app
package com.songwanxi.Model;
import java.util.ArrayList;
import java.util.List;
public class WebAppModel {
private List<ServletModel> ListServletModels = new ArrayList<>();
private List<ServletMappingModel> ListServletMappingModels = new ArrayList<>();
public List<ServletModel> getListServletModels() {
return ListServletModels;
}
public void pushListServletModels(ServletModel servletModels) {
ListServletModels.add(servletModels);
}
public List<ServletMappingModel> getListServletMappingModels() {
return ListServletMappingModels;
}
public void pushListServletMappingModels(ServletMappingModel servletMappingModels) {
ListServletMappingModels.add(servletMappingModels);
}
}
建立工厂:
package com.songwanxi.Model;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;import javax.servlet.Servlet;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ServletFactory {
public static WebAppModel build() throws Exception {
return build("web.xml");
}
public static WebAppModel build(String xmlpath) throws Exception {
WebAppModel webAppModel = new WebAppModel();
ServletModel servletModel = null;
ServletMappingModel servletMappingModel = null;
InputStream in = ServletFactory.class.getResourceAsStream(xmlpath);
SAXReader reader = new SAXReader();
Document doc = reader.read(in);
List<Element> servletEles = doc.selectNodes("/web-app/servlet");
//查询servlet节点
for (Element servletEle : servletEles) {
//填充servlet
servletModel = new ServletModel();
//找到servlet节点下的servletname节点
Element servletNameEle = (Element) servletEle.selectSingleNode("servlet-name");
//找到servlet节点下的servlet-class节点
Element servletClassEle = (Element) servletEle.selectSingleNode("servlet-class");
//填充到对应的对象里
ServletNameModel servletNameModel = new ServletNameModel();
servletNameModel.setText(servletNameEle.getText());
ServletClassModel servletClassModel = new ServletClassModel();
servletClassModel.setText(servletClassEle.getText());
servletModel.setServletNameModel(servletNameModel);
servletModel.setClassModel(servletClassModel);
//填充进webapp
webAppModel.pushListServletModels(servletModel);
}
//查询servlet-mapping节点
List<Element> ServletMappingEles = doc.selectNodes("/web-app/servlet-mapping");
for (Element servletMappingEle : ServletMappingEles) {
//填充servlet-mapping
servletMappingModel = new ServletMappingModel();
//找到servlet-mapping节点下的servletname
Element servletNameEle = (Element) servletMappingEle.selectSingleNode("servlet-name");
//填充到对应的对象里
ServletNameModel servletNameModel = new ServletNameModel();
servletNameModel.setText(servletNameEle.getText());
servletMappingModel.setServletNameModel(servletNameModel);
List<Element> urlPatternEles = servletMappingEle.selectNodes("url-pattern");
for (Element urlPatternEle : urlPatternEles) {
UrlPatternModel urlPatternModel = new UrlPatternModel();
urlPatternModel.setText(urlPatternEle.getText());
servletMappingModel.setListurl(urlPatternModel);
}
webAppModel.pushListServletMappingModels(servletMappingModel);
}
return webAppModel;
}
/**
* 通过浏览器输入的网址自动找到对应的后台处理器
* @param webAppModel 建模后的实体类
* @throws url 浏览器访问的网址
*/
public static String getServletClassByUrl(WebAppModel webAppModel,String url) {
String servletClass= "";
/**
* 找到浏览器网址对应的servlet-name
*
*/
String servletName= "";
List<ServletMappingModel> servletMappingModels = webAppModel.getListServletMappingModels();
//System.out.println(servletMappingModels.size());
for (ServletMappingModel servletMappingModel : servletMappingModels) {
List<UrlPatternModel> urlPatternModels = servletMappingModel.getListurl();
for (UrlPatternModel urlPatternModel : urlPatternModels) {
if(url.equals(urlPatternModel.getText())) {
ServletNameModel servletNameModel = servletMappingModel.getServletNameModel();
servletName = servletNameModel.getText();
}
}
}
/**
* 找到servlet-name对应的后台处理类
*/
List<ServletModel> servletModels = webAppModel.getListServletModels();
for (ServletModel servletModel : servletModels) {
ServletNameModel servletNameModel = servletModel.getServletNameModel();
if(servletName.equals(servletNameModel.getText())) {//通过上面找到的来找对应的
ServletClassModel servletClassModel = servletModel.getClassModel();
servletClass = servletClassModel.getText();
}
}
return servletClass;
}
public static void main(String[] args) throws Exception {
WebAppModel webAppModel = ServletFactory.build();
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);
}
}
运行结果: