目录
1.什么叫XML建模
2、xml建模
3.对config.xml进行建模
4.对web.xml进行建模
??
1.什么叫XML建模
以面向对象的思想 ,XML配置文件中的元素、属性、文本信息转换成对象的过程叫做XML建模? ?
2、xml建模
1)根据XML配置文件元素节点创建元素节点实体类 ?2)利用dom4j+xpath技术实现XML建模(工厂模式)
3.对config.xml进行建模
??建模的思路:?
?1)、将config.xml进行解析 ??2)、对应标签的内容将其封装赋值给相应的对象 ?? ? ? ?forward标签值 赋值给forwardmodel对象 ?? ? ?? action标签值 赋值给actionmodel对象 ?? ? ? config标签值 赋值给configmodel对象
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>

ActionModel?类:
package com.ysq.actionModel;
import java.util.HashMap;
import java.util.Map;
public class ActionModel {
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 Map<String, ForwardModel> getfMap() {
return fMap;
}
public void setfMap(Map<String, ForwardModel> fMap) {
this.fMap = fMap;
}
public void push(ForwardModel forwardModel) {
fMap.put(forwardModel.getName(), forwardModel);
}
public ForwardModel pop(String name) {
return fMap.get(name);
}
}
ConfigModel 类:
package com.ysq.actionModel;
import java.util.HashMap;
import java.util.Map;
public class ConfigModel {
private Map<String, ActionModel> acMap=new HashMap<>();
public void push(ActionModel actionModel) {
acMap.put(actionModel.getPath(), actionModel);
}
public ActionModel pop(String path) {
return acMap.get(path);
}
}
ForwardModel 类:
package com.ysq.actionModel;
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;
}
}
ConfigModelFactory类:(工厂类)
package com.ysq.actionModel;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/*
* 建模的思路
* 1、将config.xml进行解析
* 2、对应标签的内容将其封装赋值给相应的对象
* forward标签值 赋值给forwardmodel对象
* action标签值 赋值给actionmodel对象
* config标签值 赋值给configmodel对象
*/
public class ConfigModelFactory {
public static ConfigModel build() throws Exception {
return build("config.xml");
}
public static ConfigModel build(String resourcepath) throws Exception {
InputStream in = ConfigModelFactory.class.getResourceAsStream(resourcepath);
SAXReader sr=new SAXReader();
Document doc = sr.read(in);
ConfigModel configModel=new ConfigModel();
List<Element> actionEles = doc.selectNodes("/config/action");
for (Element actionEle : actionEles) {
ActionModel actionModel=new ActionModel();
// 将xml解析出来的path值赋值给actionModel对象中的path属性
actionModel.setPath(actionEle.attributeValue("path"));
actionModel.setType(actionEle.attributeValue("type"));
List<Element> forwarEles = actionEle.selectNodes("forward");
for (Element forwarEle : forwarEles) {
ForwardModel forwardModel=new ForwardModel();
forwardModel.setName(forwarEle.attributeValue("name"));
forwardModel.setPath(forwarEle.attributeValue("path"));
// Redirect只有在配置文件中赋值false的时候,代表转发,其代表重定向
forwardModel.setRedirect(!"false".equals(forwarEle.attributeValue("redirect")));
actionModel.push(forwardModel);
}
configModel.push(actionModel);
}
return configModel;
}
public static void main(String[] args) throws Exception {
ConfigModel configModel=ConfigModelFactory.build();
ActionModel actionModel = configModel.pop("/loginAction");
System.out.println(actionModel.getType());
}
}
4.对web.xml进行建模
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-mapping> ? ?? ?<servlet-name>jrebelServlet</servlet-name> ? ?? ?<url-pattern>/jrebelServlet</url-pattern> ? </servlet-mapping> ?? ? <servlet> ? ?? ?<servlet-name>jrebelServlet2</servlet-name> ? ?? ?<servlet-class>com.zking.xml.JrebelServlet2</servlet-class> ? </servlet> ?? ? <servlet-mapping> ? ?? ?<servlet-name>jrebelServlet2</servlet-name> ? ?? ?<url-pattern>/jrebelServlet2</url-pattern> ? ?? ?<url-pattern>/jrebelServlet3</url-pattern> ? </servlet-mapping> </web-app>
?
ServeletClassModel 类:
package com.ysq.xmlModel;
public class ServletClassModel {
private String content;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
ServeletMappingModel 类:?
package com.ysq.xmlModel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 List<UrlPatternModel> getUrlPatternModels() {
return urlPatternModels;
}
public void setUrlPatternModels(List<UrlPatternModel> urlPatternModels) {
this.urlPatternModels = urlPatternModels;
}
public void push(UrlPatternModel urlPatternModel) {
urlPatternModels.add(urlPatternModel);
}
public List<UrlPatternModel> popMapping(){
return urlPatternModels;
}
}
ServeletNameModel 类:
package com.ysq.xmlModel;
public class ServletNameModel {
private String content;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
ServeletModel 类:
package com.ysq.xmlModel;
import java.util.List;
public class ServletModel {
private ServletNameModel NameModel;
private ServletClassModel ClassModel;
public ServletNameModel getServletNameModel() {
return NameModel;
}
public void setServletNameModel(ServletNameModel NameModel) {
this.NameModel = NameModel;
}
public ServletClassModel getServletClassModel() {
return ClassModel;
}
public void setServletClassModel(ServletClassModel ClassModel) {
this.ClassModel = ClassModel;
}
public void pushName(String nameModel) {
NameModel.setContent(nameModel);
}
public ServletNameModel popNameModel(){
return NameModel;
}
}
UrlPatternModel 类:
package com.ysq.xmlModel;
public class UrlPatternModel {
private String content;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
?WebAppModel?类:?
package com.ysq.xmlModel;
import java.util.ArrayList;
import java.util.List;
public class WebAppModel {
private List<ServletModel> ServletModel = new ArrayList<>();
private List<ServletMappingModel> MappingModel = new ArrayList<>();
public List<ServletModel> getServletModel() {
return ServletModel;
}
public void setServletModel(List<ServletModel> ServletModel) {
this.ServletModel = ServletModel;
}
public List<ServletMappingModel> getServletMappingModel() {
return MappingModel;
}
public void setServletMappingModel(List<ServletMappingModel> MappingModel) {
this.MappingModel = MappingModel;
}
public void pushServlet(ServletModel servletModel) {
ServletModel.add(servletModel);
}
public List<ServletModel> popServlet(){
return ServletModel;
}
public void pushMappingModel(ServletMappingModel mappingModel) {
MappingModel.add(mappingModel);
}
public List<ServletMappingModel> popMapping(){
return MappingModel;
}
}
?WebAppModelFactory 类:(工厂类)
package com.ysq.xmlModel;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.ysq.actionModel.ConfigModel;
import com.ysq.actionModel.ConfigModelFactory;
public class WebAppModelFactory {
public static WebAppModel build() throws Exception {
return build("web.xml");
}
public static WebAppModel build(String resourcepath) throws Exception {
InputStream in = WebAppModelFactory.class.getResourceAsStream(resourcepath);
SAXReader saxReader = new SAXReader();
WebAppModel webAppModel = new WebAppModel();
Document doc = saxReader.read(in);
// 将servlet的标签内容填充进WebApp
List<Element> servletEles = doc.selectNodes("/web-app/servlet");
for (Element servletEle : servletEles) {
ServletModel servletModel = new ServletModel();
//找到servlet节点下的servlet-name节点
Element servletNameEle = (Element)servletEle.selectSingleNode("servlet-name");
//找到servlet节点下的servlet-class节点
Element servletClassEle = (Element)servletEle.selectSingleNode("servlet-class");
ServletNameModel servletNameModel=new ServletNameModel();
ServletClassModel servletClassModel =new ServletClassModel();
servletNameModel.setContent(servletNameEle.getText());
servletClassModel.setContent(servletClassEle.getText());
servletModel.setServletNameModel(servletNameModel);
servletModel.setServletClassModel(servletClassModel);
webAppModel.pushServlet(servletModel);
}
// 将servlet-mapping的标签内容填充进WebApp
List<Element> servletMappingEles = doc.selectNodes("/web-app/servlet-mapping");
for (Element servletMappingEle : servletMappingEles) {
ServletMappingModel servletMappingModel = new ServletMappingModel();
//找到servlet-mapping节点下的servlet-name节点
Element servletNameEle = (Element) servletMappingEle.selectSingleNode("servlet-name");
ServletNameModel servletNameModel = new ServletNameModel();
servletNameModel.setContent(servletNameEle.getText());
servletMappingModel.setServletNameModel(servletNameModel);
//找到servlet-mapping节点下的url-pattern节点
List<Element> urlPatternEles = servletMappingEle.selectNodes("url-pattern");
for (Element urlPatternEle : urlPatternEles) {
UrlPatternModel urlPatternModel = new UrlPatternModel();
urlPatternModel.setContent(urlPatternEle.getText());
servletMappingModel.push(urlPatternModel);
}
webAppModel.pushMappingModel(servletMappingModel);
}
return webAppModel;
}
}
|