xml Modeling

1. The origin of modeling

Xml string is specified as objects to manipulate
if to say when a specified xml format string to complete the modeling operations,
the benefits that only need to call the specified method can be completed to obtain a predetermined string ;

 

2. Modeling ideas
1, the analysis needs to be modeled in a file that has several objects
2, each object has behavior and attributes
3, the definition of small to large objects (inside and out)
4, by 23 kinds of design model of the factory model, parse xml produce the specified object

benefits:
improve code reusability

 

Parse the XML file (there are several objects and attributes defined objects from small to large (inside and out))

<? XML Version = "1.0" encoding = "UTF-. 8" ?> 
<-! Config tag: 0 ~ N may comprise two action tag -> 
< config > 
    ! <- action tag: 0 ~ N may be full a forward tag path: string / beginning, and only non-null value must type: string, a non-empty -> 
    < Action path = "/ regAction" type = "test.RegAction" > 
        <-! forward tag : no sub-labels; name: string, forward tag name value under the same action tag must be different; path: in / at the beginning of the string
            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>

 

Custom XML model objects

1.ForwardModel

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.ActionModel

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

	
	//Add to
    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(工厂模式用来将资源文件生产指定的实体类)

 

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.aspx

 

Guess you like

Origin www.cnblogs.com/xmf3628/p/11005861.html