建模就是实体类 model
第一:建模的思路
1、分析需要被建模的文件中有哪几个对象
2、建模中的xml有哪几种属性
第二:分析建模代码
第三:将xml放入建好的模型里面
package com.zking.jee07.mymvc.framework;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.management.RuntimeErrorException;
/**
* config.xml中的config节点模型
* @author Administrator
*
*/
public class ConfigModel {
//config节点下会有多个action
Map<String,ActionModel> actions =new HashMap<>();
//讲解析到的action数据,放入action集合中去
public void addAction(ActionModel action) {
if(actions.containsKey(action.getPath())) {
throw new RuntimeException("ActionModel["+action.getPath()+"]已存在");
}
actions.put(action.getPath(),action);
}
//通过指定的path,从actions集合中获取action对象
public ActionModel findAction(String path) {
if(!actions.containsKey(path)) {
throw new RuntimeException("ActionModel["+path+"]不存在");
}
return actions.get(path);
}
}
package com.zking.jee07.mymvc.framework;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ActionModel {
private String path;
private String type;
//action中有forward子元素
private Map<String, ForwardModel> forwards =new HashMap<>();
private static Pattern pattern = Pattern.compile("^/.+$");
public String getPath() {
return path;
}
public void setPath(String path) {
Matcher matcher =pattern.matcher(path);
boolean b =matcher.matches();
if(!b) {
throw new RuntimeException("ActionModel.path["+path+"]必须以/开头");
}
this.path = path;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public void addForward(ForwardModel forward) {
if(forwards.containsKey(forward.getName())) {
throw new RuntimeException("ActionModel["+path+"]/forwardModel.name["+forward.getName()+"]已存在");
}
forwards.put(forward.getName(), forward);
}
public ForwardModel findForward(String name) {
if(!forwards.containsKey(name)) {
throw new RuntimeException("ActionModel["+path+"]/forwardModel.name["+name+"]不存在");
}
return forwards.get(name);
}
}
package com.zking.jee07.mymvc.framework;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ForwardModel {
private String name;
private String path;
private String redirect;
private static Pattern pattern =Pattern.compile("^/.+$");
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPath() {
return path;
}
public void setPath(String path) {
Matcher matcher =pattern.matcher(path);
boolean b =matcher.matches();
if(!b) {
throw new RuntimeException("ForwardModel.path["+path+"]必须以/开头");
}
this.path = path;
}
public String getRedirect() {
return redirect;
}
public void setRedirect(String redirect) {
this.redirect = redirect;
}
}
package com.zking.jee07.mymvc.framework;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.eclipse.jdt.internal.compiler.batch.Main;
/**
* 读取config.xml放入建好的模型中
* @author Administrator
*
*/
public class ConfigFactory {
private static String configName="/config.xml";
private static ConfigModel configModel =new ConfigModel();
//在static语句模块中获取config.xml,static块只会在类加载的过程中执行一次
static {
InputStream in =ConfigFactory.class.getResourceAsStream(configName);
SAXReader reader =new SAXReader();
try {
Document doc =reader.read(in);
Element rootElement = doc.getRootElement();
List<Element> actions =rootElement.selectNodes("action");
for(Element action:actions) {
String path =action.attributeValue("path");
String type =action.attributeValue("type");
ActionModel actionModel =new ActionModel();
actionModel.setPath(path);
actionModel.setType(type);
List<Element> forwards =action.selectNodes("forward");
for(Element forward:forwards) {
String name =forward.attributeValue("name");
String fpath =forward.attributeValue("path");
String redirect =forward.attributeValue("redirect");
ForwardModel forwardModel =new ForwardModel();
forwardModel.setName(name);
forwardModel.setPath(fpath);
forwardModel.setRedirect(redirect);
actionModel.addForward(forwardModel);
}
configModel.addAction(actionModel);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static ConfigModel getConfig() {
return configModel;
}
public static void main(String[] args) {
ConfigModel configModel =getConfig();
ActionModel ActionModel =configModel.findAction("/loginAction");
ForwardModel ForwardModel =ActionModel.findForward("success");
System.out.println(ForwardModel.getPath());
}
}