1.XML的由来
1.就是将指定的xml字符串当作对象来操作.
2.如果说当对一个指定的xml格式字符串完成了建模操作.
3.好处:只需要调用指定的方法就可以完成预定的字符串获取.
2.建模的思路
1、分析需要被建模的文件中有那几个对象
2、每个对象拥有的行为以及属性
3、定义对象从小到大(从里到外)
4、通过23种的设计模式中的工厂模式,解析xml生产出指定对象
5.好处:提高代码的复用性
3.应用
在XML建模中用几个类来获取哦 config.xml中的属性
属性为String类型,子元素标签则是map的值,子元素标签的唯一标识则为map的值
先定义xml模型对象(ForwardModel,ActionModel,ConfigModel )
public class ForwardModel { // <forward name="failed" path="/reg.jsp" redirect="false" /> private String name; private String path; private boolean redirect; 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; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
import java.util.HashMap; import java.util.Map; public class ActionModel { // <action path="/regAction" type="test.RegAction"> private String path; private String type; private Map<String, ForwardModel> forMap = new HashMap<>(); 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) { forMap.put(forwardModel.getName(), forwardModel); } public ForwardModel pop(String name) { return forMap.get(name); }
}
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生产的指定对象
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; /** * 23种设计模式 * 设计模式是一种解决方案,就是为了处理java中遇到的特定的一些问题 * 工厂模式 * 解决什么问题呢? * 它是用来将资源文件生产指定的实体类 好处: * 提高了代码的复用性 */ public class ConfigModelFactory { public static ConfigModel build() throws DocumentException { return configModel("config.xml"); } /** * 生产出有内容的实体类ConfigModel * * @param string * @return * @throws DocumentException */ public static ConfigModel configModel(String xmlPath) throws DocumentException { ConfigModel configModel=new ConfigModel(); ActionModel actionModel=null; ForwardModel forwardModel=null; InputStream in=ConfigModelFactory.class.getResourceAsStream(xmlPath); SAXReader sax=new SAXReader(); Document doc=sax.read(in); List<Element> actionEles= doc.selectNodes("/config/action"); for (Element actionEle : actionEles) { actionModel=new ActionModel(); //给actionModel对象填充xml中的action标签的内容 actionModel.setPath(actionEle.attributeValue("path")); actionModel.setType(actionEle.attributeValue("type")); List<Element> forwardEles=actionEle.selectNodes("forward"); for (Element forwardEle : forwardEles) { forwardModel=new ForwardModel(); //给forwardModel对象填充xml中的forward标签的内容 forwardModel.setName(forwardEle.attributeValue("name")); forwardModel.setPath(forwardEle.attributeValue("path")); forwardModel.setRedirect(!"false".equals(forwardEle.attributeValue(("redirect")))); // <forward name="failed" path="/reg.jsp" redirect="false" /> // redirect默认是true 重定向 只有填了false才是转发 //forwardEle.attributeValue(("redirect"))拿到的是xml中你所填的值 // 不填 重定向 "false".equals(forwardEle.attributeValue(("redirect")))是false // 填 true 重定向 "false".equals(forwardEle.attributeValue(("redirect")))是false // 填flase 转发 "false".equals(forwardEle.attributeValue(("redirect")))是true; actionModel.push(forwardModel); } configModel.push(actionModel); } return configModel; } public static void main(String[] args) throws DocumentException { ConfigModel configModel = ConfigModelFactory.build(); ActionModel actionModel = configModel.pop("/loginAction"); // System.out.println(actionModel.getType()); ForwardModel forwardModel=actionModel.pop("success"); System.out.println(actionModel.getType()); System.out.println(forwardModel.getPath()); } }
作业:
public class ServletClassModel { private String context; public String getContext() { return context; } public void setContext(String context) { this.context = context; } }
import java.util.ArrayList; import java.util.List; public class ServletMappingModel { private ServletNameModel servletNameModel; private List<UrlPatternModel> urlPatternModels = new ArrayList<>(); 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; } }
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; } }
public class ServletNameModel { private String context; public String getContext() { return context; } public void setContext(String context) { this.context = context; } }
public class UrlPatternModel { private String context; public String getContext() { return context; } public void setContext(String context) { this.context = context; } }
import java.util.ArrayList; import java.util.List; public class WebAppModel { private List<ServletModel> servletModels = new ArrayList<>(); 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; } }
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() { //xml根目录下 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) { 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.setServletClassModel(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; } /** * 通过浏览器输入的网址自动找到对应的后台处理类 * * @param webAppModel * 建模后的实体类 * @param url * 浏览器访问的网址 * @return */ 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文件放置在根目录下(src文件)
<?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>