一、Xpath(会查文档,简单有些概念和印象)
二、Dom4J综合案例(练习)
拿到一个案例,首先不是敲代码,首先应该是画图,分层进行处理

按图来做,我们应该做4个包,访问数据库(XML),然后应该做一个测试类进行测试.
1.先建立Student类,JavaBeam封装数据.
com.itheima.domain:Student
2.建立util类,方便后面开发.在这个例子中,建立读取Document和写入数据库的工具即可
com.itheima.util:工具类
package com.itheima.util;
//工具类可以抛,也可以处理.
public class Dom4jUtil {
//获取Document
public static Document getDocument() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read("src/exam.xml");
return document;
}
//把数据写入XML中去
public static void write2xml(Document document) throws Exception{
OutputFormat format = OutputFormat.createPrettyPrint();//漂亮的格式,默认
format.setEncoding("UTF-8");//设置编码,默认UTF-8
XMLWriter writer = new XMLWriter(new FileOutputStream("src/exam.xml"),format);
writer.write(document);
writer.close();
}
}
//工具类可以抛,也可以处理.
public class Dom4jUtil {
//获取Document
public static Document getDocument() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read("src/exam.xml");
return document;
}
//把数据写入XML中去
public static void write2xml(Document document) throws Exception{
OutputFormat format = OutputFormat.createPrettyPrint();//漂亮的格式,默认
format.setEncoding("UTF-8");//设置编码,默认UTF-8
XMLWriter writer = new XMLWriter(new FileOutputStream("src/exam.xml"),format);
writer.write(document);
writer.close();
}
}
3.建立数据访问层
com.itheima.dao:DAO(Data Access Object)
public
class StudentDao {
/**
* 添加学生信息到exam.xml中
* @param stu
* @return 成功返回true 不成功返回false
*/
/*
* 按照xml里面的格式,在根元素下面增加数据.
*/
public boolean addStudent(Student stu ){
boolean result = false;//标记结果
try {
Document doc = Dom4jUtil.getDocument();
//获得Document,再获得根元素
Element root = doc.getRootElement();
//获得根元素后,直接加元素即可
Element element = root.addElement("student")
.addAttribute("idcard",stu.getIdcard())
.addAttribute("examid", stu.getExamid());//增加了student标签
element.addElement("name").addText(stu.getName());//增加了name标签
element.addElement("location").addText(stu.getLocation());//增加了location标签
element.addElement("grade").addText(""+stu.getGrade());//增加了grade标签
//写入文件
Dom4jUtil.write2xml(doc);
result = true;
} catch (Exception e) {
throw new RuntimeException(e);//异常转义,异常转义链
}
return result;
}
/**
* 按照学生examId返回学生信息
* @param examId
* @return 返回学生对象Student,不成功返回null
*/
public Student findExamId(String examId){
Student s = null;
try {
Document doc = Dom4jUtil.getDocument();
//先找到节点,在用节点进行判断
Node node = doc.selectSingleNode("//student[@examid='"+examId+"']");
if(node!=null){
Element e = (Element)node;
s = new Student();
s.setIdcard(e.valueOf("@idcard"));
s.setExamid(e.valueOf("@examid"));
s.setName(e.elementText("name"));
//上面一句等价:s.setName(e.element("name").getText());
s.setLocation(e.elementText("location"));
//单精度有静态方法 parseFloat 把文本转为 float.
s.setGrade(Float.parseFloat(e.elementText("grade")));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return s;
}
/**
* 按照学生姓名删除学生
* @param name
* @return 如果学生不存在或者删除成功,都删除true;否则返回false
*/
public Boolean delStudent(String name){
Boolean result = false;
try {
Document doc = Dom4jUtil.getDocument();
//先找到节点,在用节点进行判断
List<Node> nameList = doc.selectNodes("//name");
for(Node node:nameList){
if(name.equals(node.getText())){
//需要删除student,需要得到其爸爸的爸爸
//因为只有爸爸才能干掉儿子
node.getParent().getParent().remove(node.getParent());
//写入文件
Dom4jUtil.write2xml(doc);
break;
}
}
result = true;
} catch (Exception e) {
throw new RuntimeException(e);
}
return result;
}
}
/**
* 添加学生信息到exam.xml中
* @param stu
* @return 成功返回true 不成功返回false
*/
/*
* 按照xml里面的格式,在根元素下面增加数据.
*/
public boolean addStudent(Student stu ){
boolean result = false;//标记结果
try {
Document doc = Dom4jUtil.getDocument();
//获得Document,再获得根元素
Element root = doc.getRootElement();
//获得根元素后,直接加元素即可
Element element = root.addElement("student")
.addAttribute("idcard",stu.getIdcard())
.addAttribute("examid", stu.getExamid());//增加了student标签
element.addElement("name").addText(stu.getName());//增加了name标签
element.addElement("location").addText(stu.getLocation());//增加了location标签
element.addElement("grade").addText(""+stu.getGrade());//增加了grade标签
//写入文件
Dom4jUtil.write2xml(doc);
result = true;
} catch (Exception e) {
throw new RuntimeException(e);//异常转义,异常转义链
}
return result;
}
/**
* 按照学生examId返回学生信息
* @param examId
* @return 返回学生对象Student,不成功返回null
*/
public Student findExamId(String examId){
Student s = null;
try {
Document doc = Dom4jUtil.getDocument();
//先找到节点,在用节点进行判断
Node node = doc.selectSingleNode("//student[@examid='"+examId+"']");
if(node!=null){
Element e = (Element)node;
s = new Student();
s.setIdcard(e.valueOf("@idcard"));
s.setExamid(e.valueOf("@examid"));
s.setName(e.elementText("name"));
//上面一句等价:s.setName(e.element("name").getText());
s.setLocation(e.elementText("location"));
//单精度有静态方法 parseFloat 把文本转为 float.
s.setGrade(Float.parseFloat(e.elementText("grade")));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return s;
}
/**
* 按照学生姓名删除学生
* @param name
* @return 如果学生不存在或者删除成功,都删除true;否则返回false
*/
public Boolean delStudent(String name){
Boolean result = false;
try {
Document doc = Dom4jUtil.getDocument();
//先找到节点,在用节点进行判断
List<Node> nameList = doc.selectNodes("//name");
for(Node node:nameList){
if(name.equals(node.getText())){
//需要删除student,需要得到其爸爸的爸爸
//因为只有爸爸才能干掉儿子
node.getParent().getParent().remove(node.getParent());
//写入文件
Dom4jUtil.write2xml(doc);
break;
}
}
result = true;
} catch (Exception e) {
throw new RuntimeException(e);
}
return result;
}
}
4.对数据访问层进行测试,因为这个是需要外部调用的.
com.itheima.test:测试类,在测试类中直接使用方法即可.
小技巧:在需要测试的java文件上点右键,New一个测试类,然后改包名,下一步选择需要测试的方法即可.
5.建立用户使用界面
com.itheima.view:界面
package com.itheima.view;
public class Main {
//创建主函数,创建用户界面
public static void main(String[] args) throws Exception {
//创建数据操作层 DAO
StudentDao dao = new StudentDao();
System.out.println("a,添加学生\tb,查询学生\tc,删除学生");
System.out.println("请输入操作类型:");
//接受用户输入.需要缓存,需要转换流
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
if("a".equals(str)){
System.out.println("请输入学生姓名:");
String name = br.readLine();
System.out.println("请输入学生准考证号:");
String examid = br.readLine();
System.out.println("请输入学生身份证号:");
String idcard = br.readLine();
System.out.println("请输入学生所在地:");
String location = br.readLine();
System.out.println("请输入学生成绩:");
String grade = br.readLine();
Student s = new Student();
s.setName(name);
s.setExamid(examid);
s.setIdcard(idcard);
s.setLocation(location);
s.setGrade(Float.parseFloat(grade));
boolean result = dao.addStudent(s);
if(result)
System.out.println("学生添加成功");
else
System.out.println("学生添加失败");
}
else if("b".equals(str)){//查询
System.out.println("请输入要查询的准考证号");
String examid = br.readLine();
Student s = dao.findExamId(examid);
if(s==null)
System.out.println("查无此人");
else
System.out.println(s);
}
else if("c".equals(str)){
System.out.println("请输入要查询的准考证号");
String name = br.readLine();
boolean result = dao.delStudent(name);
if(result)
System.out.println("学生添加成功");
else
System.out.println("学生添加失败");
}
else
System.out.println("操作类型不符,请重新输入");
}
}
三、Schema约束
目标,根据Schema写出xml文档来,难点在于xml文件如何引入schema约束
xml中引入schema约束的步骤:(王氏独家)
1、查看schema文档,找到根元素,在xml中写出来
<?xml version="1.0" encoding="UTF-8"?>
<书架>
</书架>
2、根元素来自哪个名称空间。使用xmlns指令来声明
名称空间是在schema中定义的,就是targetNamespace的值
<?xml version="1.0" encoding="UTF-8"?>
<itheima:书架 xmlns:itheima="http://www.itheima.com/book">
</itheima:书架>
3、引入的名称空间根哪个xsd文件对应?
使用schemaLocation来指定:两个取值:第一个为名称空间 第二个为xsd文件的路径
<?xml version="1.0" encoding="UTF-8"?>
<itheima:书架 xmlns:itheima="http://www.itheima.com/book"
schemaLocation="http://www.itheima.com/book book.xsd">
</itheima:书架>
4、schemaLocation哪里来的?它来自一个标准的名称空间,直接复制黏贴即可.
<?xml version="1.0" encoding="UTF-8"?>
<itheima:书架 xmlns:itheima="http://www.itheima.com/book"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.itheima.com/book book.xsd">
</itheima:书架>
5、只要以上4部搞好了,对于子标签eclipse就有提示了
<? xml version
= "1.0" encoding
= "ISO-8859-1" ?>
< xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
targetNamespace = "http://www.itcast.cn"
elementFormDefault = "qualified" >
< xs:element name = "shiporder" >
< xs:complexType >
< xs:sequence >
< xs:element name = "orderperson1" type = "xs:string" />
< xs:element name = "shipto" >
< xs:complexType >
< xs:sequence >
< xs:element name = "name" type = "xs:string" />
< xs:element name = "address" type = "xs:string" />
< xs:element name = "city" type = "xs:string" />
< xs:element name = "country" type = "xs:string" />
</ xs:sequence >
</ xs:complexType >
</ xs:element >
< xs:element name = "item" maxOccurs = "unbounded" >
< xs:complexType >
< xs:sequence >
< xs:element name = "title" type = "xs:string" />
< xs:element name = "note" type = "xs:string" minOccurs = "0" />
< xs:element name = "quantity" type = "xs:positiveInteger" />
< xs:element name = "price" type = "xs:decimal" />
</ xs:sequence >
</ xs:complexType >
</ xs:element >
</ xs:sequence >
< xs:attribute name = "orderid" type = "xs:string" use = "required" />
</ xs:complexType >
</ xs:element > </ xs:schema >
< xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
targetNamespace = "http://www.itcast.cn"
elementFormDefault = "qualified" >
< xs:element name = "shiporder" >
< xs:complexType >
< xs:sequence >
< xs:element name = "orderperson1" type = "xs:string" />
< xs:element name = "shipto" >
< xs:complexType >
< xs:sequence >
< xs:element name = "name" type = "xs:string" />
< xs:element name = "address" type = "xs:string" />
< xs:element name = "city" type = "xs:string" />
< xs:element name = "country" type = "xs:string" />
</ xs:sequence >
</ xs:complexType >
</ xs:element >
< xs:element name = "item" maxOccurs = "unbounded" >
< xs:complexType >
< xs:sequence >
< xs:element name = "title" type = "xs:string" />
< xs:element name = "note" type = "xs:string" minOccurs = "0" />
< xs:element name = "quantity" type = "xs:positiveInteger" />
< xs:element name = "price" type = "xs:decimal" />
</ xs:sequence >
</ xs:complexType >
</ xs:element >
</ xs:sequence >
< xs:attribute name = "orderid" type = "xs:string" use = "required" />
</ xs:complexType >
</ xs:element > </ xs:schema >
<? xml version
= "1.0" encoding
= "UTF-8"
?>
< itheima:shiporder xmlns:itheima = "http://www.itcast.cn"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.itcast.cn shiporder.xsd"
orderid = "lkjj" >
< itheima:orderperson1 > 1 </ itheima:orderperson1 >
< itheima:shipto >
< itheima:name > 1 </ itheima:name >
< itheima:address > 2 </ itheima:address >
< itheima:city > 3 </ itheima:city >
< itheima:country > 4 </ itheima:country >
</ itheima:shipto >
< itheima:item >
< itheima:title > 1 </ itheima:title >
< itheima:note > 2 </ itheima:note >
< itheima:quantity > 3 </ itheima:quantity >
< itheima:price > 4 </ itheima:price >
</ itheima:item >
</ itheima:shiporder >
< itheima:shiporder xmlns:itheima = "http://www.itcast.cn"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.itcast.cn shiporder.xsd"
orderid = "lkjj" >
< itheima:orderperson1 > 1 </ itheima:orderperson1 >
< itheima:shipto >
< itheima:name > 1 </ itheima:name >
< itheima:address > 2 </ itheima:address >
< itheima:city > 3 </ itheima:city >
< itheima:country > 4 </ itheima:country >
</ itheima:shipto >
< itheima:item >
< itheima:title > 1 </ itheima:title >
< itheima:note > 2 </ itheima:note >
< itheima:quantity > 3 </ itheima:quantity >
< itheima:price > 4 </ itheima:price >
</ itheima:item >
</ itheima:shiporder >
四、Web开发入门:概念
JavaEE规范:很多种技术的集合总称。Servlet规范、JTA、JPA、JNDI、EJB等
JavaEE服务器启动的服务很多,称为重量级服务器。
Tomcat服务器启动的服务很少,该服务器支持全部JSP以及Servlet规范,所以称为轻量级服务器
五、Tomcat安装及配置(启动成功即可)
直接解压:不要解压到有空格或中文的目录中。
进入安装目录的bin目录下:执行startup.bat
在浏览器中输入:http://localhost:8080 看到公猫,succes。
配置:
必须有一个环境变量:JAVA_HOME=C:\jdk1.7.0_02(到目录即可,不要到bin目录)
环境变量path,在最后增加:C:\apache-tomcat-6.0.35\bin 多个值之间用分号分隔。
netstat -ano 查看是端口号占用情况
端口还必须没有被占用。
Tips:修改Tomcat默认的端口号。改为8888
Tomcat\conf\server.xml(Tomcat的核心配置文件 69行)
TOmcat\webapps:应用程序存放的目录。该目录中有几个文件夹就说明目前有几个应用。
Tomcat组件在server.xml文件中对应一种配置元素。
*****六、JavaWeb的标准目录结构(记住)
1、
HelloApp(应用名称下图中为web)
css|jsp|html
WEB-INF //必须有,且要大写。(Servlet规范规定)放到该目录中的资源,浏览器无法直接访问(保护重要资源)
classes //存放当前应用的class文件
com
itheima
Hello.class
lib //存放当前应用使用到的jar包。该目录中的jar包只为自己服务
web.xml //当前应用的配置文件
2、部署(知道)
打包
直接把应用拷贝到Tomcat\webapps(开放目录部署)
把应用打包war,把war拷贝到Tomcat\webapps(war部署)
注意:war部署拷贝到目录后,Tomcat会自动解包并放到webapps里,如果删除war,服务器也会删除解压的文件夹
如果想保留文件夹,需要关闭服务器,再删除war包
七、Tomcat一些配置(会配)
虚拟目录:对应的是一个Web应用
C:\apache-tomcat-6.0.35\webapps文件夹中的每一个目录就是一个虚拟目录。
练习:把任意一个目录配置成虚拟目录,让Tomcat来对外提供服务
方式一:不建议使用
找到Tomcat\conf\server.xml(该文件一旦变动,TOmcat必须重启才生效),在<Host>元素中增加子元素<Context/>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="/Hello" docBase="E:\HelloApp"/>
</Host>
path:虚拟目录名,一般都是以/开头
docBase:执行真实的本地应用的目录
就是取了一个别名而已

还有六种方式,开启Tomcat服务后在以下目录查找:
http://localhost:8080/docs/config/context.html
虚拟主机:对应的是一个网站<Host>
<Host name="www.itheima.com" appBase="e:\itheimaapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false"/>
<Host name="www.itcast.cn" appBase="e:\itcastapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false"/>
注意:不要忘记修改:C:\WINDOWS\system32\drivers\etc\ hosts文件.
综合小练习:配置默认主机、默认应用、默认端口、默认主页(部署人员)
http://localhost ==
http://localhost:80 /HelloApp/index.html 80默认端口。HTTP协议规定的。
默认应用:在Tomcat\conf\Catalina\localhost建立xml的配置文件,文件名就是应用的虚拟目录名。
如果文件名为ROOT.xml,该应用就是默认应用。(虽然这种方式不需要重启。由于特殊,也需要重启)
<Context docBase="D:\temp"/>
默认端口:
TOmcat\conf\server.xml(69行 改为80)
80为浏览器默认端口
默认主页:
修改应用中的web.xml
增加以下内容(不用记忆,在Tomcat的conf目录里的web.xml里复制):
再servlet配置默认主页,可以直接把映射的内容,去掉/复制到以下部分
例如/servlet/hello,把servlet/hello复制到以下,就是默认主页了
<welcome-file>servlet/hello</welcome-file>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
HTTPS=HTTP+SSL(服务套接字,加密传输信息),默认端口是443