xml建模的核心思想就是利用java面向对象的特性,用操作对象的方法操作xml
需要解析建模的xml文件
<?xml version="1.0" encoding="UTF-8"?>
<config>
<action path="/regAction" type="test.RegAction">
<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>
写一个forward实体类
package com.liubiao.model;
/**
* forward标签实体类
* @author liubiao
*
* http://javalb.com
*/
public class ForwardModel {
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;
}
}
action实体类
package com.liubiao.model;
import java.util.HashMap;
import java.util.Map;
/**
* action标签实体类
* @author liubiao
*
* http://javalb.com
*/
public class ActionModel {
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;
}
//2个行为
/**
* 将指定的forwardModel对象压入当前actionModel对象中
* <forward />放入<action></action>
* @param forwardModel
*/
public void push(ForwardModel forwardModel) {
forMap.put(forwardModel.getName(), forwardModel);
}
public ForwardModel pop(String name) {
return forMap.get(name);
}
}
config实体类
package com.liubiao.model;
import java.util.HashMap;
import java.util.Map;
public class ConfigModel {
private Map<String, ActionModel> acMap=new HashMap<String, ActionModel>();
/**
* 将指定的actionModel对象压入当前configModel对象中
* <action></action>放入<config></config>
* @param actionModel
*/
public void push(ActionModel actionModel) {
acMap.put(actionModel.getPath(), actionModel);
}
public ActionModel pop(String path) {
return acMap.get(path);
}
}
工厂模型对象
package com.liubiao.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;
/**
* -工厂模式解决的问题:将代码封装,提高代码的复用性
* @author liubiao
*
* http://javalb.com
*/
public class ConfigModelFactory {
/**
* -通过资源文件构建对应的模型对象
* @param path 具体的资源文件路径
* @return
* @throws Exception
*/
public static ConfigModel build(String path) throws Exception {
//加载资源文件
//path=/config.xml
InputStream in = ConfigModelFactory.class.getResourceAsStream(path);
SAXReader reader=new SAXReader();
//doc=/config.xml里面的类容
Document doc = reader.read(in);
//接下来就做一件事情--》把内容填充到configModel对象中(doc.asXML--》configModel)
ConfigModel configModel=new ConfigModel();
ActionModel actionModel=null;
ForwardModel forwardModel=null;
List<Element> actionEles = doc.selectNodes("/config/action");
for(Element actionEle : actionEles) {
actionModel= new ActionModel();
actionModel.setPath(actionEle.attributeValue("path"));
actionModel.setType(actionEle.attributeValue("type"));
//给actionModel中放入ForwardModel对象
//拿到forward标签内容
List<Element> forwardEles = actionEle.selectNodes("forward");
for(Element forwardEle : forwardEles) {
forwardModel = new ForwardModel();
forwardModel.setName(forwardEle.attributeValue("name"));
forwardModel.setPath(forwardEle.attributeValue("path"));
//redirect属性是boolean类型的
//需求,只有config.xml中的redirect属性值写了false 他才代表转发
//forwardEle.attributeValue("redirect")获取到config中的redirect属性值,那个字符串
forwardModel.setRedirect(!"false".equals(forwardEle.attributeValue("redirect")));
actionModel.push(forwardModel);
}
configModel.push(actionModel);
}
return configModel;
}
/**
*- 默认框架的模型对象构造方法
* @return
* @throws Exception
*/
public static ConfigModel build() throws Exception {
return build("/config.xml");
}
public static void main(String[] args) throws Exception {
ConfigModel model=ConfigModelFactory.build();
ActionModel actionModel = model.pop("/loginAction");
System.out.println(actionModel.getType());
ForwardModel forwardModel=actionModel.pop("success");
System.out.println(forwardModel.getPath());
}
}