Table of contents
Label element naming syntax rules:
1. Three configuration locations and reading methods of configuration files in java
Create models: ConfigModel, ActionModel, ForwardModel
1. dtd
1. Understand xml
Question: what is xml? Why study xml? The role of xml?
DTD stands for Document Type Definition in English and means "document class definition" in Chinese.
A DTD is a set of grammar rules for tags, which specifies which tags can be used in XML, which tags have attributes, and what order the tags appear in.
DTD has two tasks:
1. Help to write legal code;
2. Let the browser display the code correctly.
- XML stands for Extensible Markup Language ( EX tensible Markup Language )
- XML is a markup language , much like HTML
- XML is designed to transmit data , not display it
- XML tags are not predefined. You need to define your own labels .
- XML is designed to be self-describing .
- XML is a W3C Recommendation
Later learning mvc configuration file
In order to facilitate data sharing and communication between different applications and different platforms.
Function: network data transmission.
data storage
configuration file
2, xml syntax format
- has exactly one root element
- xml tags are case sensitive
- Correct use of closing tags, nested tags
- Use valid tag names
- define valid properties
- Start tag (open tag): <tag name>
- Closing tag (closing tag): </tagname>
For example: <name>Zhangsan</name>
3. Element Definition
Label element naming syntax rules:
1. The name can contain letters, numbers and other characters
2. The name cannot start with numbers or punctuation marks
3. The name cannot start with the characters "xml" (or XML, Xml)
4. The name cannot contain spaces and cannot contain colons (: )
5. Names are case sensitive
Add DTD declaration in XML <!DOCTYPE root[]>
Classification of elements:
<!ELEMENT element-name (#PCDATA)> ------------Text element
<!ELEMENT element-name EMPTY> ----------------empty Element
<!ELEMENT element-name (e1,e2)> ---------------- Mixed elements
Element Limits:
with ( , ) not ( | )
Times: 0 or 1------?
0~N-------*
1~N-------+
//案例
<!DOCTYPE persons[
<!ELEMENT persons (person+)>
<!ELEMENT person (name,age,contact,br*)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT contact (phone|email)>
<!ELEMENT br EMPTY>
]>
3. Attribute Definition
grammar:
<!ATTLIST element-name
att_name type desc
>
Property type :
ID (male|female) CDATA IDREF reference
Property description:
#REQUIRED Required
#IMPLED not required
'Default value' (only when type is (male|female), desc can use the default value method)
//案例
<!ATTLIST person
pid ID #REQUIRED
sex (男|女) '男'
qq CDATA #IMPLIED
parent IDREF #IMPLIED
>
Second, the parsing of XML
1. Three configuration locations and reading methods of configuration files in java
同包:Demo1.class.getResourceAsStream("db.properties");
Root path : Demo1.class.getResourceAsStream(" /db.properties ");
WIN-INF safe path : context.getResourceAsStream(" /WEB-INF/db.properties ");
2, the use of dom4j
Common methods of dom4j: selectNodes selectSingleNode attributeValue getText
config xml file
<?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>
<action path="/studentAction" type="org.lisen.mvc.action.StudentAction">
<forward name="students" path="/students/studentList.jsp" redirect="false"/>
</action>
<action path="/studentAction1" type="org.lisen.mvc.action.StudentAction">
<forward name="students1" path="/students/studentList.jsp" redirect="false"/>
</action>
<action path="/studentAction2" type="org.lisen.mvc.action.StudentAction">
<forward name="students2" path="/students/studentList.jsp" redirect="false"/>
</action>
</config>
write get method
public class Demo {
public static void main(String[] args) throws Exception {
InputStream stream = Demo.class.getResourceAsStream("/config.xml");
SAXReader reader = new SAXReader();// SAXReader 读取xml文件解析
Document document = reader.read(stream);// 文档
Element element = document.getRootElement();// 获取根元素
List<Element> actions = element.selectNodes("/config/action");
for (Element action : actions) {
String path = action.attributeValue("path");
String type = action.attributeValue("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");
System.out.println("name=" + name);
System.out.println("fpath=" + fpath);
System.out.println("redirect=" + redirect);
}
System.out.println("path=" + path);
System.out.println("type=" + type);
System.out.println("---------------");
}
}
}
The results are as follows:
3. Modeling
1. XML modeling
Create models: ConfigModel, ActionModel, ForwardModel
ConfigModel
public class ConfigModel {
private Map<String, ActionModel> actionMap = new HashMap<>();
public void put(ActionModel action) {
if (actionMap.containsKey(action.getPath())) {
throw new ActionDuplicateDefinitionException("action path:" + action.getPath() + " 不能重复");
}
actionMap.put(action.getPath(), action);
}
public ActionModel find(String path) {
if (!actionMap.containsKey(path)) {
throw new ActionNotFoundException("action path:" + path + "没有找到");
}
return actionMap.get(path);
}
}
ActionModel
public class ActionModel {
private String path;
private String type;
private Map<String, ForwardModel> forwardMap = 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 put(ForwardModel forward) {
if (forwardMap.containsKey(forward.getName())) {
throw new ForwardDuplicateDefinitionException("forward name:" + forward.getName() + "不能重复");
}
forwardMap.put(forward.getName(), forward);
}
public ForwardModel find(String name) {
if (!forwardMap.containsKey(name)) {
throw new ForwardNotFoundException("forward name:" + name + "不存在");
}
return forwardMap.get(name);
}
@Override
public String toString() {
return "ActionModel [path=" + path + ", type=" + type + "]";
}
}
ForwardModel
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;
}
public void setRedirect(String redirect) {
if ("true".equals(redirect) || "false".equals(redirect)) {
this.redirect = Boolean.valueOf(redirect);
} else {
throw new RuntimeException("属性redirect的值必须位ture或者false");
}
}
@Override
public String toString() {
return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
}
}
2. Factory mode
Parse and generate models
ConfigModelFactory
public final class ConfigModelFactory {
private ConfigModelFactory() {
}
private static ConfigModel config = new ConfigModel();
// 静态代码块,在类加载的时候只执行一次,在这里完成config.xml文件的解析和Config相关的
// 模型的建立,这样在整个系统运行阶段只会解析一次
static {
try {
InputStream in = ConfigModelFactory.class.getResourceAsStream("/config.xml");
SAXReader reader = new SAXReader();
Document doc = reader.read(in);
Element ce = doc.getRootElement();
List<Element> actions = ce.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 f : forwards) {
String name = f.attributeValue("name");
String fpath = f.attributeValue("path");
String redirect = f.attributeValue("redirect");
ForwardModel forwardModel = new ForwardModel();
forwardModel.setName(name);
forwardModel.setPath(fpath);
forwardModel.setRedirect(redirect);
actionModel.put(forwardModel);
}
config.put(actionModel);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// 获取系统的Config配置对象
public static ConfigModel getConfig() {
return config;
}
public static void main(String[] args) {
ConfigModel config = ConfigModelFactory.getConfig();
ActionModel action = config.find("/studentAction");
System.out.println(action);
ForwardModel forward = action.find("students");
System.out.println(forward);
}
}
The results are as follows: