本例子是參考了一些網(wǎng)站上有關(guān) JSP 對(duì) XML 的操作的相關(guān)文檔,又結(jié)合了一些個(gè)人的體會(huì)。例子涉及的內(nèi)容是,開(kāi)發(fā)的一個(gè)企業(yè)內(nèi)部定餐系統(tǒng)后臺(tái)管理端的部分代碼,功能主要集中在對(duì)于餐館基本信息的管理。
該例子本身開(kāi)發(fā)的起因是我在原公司和同事們一個(gè)玩笑的一部分。特此也表達(dá)對(duì)那些一起共事的朋友們的想念。
例子本身是在 TOMCAT4.01 平臺(tái)下運(yùn)行的 B/S 結(jié)構(gòu)的程式。有關(guān) TOMCAT 的配置,這里不做說(shuō)明。只講解一下相關(guān)文件及文件夾的目錄結(jié)構(gòu)。
目錄結(jié)構(gòu)說(shuō)明:
/tomcat/webapps/canyin/ ----- 主目錄
/tomcat/webapps/canyin/jsp/ -----JSP 文件目錄
/tomcat/webapps/canyin/jsp/admin/ ----- 實(shí)現(xiàn)后臺(tái)管理的 JSP 文件的存放目錄
/tomcat/webapps/canyin/WEB-INF/classes/canyin/ ------javabean 文件的存放目錄
/tomcat/webapps/canyin/data/ -----xml 文件存放目錄
/tomcat/webapps/ROOT/ -----tomcat 啟動(dòng)文件存放文件夾,只存放了 index.html 文件
文件簡(jiǎn)單說(shuō)明:
/tomcat/webapps/canyin/data/users.xml ----- 記錄用戶(hù)信息
/tomcat/webapps/canyin/data/restaurants.xml ----- 記錄餐館的基礎(chǔ)信息
/tomcat/webapps/ROOT/index.html ----- 首頁(yè),頁(yè)面出現(xiàn)輸入框,要求用戶(hù)輸入用戶(hù)名,密碼
/tomcat/webapps/canyin/jsp/loginjudge.jsp ----- 用戶(hù)身份判斷頁(yè)面,根據(jù)用戶(hù)名稱(chēng)和密碼決定頁(yè)面是轉(zhuǎn)入后臺(tái)管理端,還是前臺(tái)客戶(hù)端。本例子中,用戶(hù)身份一旦確認(rèn)為有管理權(quán)限,可以進(jìn)入后臺(tái)管理端,就直接跳到餐館基本信息管理頁(yè)面,簡(jiǎn)化說(shuō)明的流程。
/tomcat/webapps/canyin/jsp/admin/admin_rest.jsp ----- 餐館基本信息管理頁(yè)面,管理餐館的名稱(chēng),電話,地址等信息
/tomcat/webapps/canyin/WEB-INF/classes/canyin/checkSessionBean.class ----- 后臺(tái)管理端檢測(cè)標(biāo)志用戶(hù)身份的 session 的值,如果不是管理員的話,跳回登陸頁(yè)面。
/tomcat/webapps/canyin/WEB-INF/classes/canyin/connXmlBean.class ----- 連接 xml 文件
/tomcat/webapps/canyin/WEB-INF/classes/canyin/writeXmlBean.class ----- 寫(xiě)入 xml 文件
文件詳細(xì)介紹及附帶代碼說(shuō)明。
/tomcat/webapps/canyin/data/users.xml
代碼:
<?xml version="1.0" encoding="UTF-8" ?>
- <users>
<user name="joard" password="joard" roles="admin" />
<user name="joard01" password="joard01" roles="user" />
<user name="joard02" password="joard02" roles="user" />
</users>
說(shuō)明:字段含義是用戶(hù)名,密碼以及用戶(hù)的身份
/tomcat/webapps/canyin/data/restaurants.xml
代碼:
<?xml version="1.0" encoding="UTF-8" ?>
- <restaurants num="10">
- <restaurant id="1">
<name> 上海亭快餐店 </name>
<phone>021-76546726</phone>
<address> 百老匯廣場(chǎng) B 座 </address>
</restaurant>
- <restaurant id="8">
<name> 香格里拉大飯店 </name>
<phone>021-2312134</phone>
<address> 南京路 1023 號(hào) </address>
</restaurant>
</restaurants>
說(shuō)明: <num> 屬性是記錄在 restaurants.xml 文件中總共有過(guò)多少條記錄,每新增一條,無(wú)論以后刪除是否,該值都會(huì)增加 1 ,就好象數(shù)據(jù)庫(kù)中習(xí)慣使用的自動(dòng)增加 1 的 id 項(xiàng)。用來(lái)給新增的 <restaurant> 的屬性 <id> 賦一個(gè)唯一的值。其它的字段意思比較明顯。
/tomcat/webapps/ROOT/index.html (單純的 HTML 代碼)
代碼:
<html>
<head>
<title>oddWorld 餐飲系統(tǒng) </title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body onload="javascript:dataform.username.focus()">
<div align="center">
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="22">
<tr>
<td width="1"><img src="http://www.knowsky.com/images/top_r1.GIF" width="62" height="22"></td>
<td width=150 align="center"> 餐飲系統(tǒng)登錄 </td>
<td><img src="http://www.knowsky.com/images/top_r2.GIF" width="294" height="22"></td>
</tr>
</table>
<br>
<br>
<table width="300" border="0" cellspacing="1" cellpadding="0" >
<tr>
<td height="200" valign="top" align="center">
<p align="center">
<table width="100%" border="0" cellspacing="1" cellpadding="5" bgcolor=#999999 class=a9px>
<tr>
<td bgcolor="#efefef"> 餐飲系統(tǒng)登錄 </td>
</tr>
<tr>
<td bgcolor="#FFFFFF" valign="top" align="center">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<form name=dataform method=post action=''canyin/jsp/loginjudge.jsp''>
<tr>
<td width="100"><b> 登錄名: </b></td>
<td>
<input maxlength=16
name="username" class=stedit value="joard">
</td>
</tr>
<tr>
<td width="100"><b> 密碼: </b></td>
<td>
<input class=stedit maxlength=16
name="userpass" type=password value="oddworld">
</td>
</tr>
</form>
</table>
<br>
<table border=0 cellpadding=0 cellspacing=0>
<tbody>
<tr>
<td>
<input class=stbtm name=update onClick="javascript:if (checkform()==false);" type=button value=" 登 錄 ">
</td>
<td> </td>
<td>
<input class=stbtm name=Submit onClick="javascript:window.location.href=''index.asp?myjoke=1'';" type=button value=" 修改密碼 ">
</td>
<td> </td>
</tr>
</tbody>
</table>
<br>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</body>
</html>
<script language=javascript>
<!--
function checkform()
{
var Checkblank = /^(\s*|(\ )|(\.))*$/;
if (Checkblank.test(dataform.username.value))
{
alert(" 登錄名不能為空 !");
return false;
}
if (Checkblank.test(dataform.userpass.value))
{
alert(" 密碼不能為空 !");
return false;
}
window.dataform.submit();
}
-->
</script>
說(shuō)明:把用戶(hù)名稱(chēng)和用戶(hù)密碼提交到 /tomcat/webapps/canyin/jsp/loginjudge.jsp
/tomcat/webapps/canyin/WEB-INF/classes/canyin/checkSessionBean.class (代碼是相應(yīng)的 java 文件)
package canyin;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
public class checkSessionBean {
private boolean bolCheckPass=false;
private HttpServletRequest request = null;
public boolean checkSessionBean(HttpServletRequest request,String strSessionName,String strCheckValue){
public boolean checkSessionBean(HttpServletRequest request){
HttpSession session = request.getSession(false);
return(bolCheckPass);
if (strSessionName==null || strCheckValue==null){
return(bolCheckPass);
}else{
if (session!=null && session.getValue(strSessionName)!=null){
bolCheckPass=session.getValue(strSessionName).equals(strCheckValue);
}
return(bolCheckPass);
}
}
}
說(shuō)明:檢驗(yàn)參數(shù)傳入的 session 名稱(chēng)的數(shù)值和參數(shù)傳入的字段的數(shù)值是否相等。
/tomcat/webapps/canyin/WEB-INF/classes/canyin/connXmlBean.class
代碼:
package canyin;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
public class connXmlBean {
private DocumentBuilderFactory factory=null;
private DocumentBuilder builder=null;
private Document doc=null;
public connXmlBean(){}
public String connXml(String xmlFileName){
String strExc="";
try{
factory = DocumentBuilderFactory.newInstance();
builder=factory.newDocumentBuilder();
doc=builder.parse(xmlFileName);
doc.normalize();
}catch(Exception e){
strExc=e.toString();
}
return(strExc);
}
public Document getXmlDoc(){
return(doc);
}
}
說(shuō)明:打開(kāi)一個(gè)指定 xml 文件
/tomcat/webapps/canyin/WEB-INF/classes/canyin/writeXmlBean.class
代碼:
package canyin;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import org.w3c.dom.*;
public class writeXmlBean {
public writeXmlBean(){}
public String writeXml(Document doc,String xmlFileName){
String strExc="";
try{
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer transformer = tfactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(xmlFileName));
transformer.transform(source,result);
}catch(Exception e){
strExc=e.toString();
}
return(strExc);
}
}
說(shuō)明:寫(xiě)入 dom 的內(nèi)容到一個(gè)指定的 xml 文件。
/tomcat/webapps/canyin/jsp/loginjudge.jsp
代碼:
<%-- oddWorld 餐飲管理系統(tǒng) ( 簡(jiǎn)體中文版 ) 2002 年 12 月 1 日
copy right by joard ast
loginjudge.jsp 功能:用戶(hù)身份校驗(yàn),根據(jù) /data/user.xml 文件內(nèi)標(biāo)示的用戶(hù)不同的身份
決定轉(zhuǎn)入后臺(tái)管理頁(yè)面,還是客戶(hù)點(diǎn)菜頁(yè)面。
--%>
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page language="java" import="javax.xml.parsers.*" %>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="canyin.*" %>
<jsp:useBean id="xmlBean" class="canyin.connXmlBean" scope="page" />
<%
session.setMaxInactiveInterval(1800);
Document doc;
NodeList users;
String strExc="";
String strUsername,strPassword;
strUsername=(String)request.getParameter("username");
strPassword=(String)request.getParameter("userpass");
// 校驗(yàn)數(shù)據(jù)是否為空
if (strUsername=="" || strPassword=="" ){
out.println("<script language=''javascript''>");
out.println("alert('' 用戶(hù)名或密碼有空值 !'');");
out.println("window.location.href=''/index.html'';");
out.println("</script>");
return;
}
xmlBean.connXml("webapps/canyin/data/users.xml");
doc=xmlBean.getXmlDoc();
try{
users =doc.getElementsByTagName("user");
for (int i=0;i<users.getLength();i++){
Element user=(Element) users.item(i);
String strAtrNameValue=user.getAttributeNode("name").getNodeValue();
String strAtrPassWordValue=user.getAttributeNode("password").getNodeValue();
String strAtrRoleValue=user.getAttributeNode("roles").getNodeValue();
if (strAtrNameValue.equals(strUsername) && strAtrPassWordValue.equals(strPassword)){
if (strAtrRoleValue.equals("admin")){
out.println("<script language=''javascript''>");
out.println("alert('' 歡迎管理員登陸系統(tǒng) !'');");
out.println("</script>");
// 設(shè)置標(biāo)示用戶(hù)身份的 session ( sesUserRole ) ,管理員身份為 admin
session.setAttribute("sesUserRole","admin");
// 跳轉(zhuǎn)到管理頁(yè)面
response.sendRedirect("admin/admin_rest.jsp");
return;
}else{
// 設(shè)置標(biāo)示用戶(hù)身份的 session ( sesUserRole ) ,管理員身份為 user
session.setAttribute("sesUserRole","user");
// 跳轉(zhuǎn)到普通用戶(hù)頁(yè)面
response.sendRedirect("index.jsp");
return;
}
}else{
out.println("<script language=''javascript''>");
out.println("alert('' 用戶(hù)名或密碼錯(cuò)誤 !'');");
out.println("history.go(-1);");
out.println("</script>");
return;
}
}
}catch(Exception e){
strExc=e.toString();
}
%>
說(shuō)明: .......
/tomcat/webapps/canyin/jsp/admin/admin_rest.jsp
代碼:
<%-- oddWorld 餐飲管理系統(tǒng) ( 簡(jiǎn)體中文版 ) 2002 年 12 月 1 日
copy right by joard ast
admin_rest.jsp 功能:后臺(tái)管理頁(yè)面,餐館管理頁(yè)面。
--%>
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page language="java" import="javax.xml.parsers.*" %>
<%@ page import="javax.xml.transform.*" %>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="canyin.*" %>
<%@ include file="../../include/sys_dialog.jsp" %>
<jsp:useBean id="checkSessionBean" class="canyin.checkSessionBean" scope="page" />
<jsp:useBean id="xmlBean" class="canyin.connXmlBean" scope="page" />
<jsp:useBean id="writeXmlBean" class="canyin.writeXmlBean" scope="page" />
<%// 校驗(yàn)可戶(hù)身份,判斷是不是管理員
if(!checkSessionBean.checkSessionBean(request,"sesUserRole","admin")){
out.print(showDialog(" 您沒(méi)有管理的權(quán)限 !","/index.html"));
return;
}
// 從餐館資料文件 rest.xml 中得到相關(guān)數(shù)據(jù)
Document doc;
NodeList restaurants;
String strAct;
int intId=0;
String strOperation="show";
// 接受外部傳入的參數(shù)
strAct=(String)request.getParameter("act");
xmlBean.connXml("webapps/canyin/data/restaurants.xml");
doc=xmlBean.getXmlDoc();
restaurants =doc.getElementsByTagName("restaurant");
// 根據(jù)外部傳入的參數(shù)來(lái)決定對(duì) restaurant.xml 文件的操作
if (strAct!=null){
if(strAct.equals("addnewDo")){
String strName;
String strPhone;
String strAddress;
Text textseg;
strName=(String)request.getParameter("name").trim();
strPhone=(String)request.getParameter("phone").trim();
strAddress=(String)request.getParameter("address").trim();
// 數(shù)據(jù)校驗(yàn)
if(strName==null){
out.print(showDialog(" 餐館名稱(chēng)不能為空 !"));
return;
}
if(strPhone==null){
out.print(showDialog(" 餐館電話不能為空 !"));
return;
}
/*if(strAddress==null){
out.print(showDialog(" 餐館地址不能為空 !"));
return;
}*/
// 校驗(yàn)數(shù)據(jù)的唯一性
for(int i=0;i<restaurants.getLength();i++){
Element restaurant=(Element) restaurants.item(i);
if(((String)restaurant.getElementsByTagName("name").item(0).getFirstChild().getNodeValue()).equals(strName)){
out.print(showDialog(" 餐館名稱(chēng)重復(fù) !"));
return;
}else{
if(((String)restaurant.getElementsByTagName("name").item(0).getFirstChild().getNodeValue()).equals(strPhone)){
out.print(showDialog(" 餐館電話重復(fù) !"));
return;
}
}
}
// 得到已有的記錄數(shù),給新增的餐館記錄設(shè)定唯一的遞增的 id 屬性
int intNum=0;
Element restNum=(Element)doc.getElementsByTagName("restaurants").item(0);
intNum=Integer.parseInt(restNum.getAttributeNode("num").getNodeValue());
intNum+=1;
// 為 restaurants 的屬性 num 的數(shù)值加 1
restNum.getAttributeNode("num").setNodeValue(String.valueOf(intNum));
// 新增節(jié)點(diǎn)
Element newRestaurant=doc.createElement("restaurant");
Attr newArrId=doc.createAttribute("id");
//Attribute newArrId = new Attribute("id",String.valueOf(intNum));
textseg=doc.createTextNode(String.valueOf(intNum));
newArrId.setValue(String.valueOf(intNum));
newRestaurant.setAttributeNode(newArrId);
Element newName=doc.createElement("name");
textseg=doc.createTextNode(strName);
newName.appendChild(textseg);
newRestaurant.appendChild(newName);
Element newPhone=doc.createElement("phone");
textseg=doc.createTextNode(strPhone);
newPhone.appendChild(textseg);
newRestaurant.appendChild(newPhone);
Element newAddress=doc.createElement("address");
textseg=doc.createTextNode(strAddress);
newAddress.appendChild(textseg);
newRestaurant.appendChild(newAddress);
doc.getDocumentElement().appendChild(newRestaurant);
// 調(diào)用 bean 寫(xiě)入相應(yīng)的 xml 文件
writeXmlBean.writeXml(doc,"webapps/canyin/data/restaurants.xml");
response.sendRedirect(request.getRequestURI());
return;
}
if(strAct.equals("modiDo")){
String strName;
String strPhone;
String strAddress;
Text textseg;
int modiId;
// 記錄要修改的記錄是 item(i) 的哪一項(xiàng)
int intI=0;
strName=(String)request.getParameter("name").trim();
strPhone=(String)request.getParameter("phone").trim();
strAddress=(String)request.getParameter("address").trim();
modiId=Integer.parseInt(request.getParameter("recordId").trim());
// 數(shù)據(jù)校驗(yàn)
if(strName==null){
out.print(showDialog(" 餐館名稱(chēng)不能為空 !"));
return;
}
if(strPhone==null){
out.print(showDialog(" 餐館電話不能為空 !"));
return;
}
if(modiId==0){
out.print(showDialog(" 你要修改餐館的記錄不存在 !"));
return;
}
/*if(strAddress==null){
out.print(showDialog(" 餐館地址不能為空 !"));
return;
}*/
// 標(biāo)志顯示記錄存在
boolean recordExist=false;
// 校驗(yàn)數(shù)據(jù)的唯一性
for(int i=0;i<restaurants.getLength();i++){
Element restaurant=(Element) restaurants.item(i);
if(Integer.parseInt(restaurant.getAttributeNode("id").getNodeValue())==modiId){
recordExist=true;
intI=i;
}
if(((String)restaurant.getElementsByTagName("name").item(0).getFirstChild().getNodeValue()).equals(strName) && Integer.parseInt(restaurant.getAttributeNode("id").getNodeValue())!=modiId ){
out.print(showDialog(" 餐館名稱(chēng)重復(fù) !"));
return;
}else{
if(((String)restaurant.getElementsByTagName("name").item(0).getFirstChild().getNodeValue()).equals(strPhone) && Integer.parseInt(restaurant.getAttributeNode("id").getNodeValue())!=modiId ){
out.print(showDialog(" 餐館電話重復(fù) !"));
return;
}
}
}
if(!recordExist){
out.print(showDialog(" 你要修改餐館的記錄不存在 !"));
return;
}else{
// 進(jìn)行記錄更改的操作
try{
Element modiRestaurant=(Element) restaurants.item(intI);
modiRestaurant.getElementsByTagName("name").item(0).getFirstChild().setNodeValue(strName);
modiRestaurant.getElementsByTagName("phone").item(0).getFirstChild().setNodeValue(strPhone);
modiRestaurant.getElementsByTagName("address").item(0).getFirstChild().setNodeValue(strAddress);
// 調(diào)用 bean 寫(xiě)入相應(yīng)的 xml 文件
writeXmlBean.writeXml(doc,"webapps/canyin/data/restaurants.xml");
response.sendRedirect(request.getRequestURI());
return;
}catch(Exception e){}
}
}
// 進(jìn)行刪除操作
if(strAct.equals("del")){
int delId;
// 記錄要修改的記錄是 item(i) 的哪一項(xiàng)
int intI=0;
delId=Integer.parseInt(request.getParameter("recordId").trim());
if(delId==0){
out.print(showDialog(" 你要修改餐館的記錄不存在 !"));
return;
}
file:// 標(biāo)志顯示記錄存在
boolean recordExist=false;
// 校驗(yàn)數(shù)據(jù)的唯一性
for(int i=0;i<restaurants.getLength();i++){
Element restaurant=(Element) restaurants.item(i);
if(Integer.parseInt(restaurant.getAttributeNode("id").getNodeValue())==delId){
recordExist=true;
intI=i;
}
}
if(!recordExist){
out.print(showDialog(" 你要?jiǎng)h除餐館的記錄不存在 !"));
return;
}else{
// 進(jìn)行記錄刪除的操作
try{
Node delNode=(Node)restaurants.item(intI);
doc.getElementsByTagName("restaurants").item(0).removeChild(delNode);
// 調(diào)用 bean 寫(xiě)入相應(yīng)的 xml 文件
writeXmlBean.writeXml(doc,"webapps/canyin/data/restaurants.xml");
response.sendRedirect(request.getRequestURI());
return;
}catch(Exception e){}
}
}
}
// 由外部傳入?yún)?shù)決定頁(yè)面相應(yīng)的處理狀態(tài)
if (strAct==null){
strOperation="show";
}else{
if (strAct.equals("modi")){
strOperation="modi";
intId=Integer.parseInt(request.getParameter("recordId"));
}else{
if(strAct.equals("addnew")){
strOperation="addnew";
}else{
strOperation="show";
}
}
}
// 如果為空記錄,則變更頁(yè)面狀態(tài)為“新增”
if (restaurants.getLength()==0){
strOperation="addnew";
}
%>
<html>
<head>
<title>oddWorld 餐飲系統(tǒng) </title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="expires" content="0">
<link rel="stylesheet" href="../../include/itsp.css" type="text/css">
</head>
<body >
<div align="center">
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="22">
<tr>
<td width="1"><img src="../../http://www.knowsky.com/images/top_r1.GIF" width="62" height="22"></td>
<td width=150 align="center"> 餐飲系統(tǒng)管理 -- 餐館管理 </td>
<td><img src="../../http://www.knowsky.com/images/top_r2.GIF" width="294" height="22"></td>
<td width=100 align="center"><a href="/index.html">[ 退出系統(tǒng) ]</a></td>
</tr>
</table>
<br>
<br>
<table bgcolor="#999999" align=center border=0 cellpadding=1 cellspacing=1
width="90%">
<tbody>
<tr bgcolor="#efefef" align="center" valign="middle">
<td class=ttTable height=30 width="20"> </td>
<td class=ttTable height=30 width="0"> 餐館名稱(chēng) </td>
<td class=ttTable height=30 width="0"> 餐館電話 </td>
<td class=ttTable height=30 width="0">
<div align="center"> 餐館地址 </div>
</td>
<td class=ttTable height=30 width="30">
<div align="center"> 修改 </div>
</td>
<td class=ttTable height=30 width="30">
<div align="center"> 刪除 </div>
</td>
</tr>
<%
for(int i=0;i<restaurants.getLength();i++)
{
Element restaurant=(Element) restaurants.item(i);
if (strOperation=="modi" && Integer.parseInt(restaurant.getAttributeNode("id").getNodeValue())==intId){
%>
<%// 顯示修改的格式 %>
<tr align="center" bgcolor="#ffffff" valign="middle">
<form name=dataform action="<%=request.getRequestURI()%>?act=modiDo" method="post" onSubmit=''return checkform(this);'' >
<td class=tdsmall height=25 width="20">
<input type="hidden" name="recordId" value="<%=restaurant.getAttributeNode("id").getNodeValue()%>">
<%=(i+1)%></td>
<td class=tdsmall height=25>
<input name="name" class=stedit
style="HEIGHT: 22px; WIDTH: 150px" value="<%if(restaurant.getElementsByTagName("name").item(0).hasChildNodes()){
out.print(restaurant.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
}%>
" maxlength="40" >
</td>
<td class=tdsmall height=25>
<input name="phone" class=stedit
style="HEIGHT: 22px; WIDTH: 100px" value="<%if(restaurant.getElementsByTagName("phone").item(0).hasChildNodes()){
out.print(restaurant.getElementsByTagName("phone").item(0).getFirstChild().getNodeValue());
}%>" maxlength="20" >
</td>
<td class=tdsmall height=25>
<input name="address" class=stedit
style="HEIGHT: 22px; WIDTH: 200px" value="<%
if(restaurant.getElementsByTagName("address").item(0).hasChildNodes()){
out.print(restaurant.getElementsByTagName("address").item(0).getFirstChild().getNodeValue());
}%>" maxlength="100" >
</td>
<td class=tdsmall height=25 width="25"><a href="javascript:if (checkform()==false);"><img border=0
height=15 src="http://www.knowsky.com/images/editok.gif" width=15></a></td>
<td class=tdsmall height=25 width="25"> </td>
</form>
</tr>
<% }else{
// 顯示正常的格式 %>
<tr align="center" bgcolor="#ffffff" valign="middle">
<td class=tdsmall height=25 width="20"><%=(i+1)%></td>
<td class=tdsmall height=25 width="0"><%if(restaurant.getElementsByTagName("name").item(0).hasChildNodes()){
out.print(restaurant.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
}%>
</td>
<td class=tdsmall height=25 width="0"><%if(restaurant.getElementsByTagName("phone").item(0).hasChildNodes()){
out.print(restaurant.getElementsByTagName("phone").item(0).getFirstChild().getNodeValue());
}%></td>
<td class=tdsmall height=25 width="0">
<%
if(restaurant.getElementsByTagName("address").item(0).hasChildNodes()){
out.print(restaurant.getElementsByTagName("address").item(0).getFirstChild().getNodeValue());
}%>
</td>
<td class=tdsmall height=25 width="30"><a href="<%=request.getRequestURI()%>?act=modi&recordId=<%=restaurant.getAttributeNode("id").getNodeValue()%>"><img border=0
height=15 src="http://www.knowsky.com/images/edit.gif" width=15></a></td>
<td class=tdsmall height=25 width="30"><img border=0
height=15
onClick="javascript:if(confirm('' 您是否確定刪除本記錄,刪除后將導(dǎo)至記錄無(wú)法使用? '')){window.location.href=''<%=request.getRequestURI()%>?act=del&recordId=<%=restaurant.getAttributeNode("id").getNodeValue()%>'';}"
src="http://www.knowsky.com/images/delete.gif" style="CURSOR: hand" width=15> </td>
</tr>
<% }
}%>
<% if (strOperation=="addnew"){
// 顯示新增的格式 %>
<tr align="center" bgcolor="#ffffff" valign="middle">
<form name=dataform2 action="<%=request.getRequestURI()%>?act=addnewDo" method="post" onSubmit=''return checkform2(this);'' >
<td class=tdsmall height=25 width="20"></td>
<td class=tdsmall height=25>
<input name="name" class=stedit
style="HEIGHT: 22px; WIDTH: 150px" value="" maxlength="40" >
</td>
<td class=tdsmall height=25>
<input name="phone" class=stedit
style="HEIGHT: 22px; WIDTH: 100px" value="" maxlength="20" >
</td>
<td class=tdsmall height=25>
<input name="address" class=stedit
style="HEIGHT: 22px; WIDTH: 200px" value="" maxlength="100" >
</td>
<td class=tdsmall height=25 width="25"><a href="javascript:if (checkform2()==false);"><img border=0
height=15 src="http://www.knowsky.com/images/editok.gif" width=15></a></td>
<td class=tdsmall height=25 width="25"> </td>
</form>
</tr>
<% } %>
</tbody>
</table>
<br>
<table align=center border=0 cellpadding=0 cellspacing=2 width="95%">
<tbody>
<tr valign=center>
<td align=middle> <br>
<table border=0 cellpadding=0 cellspacing=0>
<tr>
<td>
<% if (strOperation=="addnew"){
%>
<input class=stbtm name=update onClick="javascript:if (checkform2()==false);" type=button value=" 更新記錄 ">
<% }else{
if(strOperation=="modi"){
%>
<input class=stbtm name=update onClick="javascript:if (checkform()==false);" type=button value=" 更新記錄 ">
<%
}else{
%>
<input class=stbtm type="button" name="Button" value=" 新 增 " onClick="javascript:window.location.href=''<%=request.getRequestURI()%>?act=addnew'';"><%
}
} %>
</td>
<td>
<input class=stbtm type="button" name="Button" value=" 返 回 " onClick="javascript:window.location.href=''index.jsp'';">
</td>
</tr>
</table>
</td>
</tr>
</table>
<p> </p>
</div>
</body>
</html>
<script LANGUAGE=javascript>
<!--
function checkform2()
{
var Checkblank = /^(\s*|(\ )|(\.))*$/;
if (Checkblank.test(dataform2.name.value))
{
alert(" 餐館名稱(chēng)不能為空! ");
dataform2.name.focus();
return false;
}
if (Checkblank.test(dataform2.phone.value))
{
alert(" 餐館電話不能為空! ");
dataform2.phone.focus();
return false;
}
window.dataform2.submit();
}
function checkform()
{
var Checkblank = /^(\s*|(\ )|(\.))*$/;
if (Checkblank.test(dataform.name.value))
{
alert(" 餐館名稱(chēng)不能為空! ");
dataform.name.focus();
return false;
}
if (Checkblank.test(dataform.phone.value))
{
alert(" 餐館電話不能為空! ");
dataform.phone.focus();
return false;
}
window.dataform.submit();
}
-->
</script>
說(shuō)明:本文件的書(shū)寫(xiě)有很多地方并不簡(jiǎn)練,因?yàn)樵诔淌降拈_(kāi)發(fā)過(guò)程中,過(guò)分簡(jiǎn)練的程序往往會(huì)帶來(lái)后期維護(hù)的困難。
開(kāi)發(fā)心得:
doc.getElementsByTagName("restaurants").item(int i) 的返回值是 node 型,如果不是要調(diào)用它的屬性值,沒(méi)有必要強(qiáng)制轉(zhuǎn)型為 Element 型。可以直接操作。本系統(tǒng)因?yàn)殚_(kāi)發(fā)的參考資料的錯(cuò)誤,所以全都采用了強(qiáng)制轉(zhuǎn)型??梢栽谝院蟮拈_(kāi)發(fā)中考慮使用 node 直接進(jìn)行操作。
trim() 和 Interger.parseInt() 函數(shù)都不可以接受 null 型的數(shù)值
在 tomcat 下左右的文件都是目錄從 TOMCAT 算起,具體情況請(qǐng)參見(jiàn) \webapps\canyin\jsp\userjudge.jsp 里關(guān)于 xml 路徑的寫(xiě)法。