使用純HTML的通用數據管理和服務。然而,為了收集數據,你需要一個數據儲存庫。要避免使用數據庫服務器帶來的很多問題,你可以在XML中收集這些數據。下面是我們的項目的基本結構:
<user>
<first_name/>
<last_name/>
<mi/>
</user>
我最初將數據限制為first name,last name和middle。這個頁面之后的基本思想是用戶信息在這個頁面中獲得。在用戶信息需求得到滿足以后,流程必須被轉到下一個邏輯收集步驟。為了使事情變得簡單,我將把用戶功能包裝到一個ASP類中。
Function Coalesce(vVar, vAlt)
If vVal = "" Or VarType(vVal) = 1 Or VarType(vVal) = 0 Then
Coalesce = vAlt
Else
Coalesce = vVal
End If
End Function
Class CUser
Private m_SQL, m_DOM
Public Property Get DOM()
Set DOM = m_DOM
End Property
Public Sub saveUser()
m_SQL.save "save_user", m_DOM
End Sub
Public Function validate()
m_DOM.loadXML "<root>" & m_SQL.validateUser(m_DOM) & "</root>"
If Not m_DOM.selectSingleNode("//error") Is Nothing Then
validate = False
Else
validate = True
End If
End Function
Private Sub collectData(dom, oCollection)
Dim nItem, node, parent_node, n, sKey
For nItem = 1 To oCollection.Count
sKey = oCollection.Key(nItem)
Set parent_node = dom.selectSingleNode("//" & sKey & "s")
If Not parent_node Is Nothing Then
For n = 1 To oCollection(sKey).Count
Set node = parent_node.selectSingleNode(sKey & _
"[string(.)='" &
oCollection(sKey)(n) & "']")
If node Is Nothing Then
Set node = dom.createNode(1, sKey, "")
Set node = parent_node.appendChild(node)
End If
node.text = Coalesce(oCollection(sKey)(n), "")
Next
Else
Set node = dom.selectSingleNode("//" & sKey)
If Not node Is Nothing Then _
node.text = Coalesce(oCollection(sKey), "")
End If
Next
End Sub
Private Sub Class_Initialize()
Set m_SQL = New CSQL
Set m_DOM = Server.CreateObject("MSXML2.DOMDocument")
m_DOM.async = False
If VarType(Request ("txtUserXML")) = 0 Or Request ("txtUserXML") = "" Then
m_DOM.loadXML Request("txtUserXML")
Else
m_DOM.load "<root>" & Server.MapPath("user.xml") & "</root>"
End If
collectData m_DOM, Request.Form
collectData m_DOM, Request.QueryString
End Sub
Private Sub Class_Terminate()
Set m_SQL = Nothing
Set m_DOM = Nothing
End Sub
End Class
Class CSQL
Private m_DAL, m_Stream
Public Function save(sStoredProc, oDOM)
'adVarChar = 200
m_DAL.RunSP Array(m_DAL.mp("@xml_param", 200, 8000, oDOM.xml))
End Function
Public Function validateUser(oDOM)
Set m_Stream = m_DAL.RunSPReturnStream("validate_user", Array(_
m_DAL.mp("@xml_param", 200, 8000, oDOM.xml)))
validateUser = m_Stream.ReadText(-1)
m_Stream.Close
End Function
Private Sub Class_Initialize()
Set m_DAL = Server.CreateObject("MyPkg.MyDAL")
m_DAL.GetConnection "some connection string"
Set m_Stream = Server.CreateObject("ADODB.Stream")
End Sub
Private Sub Class_Terminate()
Set m_DAL = Nothing
Set m_Stream = Nothing
End Sub
End Class
CSQL類是基于一個數據訪問層(m_DAL)組件MyPkg.MyDAL建立起來的。而這個組件則是基于Fitch和Mather DAL組件建立起來的,這兩個組件可以從MSDN找到。這樣我們就在SQL Server與你的代碼建立了橋梁。
當CUser對象初始化之后,它收集Request數據并使用collectData()子函數將收集到的數據放到UserDOM的一個相應的節點中。(代碼我不再解釋,因為它本身相當容易理解。)在收集了數據之后(或者不收集數據),我們將使用XSL將數據內容轉變成布局。
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl=http://www.w3.org/1999/XSL/Transform
version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<xsl:if test="//error">
<font color="red">*Information in red is required<br/></font>
</xsl:if>
<xsl:apply-templates select="//user"/>
</xsl:template>
<xsl:template match="user">
<font>
<xsl:attribute name="color">
<xsl:choose>
<xsl:when test="//error[.='first name']">red</xsl:when>
<xsl:otherwise>black</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
First Name:
</font>
<input type="text" name="first_name">
<xsl:attribute name="value"><xsl:value-of
select="first_name"/></xsl:attribute>
</input><br/>
<font>
<xsl:attribute name="color">
<xsl:choose>
<xsl:when test="//error[.='mi']">red</xsl:when>
<xsl:otherwise>black</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
MI:
</font>
<input type="text" name="mi">
<xsl:attribute name="value"><xsl:value-of select="mi"/></xsl:attribute>
</input><br/>
<font>
<xsl:attribute name="color">
<xsl:choose>
<xsl:when test="//error[.='last_name']">red</xsl:when>
<xsl:otherwise>black</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
Last Name:
</font>
<input type="text" name="last_name">
<xsl:attribute name="value"><xsl:value-of
select="last_name"/></xsl:attribute>
</input><br/>
ver
新聞熱點
疑難解答