建模分兩步:
1、以面向對象的編程思想,描述xml資源文件。
2、將xml文件中內容封裝進model實體對象。
?
導入文件:config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE config[<!ELEMENT config (action*)><!ELEMENT action (forward*)><!ELEMENT forward EMPTY><!ATTLIST actionpath CDATA #REQUIREDtype CDATA #REQUIRED><!ATTLIST forwardname CDATA #REQUIREDpath CDATA #REQUIREDredirect (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>
?
新建類:ConfigModel.java
package com.zking.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);}}
新建類:Actionmodel.java
package com.zking.model;import java.util.HashMap;
import java.util.Map;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 void push(ForwardModel forwardModel) {fmap.put(forwardModel.getName(), forwardModel);}public ForwardModel pop(String name) {return fmap.get(name);}}
新建類:ForwardModel.java
package com.zking.model;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;}}
新建類:ConfigModelFactory.java
package com.zking.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;/** 23設計模式之一* 工廠模式* 設計模式是一種解決方案,就是為了處理java中所遇到的特定的一些問題* 解決什么問題呢?* 它是用來將資源文件生產指定的實體類* 好處:* 提高了代碼的復用性*/
public class ConfigModelFactory {public static ConfigModel build() throws DocumentException {return ConfigModel("/config.xml");}/** 生產出有類內容的實體類configmodel*/private static ConfigModel ConfigModel(String string) throws DocumentException {ConfigModel configModel=new ConfigModel();Actionmodel actionmodel=null;ForwardModel forwardModel=null; InputStream in= ConfigModelFactory.class.getResourceAsStream(string);SAXReader reader=new SAXReader();Document document=reader.read(in);List<Element> list= document.selectNodes("/config/action");for (Element element : list) {actionmodel=new Actionmodel();//給actionmodel對象填充xml中的action標簽的內容actionmodel.setPath(element.attributeValue("path"));actionmodel.setType(element.attributeValue("type"));List<Element> list2= element.selectNodes("forward");for (Element element2 : list2) {forwardModel =new ForwardModel(); //給forwardmodel對象填充xml中的action標簽的內容forwardModel.setName(element2.attributeValue("name"));forwardModel.setPath(element2.attributeValue("path"));forwardModel.setRedirect(!"false".equals(element2.attributeValue("redirect")));//<forward name="failed" path="/reg.jsp" redirect="false" />//resirect默認是true//只有填了false才是轉發//element2.attributeValue("redirect")拿到的是xml中你所填的值//不填 重定向 "false".equals(element2.attributeValue("redirect"))是false
// 填 true 定向 "false".equals(element2.attributeValue("redirect"))是false//填false 轉發 false".equals(element2.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());}}
注釋:
本項目一共導用了兩個jar包:dom4j-1.6.1.jar? ? ?jaxen-1.1-beta-6.jar ?
?