目录
一,什么是XML的建模
将XML配置文件中的元素,属性,文本信息转换成对象的过程叫做XML建模
二,举例说明
1.假设我们要拿到配置文件config.xml中第二个<action>中的type属性的值“test.LoginAction”
2.再在此基础上拿到第二个<forward>标签中path属性的值“main.jsp”
分析:
可看见<forward>标签有name,path,redirect三个属性可已用作封装
完整代码:
package com.ruojuan.model;
/**
* Forward标签
* @author liuruojuan
*
* 时间:2022年6月14日下午5:36:46
*/
public class ForwardModel {
// <forward name="failed" path="/login.jsp" redirect="false" />
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;
}
}
<action>标签也有属性,内部也包含<forward>标签也可经行封装,将<forward>标签进行封装可用map集合
完整代码:
package com.ruojuan.model;
import java.util.HashMap;
import java.util.Map;
/**
* ActionModel标签
* @author liuruojuan
*
* 时间:2022年6月14日下午5:36:05
*/
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;
}
//两个行为,增加forwardModel对象,查找forwardModel对象
//将一个新的forward标签对象加入容器
public void push(ForwardModel forwardModel) {
fMap.put(forwardModel.getName(), forwardModel);
}
public ForwardModel pop(String name) {
return fMap.get(name);
}
}
再将action根标签放进map集合中,然后可获取path属性
完整代码:
package com.ruojuan.model;
import java.util.HashMap;
import java.util.Map;
/**
* 跟标签对应的对象
* @author liuruojuan
*
* 时间:2022年6月14日下午5:36:25
*/
public class ConfigModel {
private Map<String, ActionModel> aMap = new HashMap<String, ActionModel>();
public void push(ActionModel actionMpdel) {
aMap.put(actionMpdel.getPath(), actionMpdel);
}
public ActionModel pop(String path) {
return aMap.get(path);
}
}
生产configmodel对象
完整代码:
package com.ruojuan.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 liuruojuan
*
* 时间:2022年6月14日下午5:54:09
*/
public class ConfigModelFactory {
public static ConfigModel bulid(String path) throws Exception {
InputStream in = ConfigModelFactory.class.getResourceAsStream(path);
SAXReader sr = new SAXReader();
Document read = sr.read(in);
List<Element> actionEles = read.selectNodes("/config/action");
ConfigModel configModel = new ConfigModel();
for (Element actionEle : actionEles) {
ActionModel actionModel = new ActionModel();
actionModel.setPath(actionEle.attributeValue("path"));
actionModel.setType(actionEle.attributeValue("type"));
//将forwardmodel赋值并且添加actionmodel中
List<Element> forwardEles = actionEle.selectNodes("forward");
for (Element element : forwardEles) {
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")));
actionModel.push(forwardModel);
}
configModel.push(actionModel);
}
return configModel;
}
public static ConfigModel bulid() throws Exception {
String defaultPath = "/config.xml";
return bulid(defaultPath);
}
}
最后可以看结果了:
完整代码:
package com.ruojuan.model;
public class Dome1 {
public static void main(String[] args) throws Exception {
ConfigModel configModel = ConfigModelFactory.bulid();
ActionModel actionModel = configModel.pop("/loginAction");
System.out.println(actionModel.getType());
ForwardModel forwardModel = actionModel.pop("success");
System.out.println(forwardModel.getPath());
}
}
三,案例
拿到servlet-class的值
servlet-name封装类:
package com.ruojuan.servlet;
/**
* servlet-name
* @author liuruojuan
*
* 时间:2022年6月14日下午11:39:37
*/
public class SerNamModel {
private String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
servlet-class的封装类
package com.ruojuan.servlet;
/**
* servlet-class
* @author liuruojuan
*
* 时间:2022年6月14日下午11:40:32
*/
public class SerClModel {
private String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
url-pattern封装类:
package com.ruojuan.servlet;
/**
* url-pattern
* @author liuruojuan
*
* 时间:2022年6月14日下午11:41:31
*/
public class UrlPatModel {
private String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
servlet标签:
package com.ruojuan.servlet;
/**
* servlet标签
* @author liuruojuan
*
* 时间:2022年6月14日下午11:42:28
*/
public class SerModel {
private SerNamModel serNamModel;
private SerClModel serClModel;
public SerNamModel getSerNamModel() {
return serNamModel;
}
public void setSerNamModel(SerNamModel serNamModel) {
this.serNamModel = serNamModel;
}
public SerClModel getSerClModel() {
return serClModel;
}
public void setSerClModel(SerClModel serClModel) {
this.serClModel = serClModel;
}
}
servlet-mapping标签:
package com.ruojuan.servlet;
import java.util.ArrayList;
import java.util.List;
/**
* web-app根标签
* @author liuruojuan
*
* 时间:2022年6月14日下午11:45:09
*/
public class WebAppModel {
private List<SerModel> serModels = new ArrayList<SerModel>();
private List<SerMapModel> serMapModels = new ArrayList<SerMapModel>();
public void pushSerModel(SerModel serModel) {
serModels.add(serModel);
}
public List<SerModel> getSerModels() {
return serModels;
}
public void pushSerMapModel(SerMapModel serMapModel) {
serMapModels.add(serMapModel);
}
public List<SerMapModel> getSerMapModels() {
return serMapModels;
}
}
web-app根标签:
package com.ruojuan.servlet;
import java.util.ArrayList;
import java.util.List;
/**
* servlet-mapping标签
* @author liuruojuan
*
* 时间:2022年6月14日下午11:43:33
*/
public class SerMapModel {
private SerNamModel serNamModel;
private List<UrlPatModel> urlPatModels = new ArrayList<UrlPatModel>();
public SerNamModel getSerNamModel() {
return serNamModel;
}
public void setSerNamModel(SerNamModel serNamModel) {
this.serNamModel = serNamModel;
}
public void pushUrlPatModel(UrlPatModel urlPatModel) {
urlPatModels.add(urlPatModel);
}
public List<UrlPatModel> getUrlPatModel() {
return urlPatModels;
}
}
WebAppModelFactory生产webAppModel对象:
package com.ruojuan.servlet;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* WebAppModelFactory生产webAppModel对象
* @author liuruojuan
*
* 时间:2022年6月14日下午11:46:00
*/
public class WebAppModelFactory {
public static WebAppModel buildWebAppModel() {
String xmlPath = "/web.xml";
return buildWebAppModel(xmlPath);
}
/**
* 建模
*
* @param xmlPath
* @return
*/
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) {
SerModel servletModel = new SerModel();
/*
* 给ServletModel填充xml的内容
*/
Element servletNameEle = (Element) servletEle.selectSingleNode("servlet-name");
Element servletClassEle = (Element) servletEle.selectSingleNode("servlet-class");
SerNamModel servletNameModel = new SerNamModel();
SerClModel servletClassModel = new SerClModel();
servletNameModel.setContext(servletNameEle.getText());
servletClassModel.setContext(servletClassEle.getText());
servletModel.setSerNamModel(servletNameModel);
servletModel.setSerClModel(servletClassModel);
webAppModel.pushSerModel(servletModel);
}
/*
* 将servlet-mapping的标签内容填充进WebApp
*/
List<Element> servletMappingEles = doc.selectNodes("/web-app/servlet-mapping");
for (Element servletMappingEle : servletMappingEles) {
SerMapModel servletMappingModel = new SerMapModel();
/*
* 给ServletMappingModel填充xml的内容
*/
Element servletNameEle = (Element) servletMappingEle.selectSingleNode("servlet-name");
SerNamModel servletNameModel = new SerNamModel();
servletNameModel.setContext(servletNameEle.getText());
servletMappingModel.setSerNamModel(servletNameModel);
List<Element> urlPatternEles = servletMappingEle.selectNodes("url-pattern");
for (Element urlPatternEle : urlPatternEles) {
UrlPatModel urlPatternModel = new UrlPatModel();
urlPatternModel.setContext(urlPatternEle.getText());
servletMappingModel.pushUrlPatModel(urlPatternModel);
}
webAppModel.pushSerMapModel(servletMappingModel);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return webAppModel;
}
/**
* 通过浏览器输入的网址自动找到对应的后台处理类
* @param webAppModel 建模后的实体类
* @param url 浏览器访问的网址
* @return
*/
public static String getServletClassByUrl(WebAppModel webAppModel, String url) {
String servletClass = "";
/*
* 找到浏览器网址对应的servlet-name
*/
String servletName = "";
List<SerMapModel> servletMappingModels = webAppModel.getSerMapModels();
for (SerMapModel servletMappingModel : servletMappingModels) {
List<UrlPatModel> urlPatternModels = servletMappingModel.getUrlPatModel();
for (UrlPatModel urlPatternModel : urlPatternModels) {
if(url.equals(urlPatternModel.getContext())) {
SerNamModel servletNameModel = servletMappingModel.getSerNamModel();
servletName = servletNameModel.getContext();
}
}
}
/*
* 找到servlet-name对应的后台处理类
*/
List<SerModel> servletModels = webAppModel.getSerModels();
for (SerModel servletModel : servletModels) {
SerNamModel servletNameModel = servletModel.getSerNamModel();
if(servletName.equals(servletNameModel.getContext())) {
SerClModel servletClassModel = servletModel.getSerClModel();
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);
}
}
运行结果: