建模的由来
就是将指定的xml字符串当作对象来操作 如果说当对一个指定的xml格式字符串完成了建模操作,好处在于,只需要调用指定的方法就可以完成预定的字符串获取;
什么是建模
简单来说创建实体类的过程就叫做建模。
思维导图
1.建模的思路
1.1分析需要被建模的文件中有几个对象(就是xml文件中的节点)
1.2每个对象里的属性
1.3然后设计工厂模式,解析xml产生出指定的对象
2.建模的好处
将代码封装,提高代码的复用性,有利于项目后期的维护。
3.视例代码
首先要知道需求:
获取<config> 下<action> 属性path="/loginAction" 下的<forward>属性为path="/main.jsp"
然后对xml文件进行分析。(代码如下)
<!-- 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>
分析如下:
config-->ConfigMOdel类
action-->ActionModel类
forward-->ForwardModel类
分析好然后我们就要来写代码了。
3.2类中的属性和方法
将xml文件下的每个节点中的属性当做属性封装起来。
3.2.1ForwardModel类
package com.zking.entity;
import java.io.Serializable;
public class ForwardModel implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
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 getRedirect() {
return redirect;
}
public void setRedirect(Boolean redirect) {
this.redirect = redirect;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public ForwardModel() {
// TODO Auto-generated constructor stub
}
public ForwardModel(String name, String path, Boolean redirect) {
super();
this.name = name;
this.path = path;
this.redirect = redirect;
}
@Override
public String toString() {
return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
}
}
3.2.2ActionModel类
package com.zking.entity;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
public class ActionModel implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2830026581435558732L;
private String path;
private String type;
private Map<String, ForwardModel> forwards =
new HashMap<String, ForwardModel>();
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 static long getSerialversionuid() {
return serialVersionUID;
}
public Map<String, ForwardModel> getForwards() {
return forwards;
}
public void setForwards(Map<String, ForwardModel> forwards) {
this.forwards = forwards;
}
public ActionModel() {
// TODO Auto-generated constructor stub
}
public ActionModel(String path, String type) {
this.path = path;
this.type = type;
}
/**
* 将forwardModel存入map集合中的方法
* @param forwardModel ForwardModel对象
*/
public void push(ForwardModel forwardModel) {
this.forwards.put(forwardModel.getName(), forwardModel);
}
/**
* 通过forward中的name从map集合中取出对应的forwardModel;
* @param name
* @return
*/
public ForwardModel getForwardModel(String name) {
return this.forwards.get(name);
}
}
3.2.3ConfigModel类
package com.zking.entity;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
public class ConfigModel implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2741198186875117829L;
private Map<String, ActionModel> actions =
new HashMap<String, ActionModel>();
public Map<String, ActionModel> getActions() {
return actions;
}
public void setActions(Map<String, ActionModel> actions) {
this.actions = actions;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
@Override
public String toString() {
return "ConfigModel [actions=" + actions + ", getActions()=" + getActions() + ", getClass()=" + getClass()
+ ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]";
}
/**
* 将actionModel存入map集合中的方法
* @param actionModel ActionModel对象
*/
public void push(ActionModel actionModel) {
this.actions.put(actionModel.getPath(), actionModel);
}
/**
* 通过actions中的path从map集合中取出对应的actionModel;
* @param path
* @return
*/
public ActionModel getActionModel(String path) {
return this.actions.get(path);
}
}
3.3 ConfigModelFactiory类
package com.zking.utils;
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;
import com.zking.entity.ActionModel;
import com.zking.entity.ConfigModel;
import com.zking.entity.ForwardModel;
public class ConfigModelFactiory {
public static final String DEFAULT_PATH="/config.xml";
private ConfigModelFactiory(){
}
/**
* 读取文件配置信息,将信息存储到ConfigModel中
* @return
*/
public static ConfigModel createConfigModel(String path) {
InputStream is = ConfigModelFactiory.class.getResourceAsStream(path);
//定义须要用的变量
String actionPath=null;
String actionType=null;
ActionModel actionModel =null;
String forwardName=null;
String forwardPath=null;
String forwardRedirect=null;
ForwardModel forwardModel =null;
ConfigModel configModel =new ConfigModel();
SAXReader sr =new SAXReader();
try {
Document doc = sr.read(is);
List<Node> actionNodes = doc.selectNodes("/config/action");
for (Node actionNode : actionNodes) {
Element nodeEl =(Element)actionNode;
//获取属性值
actionPath=nodeEl.attributeValue("path");
actionType=nodeEl.attributeValue("type");
// System.out.println(actionPath);
//实例化ActionModel对象
actionModel =new ActionModel();
//将属性值存入 ActionModel中
actionModel.setPath(actionPath);
actionModel.setType(actionType);
//找到action节点下的forword节点
List<Node> forwordNodes = actionNode.selectNodes("/config/action/forward");
//遍历
for (Node forwordNode : forwordNodes) {
//将节点对象转换成元素对象
Element forwardE1 =(Element)forwordNode;
//获取forward的属性值
forwardName=forwardE1.attributeValue("name");
forwardPath=forwardE1.attributeValue("path");
// System.out.println(forwardPath);
forwardRedirect=forwardE1.attributeValue("redirect");
//实例化 ForwardModel对象
forwardModel =new ForwardModel();
//将xml解析出来的值放入对象属性中
forwardModel.setName(forwardName);
forwardModel.setPath(forwardPath);
forwardModel.setRedirect(Boolean.parseBoolean(forwardRedirect));
//将forwardModeld对象存入ActionMode的集合中
actionModel.push(forwardModel);
}
//将actionModel对象放入ComfigModel对象的集合中
configModel.push(actionModel);
}
return configModel;
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
public static ConfigModel createConfigModel( ) {
return createConfigModel(DEFAULT_PATH);
}
public static void main(String[] args) {
ConfigModel configModel = ConfigModelFactiory.createConfigModel();
ActionModel actionModel = configModel.getActionModel("/regAction");
System.out.println(actionModel.getType());
ForwardModel forwardModel = actionModel.getForwardModel("failed");
System.out.println(forwardModel.getPath());
}
}