xml建模

1.建模的由來java

就是將指定的xml字符串看成對象來操做
若是說當對一個指定的xml格式字符串完成了建模操做,
好處在於,只須要調用指定的方法就能夠完成預約的字符串獲取web

 

2.建模的思路
一、分析須要被建模的文件中有那幾個對象
二、每一個對象擁有的行爲以及屬性
三、定義對象從小到大(從裏到外)
四、經過23種的設計模式中的工廠模式,解析xml生產出指定對象

好處:
提升代碼的複用性設計模式

 

分析XML文件(有幾個對象以及屬性,定義對象從小到大(從裏到外))jsp

<?xml version="1.0" encoding="UTF-8"?> <!-- 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>

 

定義XML模型對象ui

1.ForwardModelthis

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; } 

  

2.ActionModelspa

public class ActionModel { // <action path="/loginAction" type="test.LoginAction"> private String path; private String type; private Map<String, ForwardModel> fmap=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 Map<String, ForwardModel> getFmap() { return fmap; } public void setFmap(Map<String, ForwardModel> fmap) { this.fmap = fmap; } //添加 public void push(ForwardModel forwardModel) { fmap.put(forwardModel.getName(), forwardModel); } //經過name獲取對象 public ForwardModel pop(String name) { return fmap.get(name); } } 

  

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); } 

 

ConfigModelFactory(工廠模式用來將資源文件生產指定的實體類)
code

 

public class ConfigModelFactory { public static ConfigModel build() throws Exception { return configModel("config.xml"); } /** * 生產出有內容的實體類configModel * @param xmlPath * @return * @throws Exception */ public static ConfigModel configModel(String xmlPath) throws Exception { 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 Exception { ConfigModel configModel=ConfigModelFactory.build(); ActionModel actionModel=configModel.pop("/loginAction"); ForwardModel forwardModel=actionModel.pop("success"); System.out.println( actionModel.getType()); System.out.println(forwardModel.getPath()); } } 

 

  對web.xml進行建模,連接 https://i.cnblogs.com/Files.aspxxml

相關文章
相關標籤/搜索