思维导图:
1.建模的由来
就是将指定的xml字符串当作对象来操作
如果说当对一个指定的xml格式字符串完成了建模操作,
好处在于,只需要调用指定的方法就可以完成预定的字符串获取;
2.建模的思路
1.将原有的config.xml进行解析
2.对应标签的内容,将其封装赋值给相对应的对象
* forward标签值 赋值给 ForwardModel对象
* Action标签值 赋值给 ActionModel对象
* config标签值 赋值给 configModel对象
ps:定义对象从小到大(从里到外)
好处:提高代码的复用性
3.建模分两步:
1、以面向对象的编程思想,描述xml资源文件
2、将xml文件中内容封装进model实体对象
举例:为config.xml建模
config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE config[
<!ELEMENT config (action*)>
<!ELEMENT action (forward*)>
<!ELEMENT forward EMPTY>
<!ATTLIST action
path CDATA #REQUIRED
type CDATA #REQUIRED
>
<!ATTLIST forward
name CDATA #REQUIRED
path CDATA #REQUIRED
redirect (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>
首先我们要对它的各个节点进行对象化(从里到外):
ForwardModel:
package com.pjl.model;
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;
}
}
ActionModel:
package com.pjl.model;
import java.util.HashMap;
import java.util.Map;
public class ActionModel {
// <action path="/regAction" type="test.RegAction">
private String path;
private String type;
private Map<String