DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> XML學習教程 >> XML詳解 >> 使用JSP + JAVABEAN + XML 開發的一個例子
使用JSP + JAVABEAN + XML 開發的一個例子
編輯:XML詳解     
是參考了一些網站上有關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  

代碼:
<?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>說明:字段含義是用戶名,密碼以及用戶的身份/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>百老匯廣場B座</address> </restaurant>- <restaurant id="8"><name> 香格裡拉大飯店</name> <phone>021- 2312134</phone> <address>南京路1023號</address> </restaurant></restaurants>說明:<num>屬性是記錄在restaurants.XML 文件中總共有過多少條記錄,每新增一 條,無論以後刪除是否,該值都會增加1,就好象數據庫中習慣使用的自動增加1的id 項。用來給 新增的 <restaurant>的屬性<id>賦一個唯一的值。其它的字段意思比較明顯。

/tomcat/webaPPS/ROOT/index.html    (單純的Html代碼)

代碼:
<Html>
<head>
<title>oddWorld 餐飲系統 </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="images/top_r1.GIF" width="62" height="22"></td>
   <td width=150 align="center"> 餐飲系統 登錄 </td>
   <td><img src="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">餐飲系統登錄</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>&nbsp;</td>
         <td>
           <input class=stbtm name=Submit onClick="Javascript:window.location.href='index.ASP?myjoke=1';" type=button value="修 改密碼">
         </td>
         <td>&nbsp;</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>

說明:把用戶名稱和用戶密碼提交到/tomcat/webaPPS/canyin/JSp/loginjudge.JSP    

/tomcat/webaPPS/canyin/WEB-INF/classes/canyin/checkSessionBean.class (代碼 是相應的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);
 }
}
}

說明:檢驗參數傳入的session 名稱的數值和參數傳入的字段的數值是否相等。

/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);
}
}

說明:打開一個指定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);
}

}

說明:寫入dom 的內容到一個指定的XML 文件。

/tomcat/webaPPS/canyin/JSp/loginjudge.JSP   

代碼:
<%-- oddWorld 餐飲管理系統(簡體中文版) 2002年12月1日
copy right by joard ast

loginjudge.JSP 功能:用戶 身份校驗,根據 /data/user.XML 文件內標示的用戶不同的身份
決定轉入後台管理頁 面,還是客戶點菜頁面。
--%>

<%@ 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");

//校驗數據是否為空
if (strUsername=="" || strPassWord=="" ){
out.println("<script language='Javascript'>");
out.println("alert('用戶名或密碼有空 值!');");
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('歡迎管理員登陸系統!');");
  out.println("</script>");
  
  //設置標 示用戶身份的 session(sesUserRole) ,管理員身份為 admin
  session.setAttribute ("sesUserRole","admin");
  
  //跳轉到管理頁面
  response.sendRedirect("admin/admin_rest.JSP");
  return;
  
     }else{
     //設 置標示用戶身份的 session(sesUserRole) ,管理員身份為 user
      session.setAttribute("sesUserRole","user");
     
      //跳轉到普通用戶頁面
      response.sendRedirect("index.JSP");   
     return;
     }

    }else{
     out.println ("<script language='Javascript'>");
  out.println("alert('用 戶名或密碼錯誤!');");
  out.println("history.go(- 1);");
  out.println ("</script>");
  return;
    }

}
}catch(Exception e){
  strExc=e.toString ();
}
%>
說明:.......

/tomcat/webaPPS/canyin/JSp/admin/admin_rest.JSP 

代碼:
<%-- oddWorld 餐飲管理系統(簡體中文版) 2002年12月1日
copy right by joard ast

admin_rest.JSP 功能:後台 管理頁面,餐館管理頁面。
--%>

<%@ 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" />

<%//校驗可戶身份,判斷是不是管理員
if(!checkSessionBean.checkSessionBean (request,"sesUserRole","admin")){
out.print(showDialog("您沒有管理的權 限!","/index.Html"));
return;
}

//從餐館資料文件 rest.XML 中得到相關數據
Document doc;
NodeList restaurants;

String strAct;
int intId=0;
String strOperation="show";

//接受外部傳入的參數
strAct=(String) request.getParameter("act");

xmlBean.connXml("webaPPS/canyin/data/restaurants.XML");
doc=xmlBean.getXMLDoc ();
restaurants =doc.getElementsByTagName("restaurant");

//根據外部傳入的參數來決定對 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 ();
 
 //數據校驗
 if(strName==null){
  out.print(showDialog("餐館名 稱不能為空!"));
  return;
 }
 if (strPhone==null){
  out.print(showDialog("餐館電話不能為 空!"));
  return;
 }
 /*if (strAddress==null){
  out.print(showDialog("餐館地址不能為 空!"));
  return;
 } */
 
 //校驗數據的唯一性
 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("餐 館名稱重復!"));
  return;
  }else{
  if(((String) restaurant.getElementsByTagName("name").item(0).getFirstChild().getNodeValue ()).equals(strPhone)){
   out.print(showDialog("餐館電話重復!"));
   return;
  }
  }
 
 }
 
 
  
 //得 到已有的記錄數,給新增的餐館記錄設定唯一的遞增的id 屬性
 int intNum=0;
 Element restNum=(Element)doc.getElementsByTagName ("restaurants").item(0);
 intNum=Integer.parseInt (restNum.getAttributeNode("num").getNodeValue());

 intNum+=1;
 
 //為 restaurants的屬性num 的數值加1
 restNum.getAttributeNode ("num").setNodeValue(String.valueOf(intNum));

 //新增節點  
 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);

 //調用bean 寫入相應的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)的哪一項
 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 ());
 
 //數據校驗
 if(strName==null){
  out.print(showDialog("餐館名 稱不能為空!"));
  return;
 }
 if (strPhone==null){
  out.print(showDialog("餐館電話不能為 空!"));
  return;
 }
 if(modiId==0) {
  out.print(showDialog("你要修改餐館的記錄不存 在!"));
  return;
 }
 /*if (strAddress==null){
  out.print(showDialog("餐館地址不能為 空!"));
  return;
 } */
 
 //標志顯示記錄存在
 boolean recordExist=false;
 
 //校驗數據的唯一性
 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("餐館名稱重復!"));
  return;
  }else{
  if(((String) restaurant.getElementsByTagName("name").item(0).getFirstChild().getNodeValue ()).equals(strPhone) && Integer.parseInt(restaurant.getAttributeNode ("id").getNodeValue())!=modiId ){
   out.print(showDialog("餐館電話重 復!"));
   return;
  }
  }
 
 }
 

 
 if(!recordExist){
  out.print (showDialog("你要修改餐館的記錄不存 在!"));
  return;
 }else{
  //進行記錄更改的操作
  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);
  
  //調用bean 寫入相應的XML文件
  writeXmlBean.writeXml (doc,"webaPPS/canyin/data/restaurants.XML");
 
   response.sendRedirect(request.getRequestURI());
  return;
  
  }catch(Exception e){ }
 }
}
//進行刪除操作
if(strAct.equals("del")) {
 int delId;
 //記錄要修改的記 錄是item(i)的哪一項
 int intI=0;

 delId=Integer.parseInt(request.getParameter("recordId").trim());

 if(delId==0){
  out.print(showDialog("你要修改餐館 的記錄不存在!"));
  return;
 }
 
 file://標志顯示記錄存在
 boolean recordExist=false;

 //校驗數據的唯一性
 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("你要刪除餐館的記錄不存 在!"));
  return;
 }else{
  //進行記錄刪除的操作
  try{
  Node delNode=(Node)restaurants.item(intI);
  
  doc.getElementsByTagName ("restaurants").item(0).removeChild(delNode);

  //調用bean 寫入相應的XML文件
   writeXmlBean.writeXml(doc,"webaPPS/canyin/data/restaurants.XML");

  response.sendRedirect(request.getRequestURI());
   return;
  
  }catch(Exception e){ }
 }

}
}

//由外部傳入參數決定頁面相應的處理狀態
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";
 }
}
}

//如果為空記錄,則變更頁面狀態為“新增”
if (restaurants.getLength()==0){
strOperation="addnew";
}
%>

<Html>
<head>
<title>oddWorld 餐飲系統 </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="../../images/top_r1.GIF" width="62" height="22"></td>
   <td width=150 align="center"> 餐飲系統管理--餐館管理</td>
   <td><img src="../../images/top_r2.GIF" width="294" height="22"></td>
  <td width=100 align="center"><a href="/index.Html">[ 退出系統 ] </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">&nbsp;</td>
   <td class=ttTable height=30 width="0">餐館名稱</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="../../images/editok.gif" width=15></a></td>
    <td class=tdsmall height=25 width="25">&nbsp;</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="../../images/edit.gif" width=15></a></td>
   <td class=tdsmall height=25 width="30"><img border=0
    height=15
    onClick="Javascript:if(confirm('您是 否確定刪除本記錄,刪除後將導至記錄無法使用?')){window.location.href='<% =request.getRequestURI()%>?act=del&recordId=<%=restaurant.getAttributeNode ("id").getNodeValue()%>';}"
    src="../../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="../../images/editok.gif" width=15></a></td>
     <td class=tdsmall height=25 width="25">&nbsp;</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';"><%
        }
       } %>
       &nbsp;</td>
       <td>
       <input class=stbtm type="button" name="Button" value="返 回" onClick="Javascript:window.location.href='index.JSP';">
       </td>
     </tr>
     </table>
   </td>
  </tr>
</table>
<p>&nbsp;</p>
</div>
</body>
</Html>
&l t;SCRIPT LANGUAGE=Javascript>
<!--
function checkform2()
{
var Checkblank = /^(\s*|(\ )|(\.))*$/;
 
 if (Checkblank.test(dataform2.name.value))
{
     alert("餐館名稱 不能為空!");
     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("餐館名稱不能為空!");
     dataform.name.focus();
  return false;
     }
    
     if (Checkblank.test(dataform.phone.value))
{
     alert("餐館電話不能為空!");
     dataform.phone.focus ();
  return false;
     }
   

     window.dataform.submit();
}
-- >
</SCRIPT>
說明:本文件的書寫有很多地方並不簡練,因為在程式的開發 過程中,過分簡練的程序往往會帶來後期維護的困難。

開發心得:

doc.getElementsByTagName("restaurants").item(int i)的返回值是node 型,如果不是要調 用它的屬性值,沒有必要強制轉型為 Element型。可以直接操作。本系統因為開發的參考資料的 錯誤,所以全都采用了強制轉型。可以在以後的開發中考慮使用node 直接進行操作。

trim() 和 Interger.parseInt() 函數都不可以接受null 型的數值

在tomcat 下左右 的文件都是目錄從TOMCAT 算起,具體情況請參見\webaPPS\canyin\JSp\userjudge.JSP 裡關於 XML 路徑的寫法。

對原代碼感興趣的朋友請通過如下信箱和我聯系,[email protected]

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved