目录
1.XML建模
2.工厂模式
课堂解析图
XML建模
1.将原有的config.xml进行解析

2.对应标签的内容,将其封装赋值给相应的对象(注意事项,从内向外建模)
forward标签赋值给ForwardModel对象
利用对象进行标签赋值,根据标签所需的属性不同而给予不同的值
package com.xly.a;
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;
}
}
action标签赋值给ActionModel对象
对其内部对象进行压栈和弹栈
代码块
package com.xly.a;
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<>();
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 void push(ForwardModel ForwardModel) {
fmap.put(ForwardModel.getName(), ForwardModel);
}
// 弹栈
public ForwardModel pop(String name) {
return fmap.get(name);
}
}
config标签赋值给ConfigModel对象
最大的对象压栈弹栈完之后可进行查询其内部数据
代码块
package com.xly.a;
import java.util.HashMap;
import java.util.Map;
/**
* ConfigModel
* @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);
}
public static void main(String[] args) throws Exception {
ConfigModel cm=ConfigModelFactory.build();
ActionModel am = cm.pop("/loginAction");
System.out.println(am.getType());
}
}
其输出的结果
工厂模式
1.新建一个对象方法找到相关的xml文档类
2.找到工厂类自定义数据进行输入
3.找到相对应元素进行遍历
4.将xml文件解析得来的path值赋值给actionmodel对象中的path属性
5.Redirect只有在配置文件中赋值false的时候代表转发,其他代表重定向
代码显示
public class ConfigModelFactory {
// 新建一个类找到相关的xml文档类
public static ConfigModel build() throws Exception {
return build("config.xml");
}
public static ConfigModel build(String resourcepath) throws Exception {
InputStream is = ConfigModelFactory.class.getResourceAsStream(resourcepath);
SAXReader saxreader=new SAXReader();
Document doc = saxreader.read(is);
ConfigModel con=new ConfigModel();
// 找到相对应元素进行遍历
List<Element> alist=doc.selectNodes("/config/action");
for (Element i : alist) {
ActionModel am=new ActionModel();
// 将xml文件解析得来的path值赋值给actionmodel对象中的path属性
am.setPath(i.attributeValue("path"));
am.setType(i.attributeValue("type"));
List<Element> flist=i.selectNodes("forword");
for (Element fi : flist) {
ForwardModel fm=new ForwardModel();
fm.setName(fi.attributeValue("name"));
fm.setPath(fi.attributeValue("path"));
// Redirect只有在配置文件中赋值false的时候代表转发,其他代表重定向
fm.setRedirect(!"false".equals(fi.attributeValue("redirect")));
am.push(fm);
}
con.push(am);
}
return con;
}
}
课后练习
题目web.xml进行建模
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>jrebelServlet</servlet-name>
<servlet-class>com.zking.xml.JrebelServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>jrebelServlet2</servlet-name>
<servlet-class>com.zking.xml.JrebelServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jrebelServlet</servlet-name>
<url-pattern>/jrebelServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>jrebelServlet2</servlet-name>
<url-pattern>/jrebelServlet2</url-pattern>
<url-pattern>/jrebelServlet3</url-pattern>
</servlet-mapping>
</web-app>
对web.xml标签进行建模(从内向外一次进行建模)
servlet-name
package com.xly.b;
public class ServletNameModel {
private String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
servlet-class
package com.xly.b;
public class ServletClassModel {
private String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
url-pattern
package com.xly.b;
public class UrlPatternModel {
private String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
servlet
package com.xly.b;
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 setServletClassModel(ServletClassModel servletClassModel) {
this.servletClassModel = servletClassModel;
}
}
servlet-mapping
package com.xly.b;
import java.util.ArrayList;
import java.util.List;
public class ServletMappingModel {
private ServletNameModel servletNameModel;
private List<UrlPatternModel> ulist = new ArrayList<>();
public ServletNameModel getServletNameModel() {
return servletNameModel;
}
public void setServletNameModel(ServletNameModel servletNameModel) {
this.servletNameModel = servletNameModel;
}
public void pushUrlPatternModel(UrlPatternModel urlPatternModel) {
ulist.add(urlPatternModel);
}
public List<UrlPatternModel> getUrlPatternModels() {
return ulist;
}
}
web-app
package com.xly.b;
import java.util.ArrayList;
import java.util.List;
public class WebappModel {
private List<ServletModel> slist = new ArrayList<>();
private List<ServletMappingModel> smlist = new ArrayList<>();
public void pushServletModel(ServletModel servletModel) {
slist.add(servletModel);
}
public List<ServletModel> getServletModels() {
return slist;
}
public void pushServletMappingModel(ServletMappingModel servletMappingModel) {
smlist.add(servletMappingModel);
}
public List<ServletMappingModel> getServletMappingModels() {
return smlist;
}
}
进行工厂模式
package com.xly.b;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class WebappModelFactory {
public static WebappModel buildWebAppModel() {
String xmlPath = "/web.xml";
return buildWebAppModel(xmlPath);
}
/**
* 建模
*
* @param xmlPath
* @return
*/
public static WebappModel buildWebAppModel(String xmlPath) {
InputStream is = WebappModelFactory.class.getResourceAsStream(xmlPath);
SAXReader saxred = new SAXReader();
WebappModel webAppModel = new WebappModel();
try {
Document doc = saxred.read(is);
/*
* 将servlet的标签内容填充进WebApp
*/
List<Element> list = doc.selectNodes("/web-app/servlet");
for (Element i : list) {
ServletModel sm = new ServletModel();
/*
* 给ServletModel填充xml的内容
*/
Element sn = (Element) i.selectSingleNode("servlet-name");
Element sc = (Element) i.selectSingleNode("servlet-class");
ServletNameModel snm = new ServletNameModel();
ServletClassModel scm = new ServletClassModel();
snm.setContext(snm.getContext());
scm.setContext(scm.getContext());
sm.setServletNameModel(snm);
sm.setServletClassModel(scm);
webAppModel.pushServletModel(sm);
}
/*
* 将servlet-mapping的标签内容填充进WebApp
*/
List<Element> smg = doc.selectNodes("/web-app/servlet-mapping");
for (Element ism : smg) {
ServletMappingModel smm = new ServletMappingModel();
/*
* 给ServletMappingModel填充xml的内容
*/
Element sn = (Element) ism.selectSingleNode("servlet-name");
ServletNameModel snm = new ServletNameModel();
snm.setContext(sn.getText());
smm.setServletNameModel(snm);
List<Element> up = ism.selectNodes("url-pattern");
for (Element ui : up) {
UrlPatternModel urlPatternModel = new UrlPatternModel();
urlPatternModel.setContext(ui.getText());
smm.pushUrlPatternModel(urlPatternModel);
}
webAppModel.pushServletMappingModel(smm);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return webAppModel;
}
/**
* 通过浏览器输入的网址自动找到对应的后台处理类
* @param webAppModel 建模后的实体类
* @param url 浏览器访问的网址
* @return
*/
public static String getServletClassByUrl(WebappModel webAppModel, String url) {
String servletClass = "";
/*
* 找到浏览器网址对应的servlet-name
*/
String servletName = "";
List<ServletMappingModel> smm = webAppModel.getServletMappingModels();
for (ServletMappingModel smi : smm) {
List<UrlPatternModel> upm = smi.getUrlPatternModels();
for (UrlPatternModel ui : upm) {
if(url.equals(ui.getContext())) {
ServletNameModel snm = smi.getServletNameModel();
servletName = snm.getContext();
}
}
}
/*
* 找到servlet-name对应的后台处理类
*/
List<ServletModel> list1 = webAppModel.getServletModels();
for (ServletModel sm : list1) {
ServletNameModel snm = sm.getServletNameModel();
if(servletName.equals(snm.getContext())) {
ServletClassModel scm = sm.getServletClassModel();
servletClass = scm.getContext();
}
}
return servletClass;
}
public static void main(String[] args) {
WebappModel wam = WebappModelFactory.buildWebAppModel();
String req = getServletClassByUrl(wam, "/jrebelServlet");
String req1 = getServletClassByUrl(wam, "/jrebelServlet2");
String req2 = getServletClassByUrl(wam, "/jrebelServlet3");
System.out.println(req);
System.out.println(req1);
System.out.println(req2);
}
}