建模的核心就是利用java面向对象的特性,用操作对象的方式来操作XML
1、分析需要被建模的文件中有那几个对象
2、每个对象拥有的行为以及属性
3、定义对象从小到大(从里到外)
根据servlet-name查找的对应的标签值
代码如下:
<?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>
在敲代码之前,我们需要先理清一下思路,想清楚或者写出来,每个标签需要什么属性和行为,这里我画了一个图,可参考:
备注:我用的web.xml文件放在src根目录下
思路理清后,我们就可以开始建模型了
每个标签对应一个类
建模型的时候要记得是从小到大,从里到外的顺序
按照这个顺序我们从servlet-name,servlet-class,url-pattern开始建
servlet-name
value代表内容
package com.wangjuanxia;
public class ServletNameModel {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
servlet-class
package com.wangjuanxia;
public class ServletClassModel {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
url-pattern
package com.wangjuanxia;
public class UrlPatternModel {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
然后往外是第二梯队der
servlet,servlet-mapping
servlet
因为servlet包含servlet-name,servlet-class所以定义两个以模型对象为类型的变量并封装
package com.wangjuanxia;
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;
}
public void setServletClassModel(ServletClassModel servletClassModel) {
this.servletClassModel = servletClassModel;
}
}
servlet-mapping
由于servlet-mapping里的url-pattern标签不止一个,所以用一个map集合存储,再写两个行为——pop取,push存,存的键是servlet-name的值,同理取也应该是根据servlet-name取
package com.wangjuanxia;
public class ServletMappingModel {
private ServletNameModel servletNameModel;
private Map<String, UrlPatternModel> urlPatternModels=new HashMap<String, UrlPatternModel>();
public ServletNameModel getServletNameModel() {
return servletNameModel;
}
public void setServletNameModel(ServletNameModel servletNameModel) {
this.servletNameModel = servletNameModel;
}
public UrlPatternModel pop(String value) {
UrlPatternModel urlPatternModel = urlPatternModels.get(value);
return urlPatternModel;
}
public void push(UrlPatternModel urlPatternModel,ServletNameModel servletNameModel) {
urlPatternModels.put(servletNameModel.getValue(), urlPatternModel);
}
}
再往上走是web-app
web-app包含servlet,servlet-mapping标签,定义两个map集合分别存储,为了好查询,我这里键都是servlet-name,再写行为:每个标签分别写两个行为——pop,push;pops代表servlet标签的取,popm代表servlet-mapping的取,pushs代表servlet的存,pushm代表servlet-mapping的存。
package com.wangjuanxia;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class WebappModel {
private Map<String, ServletModel> servletM=new HashMap<String, ServletModel>();
private Map<String, ServletMappingModel> servletmappingM=new HashMap<String, ServletMappingModel>();
public ServletModel pops(String name) {
return servletM.get(name);
}
public ServletMappingModel popm(String name) {
return servletmappingM.get(name);
}
public void pushs(ServletModel s) {
servletM.put(s.getServletNameModel().getValue(), s);
}
public void pushm(ServletMappingModel m) {
servletmappingM.put(m.getServletNameModel().getValue(), m);
}
}
然后是重头戏,建立生产模型的方法(工厂)
注意:在工厂类中一般会有两个及以上的构建方法,1、默认框架路径的模型对象构建方法,2、动态读取任意位置下的框架配置文件
步骤
1、获取资源文件
2、得到所有的标签
3、用selectNodes/selectSingeNode筛选得到标签
4、遍历填进
package com.wangjuanxia;
public class WebAppFactory {
/**
* 生产webapp模型的方法
* @param path
* @return
* @throws Exception
*/
public static WebappModel build(String path) throws Exception {
InputStream in = WebAppFactory.class.getResourceAsStream(path);
SAXReader reader=new SAXReader();
Document document = reader.read(in);
WebappModel webappModel=new WebappModel();
ServletModel servletModel=null;
ServletMappingModel servletMappingModel=null;
List<Element> servleteles = document.selectNodes("/web-app/servlet");
for (Element servletele : servleteles) {
//实例化Servlet,servlet-name,servlet-class标签模型
servletModel=new ServletModel();
ServletNameModel servletNameModel=new ServletNameModel();
ServletClassModel servletClassModel = new ServletClassModel();
//获取到servlet-name,servlet-class模型
Element servletNameele = (Element) servletele.selectSingleNode("servlet-name");
Element servletClassele = (Element) servletele.selectSingleNode("servlet-class");
//添加进去
servletNameModel.setValue(servletNameele.getText());
servletClassModel.setValue(servletClassele.getText());
//将servlet-name,servlet-class填进servlet中
servletModel.setServletNameModel(servletNameModel);
servletModel.setServletClassModel(servletClassModel);
//将servlet填进web-app
webappModel.pushs(servletModel);
}
List<Element> mappingEles = document.selectNodes("/web-app/servlet-mapping");
for (Element mappingEle : mappingEles) {
//实例化servlet-mapping,servlet-name,urlPattern的模型
servletMappingModel=new ServletMappingModel();
ServletNameModel servletNameModel = new ServletNameModel();
//得到servlet-name,url-pattern标签
Element servletNameEle = (Element) mappingEle.selectSingleNode("servlet-name");
//添加
servletNameModel.setValue(servletNameEle.getText());
servletMappingModel.setServletNameModel(servletNameModel);
//得到url-pattern
List<Element> urlPatternEles=mappingEle.selectNodes("url-pattern");
//遍历填充
for (Element urlPatternEle : urlPatternEles) {
UrlPatternModel urlPatternModel = new UrlPatternModel();
urlPatternModel.setValue(urlPatternEle.getText());
servletMappingModel.push(urlPatternModel,servletNameModel);
}
//将servlet-mapping填进web-app中
webappModel.pushm(servletMappingModel);
}
return webappModel;
}
public static WebappModel build() throws Exception{
return build("/web.xml");
}
}
调用方法
根据jrebelServlet查
public static void main(String[] args) throws Exception {
//调用方法
WebappModel w= WebAppFactory.build();
//根据jrebelServlet查,得到servlet
ServletModel servletModel=w.pops("jrebelServlet");
//得到servletMaping
ServletMappingModel servletMappingModel = w.popm("jrebelServlet");
//得到url-pattern
UrlPatternModel urlPatternModel = servletMappingModel.pop(servletMappingModel.getServletNameModel().getValue());
//输出
System.out.println("servlet-name="+servletModel.getServletNameModel().getValue()+"\nclass="+servletModel.getServletClassModel().getValue());
System.out.println("-------------------------------------------------------------");
System.out.println("servlet-mapping");
System.out.println("servlet-name= "+servletMappingModel.getServletNameModel().getValue()+"\nurl-pattern="+urlPatternModel.getValue());
}