本例子是参考了一些网站上有关JSP 对 XML 的操作的相关文档,又结合了一些个人的体会。例子涉及的内容是,开发的一个企业内部定餐系统后台管理端的部分代码,功能主要集中在对于餐馆基本信息的管理。
该例子本身开发的起因是我在原公司和同事们一个玩笑的一部分。特此也表达对那些一起共事的朋友们的想念。
例子本身是在TOMCAT4.01 平台下运行的B/S结构的程式。有关TOMCAT 的配置,这里不做说明。只讲解一下相关文件及文件夹的目录结构。
目录结构说明:
/tomcat/webapps/canyin/ -----主目录
/tomcat/webapps/canyin/jsp/ -----JSP 文件目录
/tomcat/webapps/canyin/jsp/admin/ -----实现后台管理的JSP 文件的存放目录
/tomcat/webapps/canyin/WEB-INF/classes/canyin/ ------javabean 文件的存放目录
/tomcat/webapps/canyin/data/ -----xml 文件存放目录
/tomcat/webapps/ROOT/ -----tomcat 启动文件存放文件夹,只存放了index.html 文件
文件简单说明:
/tomcat/webapps/canyin/data/users.xml -----记录用户信息
/tomcat/webapps/canyin/data/restaurants.xml -----记录餐馆的基础信息
/tomcat/webapps/ROOT/index.html -----首页,页面出现输入框,要求用户输入用户名,密码
/tomcat/webapps/canyin/jsp/loginjudge.jsp -----用户身份判断页面,根据用户名称和密码决定页面是转入后台管理端,还是前台客户端。本例子中,用户身份一旦确认为有管理权限,可以进入后台管理端,就直接跳到餐馆基本信息管理页面,简化说明的流程。
/tomcat/webapps/canyin/jsp/admin/admin_rest.jsp -----餐馆基本信息管理页面,管理餐馆的名称,电话,地址等信息
/tomcat/webapps/canyin/WEB-INF/classes/canyin/checkSessionBean.class ----- 后台管理端检测标志用户身份的session 的值,如果不是管理员的话,跳回登陆页面。
/tomcat/webapps/canyin/WEB-INF/classes/canyin/connXmlBean.class -----连接xml 文件
/tomcat/webapps/canyin/WEB-INF/classes/canyin/writeXmlBean.class -----写入xml文件
文件详细介绍及附带代码说明。
/tomcat/webapps/canyin/data/users.xml
代码:
1<?xml version="1.0" encoding="UTF-8" ?>
2\-
3
4<users>
5<user name="joard" password="joard" roles="admin"></user>
6<user name="joard01" password="joard01" roles="user"></user>
7<user name="joard02" password="joard02" roles="user"></user>
8</users>
9
10说明:字段含义是用户名,密码以及用户的身份
11
12/tomcat/webapps/canyin/data/restaurants.xml
13
14代码:
15<?xml version="1.0" encoding="UTF-8" ?>
16\-
17
18<restaurants num="10">
19\- <restaurant id="1">
20<name>上海亭快餐店</name>
21<phone>021-76546726</phone>
22
23<address>百老汇广场B座</address>
24
25</restaurant>
26\- <restaurant id="8">
27<name>香格里拉大饭店</name>
28<phone>021-2312134</phone>
29
30<address>南京路1023号</address>
31
32</restaurant>
33</restaurants>
34
35说明:
36
37<num>属性是记录在restaurants.xml 文件中总共有过多少条记录,每新增一条,无论以后删除是否,该值都会增加1,就好象数据库中习惯使用的自动增加1的id 项。用来给新增的 <restaurant>的属性<id>赋一个唯一的值。其它的字段意思比较明显。
38/tomcat/webapps/ROOT/index.html (单纯的HTML代码)
39
40代码:
41<html>
42
43<head>
44<title>oddWorld 餐饮系统</title>
45<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
46</head>
47
48<body onload="javascript:dataform.username.focus()">
49
50<div align="center">
51<table border="0" cellpadding="0" cellspacing="0" height="22" width="100%">
52<tr>
53<td width="1"><img height="22" src="images/top_r1.GIF" width="62"/></td>
54<td align="center" width="150"> 餐饮系统登录 </td>
55<td><img height="22" src="images/top_r2.GIF" width="294"/></td>
56</tr>
57</table>
58<br/>
59<br/>
60<table border="0" cellpadding="0" cellspacing="1" width="300">
61<tr>
62<td align="center" height="200" valign="top">
63<p align="center">
64<table bgcolor="#999999" border="0" cellpadding="5" cellspacing="1" class="a9px" width="100%">
65<tr>
66<td bgcolor="#efefef">餐饮系统登录</td>
67</tr>
68<tr>
69<td align="center" bgcolor="#FFFFFF" valign="top">
70<table border="0" cellpadding="0" cellspacing="0" width="100%">
71<form action="" canyin="" jsp="" loginjudge.jsp''="" method="post" name="dataform">
72<tr>
73<td width="100"><b>登录名:</b></td>
74<td>
75<input class="stedit" maxlength="16" name="username" value="joard"/>
76</td>
77</tr>
78<tr>
79<td width="100"><b>密码:</b></td>
80<td>
81<input class="stedit" maxlength="16" name="userpass" type="password" value="oddworld"/>
82</td>
83</tr>
84</form>
85</table>
86<br/>
87<table border="0" cellpadding="0" cellspacing="0">
88<tbody>
89<tr>
90<td>
91<input class="stbtm" name="update" onclick="javascript:if (checkform()==false);" type="button" value="登 录"/>
92</td>
93<td> </td>
94<td>
95<input class="stbtm" name="Submit" onclick="javascript:window.location.href=''index.asp?myjoke=1'';" type="button" value="修改密码"/>
96</td>
97<td> </td>
98</tr>
99</tbody>
100</table>
101<br/>
102</td>
103</tr>
104</table>
105</p></td>
106</tr>
107</table>
108</div>
109
110</body>
111</html>
112
113<script language="javascript">
114<!--
115function checkform()
116{
117var Checkblank = /^(\s*|(\ )|(\\.))*$/;
118if (Checkblank.test(dataform.username.value))
119{
120alert("登录名不能为空!");
121return false;
122}
123
124
125if (Checkblank.test(dataform.userpass.value))
126{
127alert("密码不能为空!");
128return false;
129}
130
131
132window.dataform.submit();
133
134}
135\-->
136
137</script>
138
139说明:把用户名称和用户密码提交到/tomcat/webapps/canyin/jsp/loginjudge.jsp
140
141/tomcat/webapps/canyin/WEB-INF/classes/canyin/checkSessionBean.class (代码是相应的java 文件)
142
143package canyin;
144
145import javax.servlet.http.HttpSession;
146import javax.servlet.http.HttpServletRequest;
147
148public class checkSessionBean {
149
150private boolean bolCheckPass=false;
151private HttpServletRequest request = null;
152
153public boolean checkSessionBean(HttpServletRequest request,String strSessionName,String strCheckValue){
154public boolean checkSessionBean(HttpServletRequest request){
155HttpSession session = request.getSession(false);
156return(bolCheckPass);
157
158if (strSessionName==null || strCheckValue==null){
159return(bolCheckPass);
160}else{
161if (session!=null && session.getValue(strSessionName)!=null){
162bolCheckPass=session.getValue(strSessionName).equals(strCheckValue);
163}
164
165return(bolCheckPass);
166}
167}
168}
169
170说明:检验参数传入的session 名称的数值和参数传入的字段的数值是否相等。
171
172/tomcat/webapps/canyin/WEB-INF/classes/canyin/connXmlBean.class
173
174代码:
175package canyin;
176
177import javax.xml.parsers.*;
178import javax.xml.transform.*;
179import javax.xml.transform.dom.DOMSource;
180import javax.xml.transform.stream.StreamResult;
181import org.w3c.dom.*;
182
183public class connXmlBean {
184
185private DocumentBuilderFactory factory=null;
186private DocumentBuilder builder=null;
187private Document doc=null;
188
189public connXmlBean(){}
190
191public String connXml(String xmlFileName){
192
193String strExc="";
194
195try{
196factory = DocumentBuilderFactory.newInstance();
197builder=factory.newDocumentBuilder();
198doc=builder.parse(xmlFileName);
199doc.normalize();
200}catch(Exception e){
201strExc=e.toString();
202}
203
204return(strExc);
205}
206
207public Document getXmlDoc(){
208return(doc);
209}
210}
211
212说明:打开一个指定xml 文件
213
214/tomcat/webapps/canyin/WEB-INF/classes/canyin/writeXmlBean.class
215
216代码:
217package canyin;
218
219import javax.xml.parsers.*;
220import javax.xml.transform.*;
221import javax.xml.transform.dom.DOMSource;
222import javax.xml.transform.stream.StreamResult;
223import java.io.File;
224import org.w3c.dom.*;
225
226public class writeXmlBean {
227
228public writeXmlBean(){}
229
230public String writeXml(Document doc,String xmlFileName){
231
232String strExc="";
233
234try{
235TransformerFactory tfactory = TransformerFactory.newInstance();
236Transformer transformer = tfactory.newTransformer();
237
238DOMSource source = new DOMSource(doc);
239
240StreamResult result = new StreamResult(new File(xmlFileName));
241
242transformer.transform(source,result);
243}catch(Exception e){
244strExc=e.toString();
245}
246
247return(strExc);
248}
249
250}
251
252说明:写入dom 的内容到一个指定的xml 文件。
253
254/tomcat/webapps/canyin/jsp/loginjudge.jsp
255
256代码:
257
258-- oddWorld 餐饮管理系统(简体中文版) 2002年12月1日
259copy right by joard ast
260
261loginjudge.jsp 功能:用户身份校验,根据 /data/user.xml 文件内标示的用户不同的身份
262决定转入后台管理页面,还是客户点菜页面。
263\--
264
265
266
267@ page contentType="text/html;charset=gb2312"
268
269
270@ page language="java" import="javax.xml.parsers.*"
271
272
273@ page import="org.w3c.dom.*"
274
275
276@ page import="canyin.*"
277
278
279<jsp:usebean class="canyin.connXmlBean" id="xmlBean" scope="page"></jsp:usebean>
280
281
282
283session.setMaxInactiveInterval(1800);
284
285
286Document doc;
287NodeList users;
288String strExc="";
289String strUsername,strPassword;
290
291strUsername=(String)request.getParameter("username");
292strPassword=(String)request.getParameter("userpass");
293
294//校验数据是否为空
295if (strUsername=="" || strPassword=="" ){
296out.println("<script javascript''="" language="">");
297out.println("alert(''用户名或密码有空值!'');");
298out.println("window.location.href=''/index.html'';");
299out.println("</script>");
300return;
301}
302
303xmlBean.connXml("webapps/canyin/data/users.xml");
304doc=xmlBean.getXmlDoc();
305
306try{
307users =doc.getElementsByTagName("user");
308
309for (int i=0;i<users.getlength();i++){ &&="" (stratrnamevalue.equals(strusername)="" (stratrrolevalue.equals("admin")){="" element="" if="" javascript''="" language="" out.println("<script="" stratrnamevalue='user.getAttributeNode("name").getNodeValue();' stratrpasswordvalue='user.getAttributeNode("password").getNodeValue();' stratrpasswordvalue.equals(strpassword)){="" stratrrolevalue='user.getAttributeNode("roles").getNodeValue();' string="" user="(Element)" users.item(i);="">");
310out.println("alert(''欢迎管理员登陆系统!'');");
311out.println("");
312
313//设置标示用户身份的 session(sesUserRole) ,管理员身份为 admin
314session.setAttribute("sesUserRole","admin");
315
316//跳转到管理页面
317response.sendRedirect("admin/admin_rest.jsp");
318return;
319
320}else{
321//设置标示用户身份的 session(sesUserRole) ,管理员身份为 user
322session.setAttribute("sesUserRole","user");
323
324//跳转到普通用户页面
325response.sendRedirect("index.jsp");
326return;
327}
328
329}else{
330out.println("<script javascript''="" language="">");
331out.println("alert(''用户名或密码错误!'');");
332out.println("history.go(-1);");
333out.println("</script>");
334return;
335}
336
337}
338}catch(Exception e){
339strExc=e.toString();
340}
341
342
343说明:.......
344
345/tomcat/webapps/canyin/jsp/admin/admin_rest.jsp
346
347代码:
348
349-- oddWorld 餐饮管理系统(简体中文版) 2002年12月1日
350copy right by joard ast
351
352admin_rest.jsp 功能:后台管理页面,餐馆管理页面。
353\--
354
355
356
357@ page contentType="text/html;charset=gb2312"
358
359
360@ page language="java" import="javax.xml.parsers.*"
361
362
363@ page import="javax.xml.transform.*"
364
365
366@ page import="org.w3c.dom.*"
367
368
369@ page import="canyin.*"
370
371
372
373@ include file="../../include/sys_dialog.jsp"
374
375
376<jsp:usebean class="canyin.checkSessionBean" id="checkSessionBean" scope="page"></jsp:usebean>
377<jsp:usebean class="canyin.connXmlBean" id="xmlBean" scope="page"></jsp:usebean>
378<jsp:usebean class="canyin.writeXmlBean" id="writeXmlBean" scope="page"></jsp:usebean>
379
380
381//校验可户身份,判断是不是管理员
382if(!checkSessionBean.checkSessionBean(request,"sesUserRole","admin")){
383out.print(showDialog("您没有管理的权限!","/index.html"));
384return;
385}
386
387//从餐馆资料文件 rest.xml 中得到相关数据
388Document doc;
389NodeList restaurants;
390
391String strAct;
392int intId=0;
393String strOperation="show";
394
395//接受外部传入的参数
396strAct=(String)request.getParameter("act");
397
398xmlBean.connXml("webapps/canyin/data/restaurants.xml");
399doc=xmlBean.getXmlDoc();
400restaurants =doc.getElementsByTagName("restaurant");
401
402//根据外部传入的参数来决定对 restaurant.xml 文件的操作
403if (strAct!=null){
404if(strAct.equals("addnewDo")){
405
406String strName;
407String strPhone;
408String strAddress;
409Text textseg;
410
411strName=(String)request.getParameter("name").trim();
412strPhone=(String)request.getParameter("phone").trim();
413strAddress=(String)request.getParameter("address").trim();
414
415//数据校验
416if(strName==null){
417out.print(showDialog("餐馆名称不能为空!"));
418return;
419}
420if(strPhone==null){
421out.print(showDialog("餐馆电话不能为空!"));
422return;
423}
424/*if(strAddress==null){
425out.print(showDialog("餐馆地址不能为空!"));
426return;
427}*/
428
429//校验数据的唯一性
430for(int i=0;i<restaurants.getlength();i++){ &&="" (restaurants.getlength()="0){" (stract="null){" (stract.equals("modi")){="" ){="" *if(straddress="null){" ;="" <html="" ="" attr="" attribute="" attribute("id",string.valueof(intnum));="" boolean="" canyin="" data="" delid='Integer.parseInt(request.getParameter("recordId").trim());' delid;="" delnode="(Node)restaurants.item(intI);" doc.getdocumentelement().appendchild(newrestaurant);="" doc.getelementsbytagname("restaurants").item(0).removechild(delnode);="" e){}="" element="" file:="" for(int="" i="0;i<restaurants.getLength();i++){" if="" if(!recordexist){="" if(((string)restaurant.getelementsbytagname("name").item(0).getfirstchild().getnodevalue()).equals(strname)="" if(((string)restaurant.getelementsbytagname("name").item(0).getfirstchild().getnodevalue()).equals(strname)){="" if(((string)restaurant.getelementsbytagname("name").item(0).getfirstchild().getnodevalue()).equals(strphone)="" if(((string)restaurant.getelementsbytagname("name").item(0).getfirstchild().getnodevalue()).equals(strphone)){="" if(delid="0){" if(integer.parseint(restaurant.getattributenode("id").getnodevalue())="delId){" if(modiid="0){" if(stract.equals("addnew")){="" if(stract.equals("del")){="" if(stract.equals("modido")){="" if(strname="null){" if(strphone="null){" int="" integer.parseint(restaurant.getattributenode("id").getnodevalue())!="modiId" inti="i;" intid='Integer.parseInt(request.getParameter("recordId"));' intnum='Integer.parseInt(restNum.getAttributeNode("num").getNodeValue());' intnum+="1;" modiid='Integer.parseInt(request.getParameter("recordId").trim());' modiid;="" modirestaurant="(Element)" modirestaurant.getelementsbytagname("address").item(0).getfirstchild().setnodevalue(straddress);="" modirestaurant.getelementsbytagname("name").item(0).getfirstchild().setnodevalue(strname);="" modirestaurant.getelementsbytagname("phone").item(0).getfirstchild().setnodevalue(strphone);="" newaddress='doc.createElement("address");' newaddress.appendchild(textseg);="" newarrid="new" newarrid.setvalue(string.valueof(intnum));="" newname='doc.createElement("name");' newname.appendchild(textseg);="" newphone='doc.createElement("phone");' newphone.appendchild(textseg);="" newrestaurant='doc.createElement("restaurant");' newrestaurant.appendchild(newaddress);="" newrestaurant.appendchild(newname);="" newrestaurant.appendchild(newphone);="" newrestaurant.setattributenode(newarrid);="" node="" out.print(showdialog("你要修改餐馆的记录不存在!"));="" out.print(showdialog("你要删除餐馆的记录不存在!"));="" out.print(showdialog("餐馆名称不能为空!"));="" out.print(showdialog("餐馆名称重复!"));="" out.print(showdialog("餐馆地址不能为空!"));="" out.print(showdialog("餐馆电话不能为空!"));="" out.print(showdialog("餐馆电话重复!"));="" recordexist="true;" response.sendredirect(request.getrequesturi());="" restaurant="(Element)" restaurants.item(i);="" restaurants.item(inti);="" restaurants.xml");="" restnum='(Element)doc.getElementsByTagName("restaurants").item(0);' restnum.getattributenode("num").setnodevalue(string.valueof(intnum));="" return;="" straddress='(String)request.getParameter("address").trim();' straddress;="" string="" strname='(String)request.getParameter("name").trim();' strname;="" stroperation="addnew" strphone='(String)request.getParameter("phone").trim();' strphone;="" text="" textseg="doc.createTextNode(strAddress);" textseg;="" try{="" writexmlbean.writexml(doc,"webapps="" }="" }*="" }catch(exception="" }else{="" 为restaurants的属性num="" 写入相应的xml文件="" 如果为空记录,则变更页面状态为“新增”="" 属性="" 得到已有的记录数,给新增的餐馆记录设定唯一的递增的id="" 数据校验="" 新增节点="" 标志显示记录存在="" 校验数据的唯一性="" 由外部传入参数决定页面相应的处理状态="" 的数值加1="" 记录要修改的记录是item(i)的哪一项="" 调用bean="" 进行删除操作="" 进行记录删除的操作="" 进行记录更改的操作="">
431
432<head>
433<title>oddWorld 餐饮系统</title>
434<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
435<meta content="0" http-equiv="expires"/>
436<link href="../../include/itsp.css" rel="stylesheet" type="text/css"/>
437</head>
438
439<body>
440
441<div align="center">
442<table border="0" cellpadding="0" cellspacing="0" height="22" width="100%">
443<tr>
444<td width="1"><img height="22" src="../../images/top_r1.GIF" width="62"/></td>
445<td align="center" width="150"> 餐饮系统管理--餐馆管理</td>
446<td><img height="22" src="../../images/top_r2.GIF" width="294"/></td>
447<td align="center" width="100"><a href="/index.html">[ 退出系统 ]</a></td>
448</tr>
449</table>
450<br/>
451<br/>
452<table align="center" bgcolor="#999999" border="0" cellpadding="1" cellspacing="1" width="90%">
453<tbody>
454<tr align="center" bgcolor="#efefef" valign="middle">
455<td class="ttTable" height="30" width="20"> </td>
456<td class="ttTable" height="30" width="0">餐馆名称</td>
457<td class="ttTable" height="30" width="0">餐馆电话</td>
458<td class="ttTable" height="30" width="0">
459<div align="center">餐馆地址</div>
460</td>
461<td class="ttTable" height="30" width="30">
462<div align="center">修改</div>
463</td>
464<td class="ttTable" height="30" width="30">
465<div align="center">删除</div>
466</td>
467</tr>
468
469
470
471for(int i=0;i<restaurants.getlength();i++) &&="" (stroperation="modi" <tr="" ="" align="center" bgcolor="#ffffff" element="" if="" integer.parseint(restaurant.getattributenode("id").getnodevalue())="intId){" restaurant="(Element)" restaurants.item(i);="" valign="middle" {="" 显示修改的格式="">
472
473<form action="
474=request.getRequestURI()
475?act=modiDo" checkform(this);''="" method="post" name="dataform" onsubmit="" return="">
476<td class="tdsmall" height="25" width="20">
477<input "="" id").getnodevalue()="" name="recordId" type="hidden" value="
478=restaurant.getAttributeNode("/>
479
480
481=(i+1)
482</td>
483<td class="tdsmall" height="25">
484<input "="" ="" class="stedit" maxlength="40" name="name" name").item(0).haschildnodes()){="" out.print(restaurant.getelementsbytagname("name").item(0).getfirstchild().getnodevalue());="" style="HEIGHT: 22px; WIDTH: 150px" value="
485if(restaurant.getElementsByTagName(" }=""/>
486</td>
487<td class="tdsmall" height="25">
488<input "="" class="stedit" maxlength="20" name="phone" out.print(restaurant.getelementsbytagname("phone").item(0).getfirstchild().getnodevalue());="" phone").item(0).haschildnodes()){="" style="HEIGHT: 22px; WIDTH: 100px" value="
489if(restaurant.getElementsByTagName(" }=""/>
490</td>
491<td class="tdsmall" height="25">
492<input "="" address").item(0).haschildnodes()){="" class="stedit" maxlength="100" name="address" out.print(restaurant.getelementsbytagname("address").item(0).getfirstchild().getnodevalue());="" style="HEIGHT: 22px; WIDTH: 200px" value="
493
494
495if(restaurant.getElementsByTagName(" }=""/>
496</td>
497<td class="tdsmall" height="25" width="25"><a href="javascript:if (checkform()==false);"><img border="0" height="15" src="../../images/editok.gif" width="15"/></a></td>
498<td class="tdsmall" height="25" width="25"> </td>
499</form>
500
501
502 }else{
503//显示正常的格式
504
505<tr align="center" bgcolor="#ffffff" valign="middle">
506<td class="tdsmall" height="25" width="20">
507=(i+1)
508</td>
509<td class="tdsmall" height="25" width="0">
510if(restaurant.getElementsByTagName("name").item(0).hasChildNodes()){
511out.print(restaurant.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
512
513}
514
515</td>
516<td class="tdsmall" height="25" width="0">
517if(restaurant.getElementsByTagName("phone").item(0).hasChildNodes()){
518out.print(restaurant.getElementsByTagName("phone").item(0).getFirstChild().getNodeValue());
519
520}
521</td>
522<td class="tdsmall" height="25" width="0">
523
524
525if(restaurant.getElementsByTagName("address").item(0).hasChildNodes()){
526out.print(restaurant.getElementsByTagName("address").item(0).getFirstChild().getNodeValue());
527
528}
529
530</td>
531<td class="tdsmall" height="25" width="30"><a "="" href="
532=request.getRequestURI()
533?act=modi&amp;recordId=
534=restaurant.getAttributeNode(" id").getnodevalue()=""><img border="0" height="15" src="../../images/edit.gif" width="15"/></a></td>
535<td class="tdsmall" height="25" width="30"><img '';}"="" border="0" height="15" id").getnodevalue()="" onclick="javascript:if(confirm(''您是否确定删除本记录,删除后将导至记录无法使用?'')){window.location.href=''
536=request.getRequestURI()
537?act=del&amp;recordId=
538=restaurant.getAttributeNode(" src="../../images/delete.gif" style="CURSOR: hand" width="15"/> </td>
539</tr>
540
541 }
542}
543
544
545 if (strOperation=="addnew"){
546//显示新增的格式
547
548<tr align="center" bgcolor="#ffffff" valign="middle">
549
550<form action="
551=request.getRequestURI()
552?act=addnewDo" checkform2(this);''="" method="post" name="dataform2" onsubmit="" return="">
553<td class="tdsmall" height="25" width="20"></td>
554<td class="tdsmall" height="25">
555<input class="stedit" maxlength="40" name="name" style="HEIGHT: 22px; WIDTH: 150px" value=""/>
556</td>
557<td class="tdsmall" height="25">
558<input class="stedit" maxlength="20" name="phone" style="HEIGHT: 22px; WIDTH: 100px" value=""/>
559</td>
560<td class="tdsmall" height="25">
561<input class="stedit" maxlength="100" name="address" style="HEIGHT: 22px; WIDTH: 200px" value=""/>
562</td>
563<td class="tdsmall" height="25" width="25"><a href="javascript:if (checkform2()==false);"><img border="0" height="15" src="../../images/editok.gif" width="15"/></a></td>
564<td class="tdsmall" height="25" width="25"> </td>
565</form>
566
567</tr>
568
569 }
570
571</restaurants.getlength();i++)></tbody>
572</table>
573<br/>
574
575<table align="center" border="0" cellpadding="0" cellspacing="2" width="95%">
576<tbody>
577<tr valign="center">
578<td align="middle"> <br/>
579<table border="0" cellpadding="0" cellspacing="0">
580<tr>
581<td>
582
583
584 if (strOperation=="addnew"){
585
586
587<input class="stbtm" name="update" onclick="javascript:if (checkform2()==false);" type="button" value="更新记录"/>
588
589 }else{
590if(strOperation=="modi"){
591
592
593<input class="stbtm" name="update" onclick="javascript:if (checkform()==false);" type="button" value="更新记录"/>
594
595
596}else{
597
598
599<input class="stbtm" name="Button" onclick="javascript:window.location.href=''
600=request.getRequestURI()
601?act=addnew'';" type="button" value="新 增"/>
602
603}
604}
605
606</td>
607<td>
608<input class="stbtm" name="Button" onclick="javascript:window.location.href=''index.jsp'';" type="button" value="返 回"/>
609</td>
610</tr>
611</table>
612</td>
613</tr>
614</tbody></table>
615
616<p> </p>
617
618</div>
619</body>
620
621<script language="javascript">
622<!--
623function checkform2()
624{
625var Checkblank = /^(\s*|(\ )|(\\.))*$/;
626
627
628if (Checkblank.test(dataform2.name.value))
629{
630alert("餐馆名称不能为空!");
631dataform2.name.focus();
632return false;
633}
634
635if (Checkblank.test(dataform2.phone.value))
636{
637alert("餐馆电话不能为空!");
638dataform2.phone.focus();
639return false;
640}
641window.dataform2.submit();
642}
643
644function checkform()
645{
646var Checkblank = /^(\s*|(\ )|(\\.))*$/;
647if (Checkblank.test(dataform.name.value))
648{
649alert("餐馆名称不能为空!");
650dataform.name.focus();
651return false;
652}
653
654if (Checkblank.test(dataform.phone.value))
655{
656alert("餐馆电话不能为空!");
657dataform.phone.focus();
658return false;
659}
660
661
662window.dataform.submit();
663}
664\-->
665</script>
666说明:本文件的书写有很多地方并不简练,因为在程式的开发过程中,过分简练的程序往往会带来后期维护的困难。
667
668开发心得:
669
670doc.getElementsByTagName("restaurants").item(int i)的返回值是node 型,如果不是要调用它的属性值,没有必要强制转型为 Element型。可以直接操作。本系统因为开发的参考资料的错误,所以全都采用了强制转型。可以在以后的开发中考虑使用node 直接进行操作。
671
672trim() 和 Interger.parseInt() 函数都不可以接受null 型的数值
673
674在tomcat 下左右的文件都是目录从TOMCAT 算起,具体情况请参见\webapps\canyin\jsp\userjudge.jsp 里关于xml 路径的写法。
675
676
677对原代码感兴趣的朋友请通过如下信箱和我联系,joard@163.com</restaurants.getlength();i++){></users.getlength();i++){></id></restaurant></num>