xml建模注意事项
- xml建模先要从没有子类的标签开始实例化
- xml模型要实现serializable接口
xml建模步骤
config.xml的源文件
<?xml version="1.0" encoding="UTF-8"?>
<!--
config标签:可以包含0~N个action标签
-->
<config>
<!--
action标签:可以饱含0~N个forward标签
path:以/开头的字符串,并且值必须唯一 非空 ,子控制器对应的路径
type:字符串,非空,子控制器的完整类名
-->
<action path="/registerAction" type="test.action.RegisterAction">
<forward name="success" path="/index.jsp" redirect="true" />
<forward name="failed" path="/register.jsp" redirect="false" />
</action>
<action path="/loginAction" type="test.action.LoginAction">
<forward name="a" path="/index.jsp" redirect="false" />
<forward name="b" path="/welcome.jsp" redirect="true" />
</action>
</config>
第一步:按照先后顺序来实例化对象
第二步:按照需要的方法,在实例化对象中写方法
实例化ForwardModel
public class ForwardModel implements Serializable{
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;
}
public void setRedirect(String redirect) {
this.redirect =Boolean.parseBoolean(redirect);
}
@Override
public String toString() {
return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
}
实例化ActionModel
public class ActionModel implements Serializable{
private String path;
private String type;
private Map<String, ForwardModel> forwardmodels=new HashMap<String, ForwardModel>();
public ActionModel() {
// TODO Auto-generated constructor stub
}
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> getMa() {
return forwardmodels;
}
public void setMa(Map<String, ForwardModel> ma) {
this.forwardmodels = ma;
}
public ActionModel(String path, String type, Map<String, ForwardModel> ma) {
super();
this.path = path;
this.type = type;
this.forwardmodels = ma;
}
/**
*判断传入的键位是否已经重复
*/
public void put(ForwardModel f) {
if(this.forwardmodels.containsKey(f.getName())) {
throw new RuntimeException("该"+f.getName()+"键位已经存在");//抛出中断代码的运行异常
}
this.forwardmodels.put(f.getName(), f);
}
/**
*判断是否有这个对象
*/
public ForwardModel get(String name) {
if(null==name) {
throw new RuntimeException("该"+name+"键位不存在");
}
return this.forwardmodels.get(name);
}
@Override
public String toString() {
return "ActionModel [path=" + path + ", type=" + type + ", forwardmodels=" + forwardmodels + "]";
}
实例化ConfigModel
public class ConfigModel {
private Map<String, ActionModel> actionModels=new HashMap<String, ActionModel>();
private static String PATH="config.xml";
public Map<String, ActionModel> getActionModels() {
return actionModels;
}
public void setActionModels(Map<String, ActionModel> actionModels) {
this.actionModels = actionModels;
}
public ConfigModel(Map<String, ActionModel> actionModels) {
super();
this.actionModels = actionModels;
}
public void put(ActionModel a) {
if(this.actionModels.containsKey(a.getPath())) {
throw new RuntimeException("该"+a.getPath()+"键位已经存在");
}
this.actionModels.put(a.getPath(), a);
}
public ActionModel get(String path) {
if(null==path) {
throw new RuntimeException("该"+path+"键位不存在");
}
return this.actionModels.get(path);
}
/**
* 封装无参构造函数
*/
private ConfigModel(){
}
public static ConfigModel createConfigModel(String path) throws DocumentException {
ConfigModel configmodel=new ConfigModel();
InputStream is=null;
if(null==path) {
is=ConfigModel.class.getResourceAsStream(PATH);
}else {
is=ConfigModel.class.getResourceAsStream(path);
}
//解析
SAXReader sa=new SAXReader();
Document d = sa.read(is);
List<Element> la = d.selectNodes("/config/action");//得到action里面的所有内容
for (Element actions : la) {
ActionModel m=new ActionModel();//创建ActionModel
m.setType(actions.attributeValue("type"));//将属性值赋给new出来的对象
m.setPath(actions.attributeValue("path"));
List<Element> lb = d.selectNodes("/config/action/forward");//得到节点forward里面的所有内容
for (Element forwards : lb) {
ForwardModel f=new ForwardModel();
f.setName(forwards.attributeValue("name"));
f.setPath(forwards.attributeValue("path"));
f.setRedirect(forwards.attributeValue("path"));
m.put(f);
}
configmodel.put(m);
}
return configmodel;
}
@Override
public String toString() {
return "ConfigModel [actionModels=" + actionModels + "]";
}
最后每次调用createConfigModel的时候,都会直接将XMl解析完成