目录
一,xml建模:
1,ConfigModel:
package zhoujaun.com;
import java.util.HashMap;
import java.util.Map;
/**
* 根标签对应对象
* @author zjjt
*
*/
public class ConfigModel {
private Map<String, ActionModel> aMap=new HashMap<>();
public void push(ActionModel actionModel) {
aMap.put(actionModel.getPath(), actionModel);
}
public ActionModel pop(String path) {
return aMap.get(path);
}
}
2,ActionModel:
package zhoujaun.com;
import java.util.HashMap;
import java.util.Map;
public class ActionModel {
// <action path="/loginAction" type="test.LoginAction">
private String path;
private String type;
private Map<String, ForwardModel> fMap=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;
}
//两个行为,增加forwardModel对象,查找forwardModel对象
// 将一个新的forward标签对象加入容器
public void push(ForwardModel forwardModel) {
fMap.put(forwardModel.getName(), forwardModel);
}
public ForwardModel pop(String name) {
return fMap.get(name);
}
}
3,ForwardModel:
package zhoujaun.com;
/**
* 对应forward标签
* @author zjjt
*
*/
public class ForwardModel {
// <forward name="failed" path="/login.jsp" redirect="false" />
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;
}
}
4,ConfigModelFactory:
package zhoujaun.com;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* 23种设计模式之工厂模式
* ConfigModelFactory就是用来生产configmodel对象的
* 生产出来的configmodel对象就包含了config.xml的配置内容
*
* 此地生产configmodel有配置信息?
* 1,解析config.xml中的配置信息
* 2.将对应的配置信息分别加载进行不同的配置
* @author zjjt
*
*/
public class ConfigModelFactory {
public static ConfigModel bulid(String path) throws Exception {
String defaultPath="/config.xml";
InputStream in = ConfigModelFactory.class.getResourceAsStream(path);
SAXReader sr=new SAXReader();
Document doc=sr.read(in);
ConfigModel configModel=new ConfigModel();
List<Element> actionEls = doc.selectNodes("/config/action");
for (Element actionEle : actionEls) {
ActionModel actionModel = new ActionModel();
actionModel.setPath(actionEle.attributeValue("path"));
actionModel.setType(actionEle.attributeValue("type"));
// 将forwardModel赋值并且添加到Actionmodel中
List<Element> frowardEles = actionEle.selectNodes("forward");
for (Element element : frowardEles) {
ForwardModel forwardModel = new ForwardModel();
forwardModel.setName(element.attributeValue("name"));
forwardModel.setPath(element.attributeValue("path"));
// Redirect:只能是false|true,允许空,默认为true
forwardModel.setRedirect("true".equals(element.attributeValue("redirect")));
actionModel.push(forwardModel);
}
configModel.push(actionModel);
}
return configModel;
}
public static ConfigModel bulid() throws Exception{
String defaultPath="/config.xml";
return bulid(defaultPath);
}
}
最后一不步进行查找,前期建模完成后,只需要在括号内输入你要查找的内容即可。
package zhoujaun.com;
public class Demo1 {
public static void main(String[] args) throws Exception {
// ConfigModel configModel =new ConfigModel();
ConfigModel configModel =ConfigModelFactory.bulid();
ActionModel actionModel = configModel.pop("/loginAction");
System.out.println(actionModel.getType());
ForwardModel forwardModel = actionModel.pop("success");
System.out.println(forwardModel.getPath());
}
}
注意:在写的过程中一定要注意建模和根目录的位置是否正确,否则很容易报空指针异常
三,案例。
1,
package zhoujuan.zy;
public class ServletClassModel {
private String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
2,
package zhoujuan.zy;
public class ServletNameModel {
private String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context=context;
}
}
3,
package zhoujuan.zy;
public class UrlPatternModel {
private String context;
public String getContext() {
return context;
}
public void getContext(String context) {
this.context=context;
}
}
4,
package zhoujuan.zy;
import java.util.ArrayList;
import java.util.List;
public class ServletMappingModel {
private ServletNameModel servletNameModel;
private List<UrlPatternModel> urlPatternModels=new ArrayList<>();
public ServletNameModel getServletNameModel() {
return servletNameModel;
}
public void setServletNameModel(ServletNameModel servletNameModel) {
this.servletNameModel=servletNameModel;
}
public void pushUrlPatternModel(UrlPatternModel urlPatternModel) {
urlPatternModels.add(urlPatternModel);
}
public List<UrlPatternModel> getUrlPatternModel(){
return urlPatternModels;
}
}
5,
package zhoujuan.zy;
public class ServletModel {
private ServletNameModel servletNameModel;
private ServletClassModel servletClassModel;
public ServletNameModel getServletNameModel() {
return servletNameModel;
}
public void setServletNameModel(ServletNameModel servletNameModel) {
this.servletNameModel=servletNameModel;
}
public ServletClassModel getServletClassModel() {
return servletClassModel;
}
public void setSerlectClassModel(ServletClassModel servletClassModel) {
this.servletClassModel=servletClassModel;
}
}
6,
package zhoujuan.zy;
import java.util.ArrayList;
import java.util.List;
public class WebAppModel {
private List<ServletModel> servletModels=new ArrayList<>();
private List<ServletMappingModel> servletMappingModels=new ArrayList<>();
public void pushServletModel(ServletModel servletModel) {
servletModels.add(servletModel);
}
public List<ServletModel>getServletModels(){
return servletModels;
}
public void pushServletMappingModel(ServletMappingModel servletMappingModel) {
servletMappingModels.add(servletMappingModel);
}
public List<ServletMappingModel> getServletMappingModels(){
return servletMappingModels;
}
}
package zhoujuan.zy;
import java.io.InputStream;
import java.sql.SQLXML;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class WebAppModelFactory {
public static WebAppModel buildWebModel() {
String xmlPath ="/web.xml";
return buildWebAppModel(xmlPath);
}
/**
* 建模
*
*/
public static WebAppModel buildWebAppModel(String xmlPath) {
InputStream in = WebAppModelFactory.class.getResourceAsStream(xmlPath);
SAXReader sr=new SAXReader();
WebAppModel webAppModel=new WebAppModel();
try {
Document doc= sr.read(in);
List<Element> servletEles=doc.selectNodes("/web-app/servlet");
for (Element servletEle : servletEles) {
ServletModel servletModel=new ServletModel();
Element servletNameEle = (Element)servletEle.selectSingleNode("servlet-name");
Element servletClassEle= (Element) servletEle.selectSingleNode("servlet-class");
ServletNameModel servletNameModel=new ServletNameModel();
ServletClassModel servletClassModel=new ServletClassModel();
servletNameModel.setContext(servletNameEle.getText());
servletClassModel.setContext(servletClassEle.getText());
servletModel.setServletNameModel(servletNameModel);
servletModel.setSerlectClassModel(servletClassModel);
webAppModel.pushServletModel(servletModel);
}
List<Element> servletMappingEles=doc.selectNodes("/web-app/servlet-mapping");
for (Element servletMappingEle : servletMappingEles) {
ServletMappingModel servletMappingModel= new ServletMappingModel();
Element ServletNamEle=(Element) servletMappingEle.selectSingleNode("servlet-name");
ServletNameModel servletNameModel=new ServletNameModel();
servletNameModel.setContext(ServletNamEle.getText());
servletMappingModel.setServletNameModel(servletNameModel);
List<Element>urlPatternEles=servletMappingEle.selectNodes("url-pattern");
for (Element urlPatternEle : servletMappingEles) {
UrlPatternModel urlPatternModel = new UrlPatternModel();
urlPatternModel.setContext(urlPatternEle.getText());
servletMappingModel.pushUrlPatternModel(urlPatternModel);
}
webAppModel.pushServletMappingModel(servletMappingModel);
}
} catch (Exception e) {
// TODO: handle exception
}
return webAppModel;
}
public static String getServletClassByUrl(WebAppModel webAppModel,String url) {
String servletClass="";
String servletName="";
List<ServletMappingModel> servletMappingModels=webAppModel.getServletMappingModels();
for (ServletMappingModel servletMappingModel : servletMappingModels) {
List<UrlPatternModel>urlPatternModels=servletMappingModel.getUrlPatternModels(){
for (UrlPatternModel urlPatternModel : UrlPatternModels) {
if(url.equals(urlPatternModel.getContext())) {
ServletNameModel servletNameModel = servletMappingModel.getServletNameModel();
servletName = servletNameModel.getContext();
}
}
}
List<ServletModel> servletModels = webAppModel.getServletModels();
for (ServletModel servletModel : servletModels) {
ServletNameModel servletNameModel = servletModel.getServletNameModel();
if(servletName.equals(servletNameModel.getContext())) {
ServletClassModel servletClassModel = servletModel.getServletClassModel();
servletClass = servletClassModel.getContext();
}
}
return servletClass;
}
public static void main(String[] args) {
WebAppModel webAppModel = WebAppModelFactory.buildWebAppModel(null);
String res = getServletClassByUrl(webAppModel, "/jrebelServlet");
String res2 = getServletClassByUrl(webAppModel, "/jrebelServlet2");
String res3 = getServletClassByUrl(webAppModel, "/jrebelServlet3");
System.out.println(res);
System.out.println(res2);
System.out.println(res3);
}
}