跳到內容

XQuery/鏈式網頁表單

來自 Wikibooks,開放的書,為開放的世界

您想建立一個系列的網頁,將資訊從一個頁面傳遞到下一個頁面。這在 web 應用程式開發中非常常見,例如在建立“嚮導”時,這些嚮導會在單獨的網頁上詢問使用者一系列問題。

我們將使用三種方法來演示這一點

  • 在客戶端使用 URL 引數和隱藏表單欄位
  • 在客戶端使用 Cookies
  • 在伺服器上使用會話

使用 URL 引數和隱藏表單欄位

[編輯 | 編輯原始碼]

在這種方法中,我們將在一系列連續頁面中使用 HTML 表單。每個頁面都會收集一些資訊,並透過向 URL 新增額外的引數,將這些資訊傳遞給下一個表單。我們將使用 request:get-parameter 函式從 URL 獲取鍵值對。

我們的第一個表單將詢問使用者他們的姓名。第二個將詢問他們最喜歡的顏色。

以下是一個第一個表單的示例

question-1.html

<html>
    <head>
        <title>Question 1: Your Name</title>
    </head>
    <body>
        <h1>Question 1</h1>
        <form action="question-2.xq">
            <span class="label">Please enter your first name:</span>
            <input type="text" name="name"/><br/>
            <input type="submit" value="Next Question"/>
        </form>
    </body>
</html>

URL 被傳遞給第二個表單,我們將使用 request:get-parameter() 函式從 URL 獲取值。

以下是 question 2 的 XQuery 函式:question-2.xq

xquery version "1.0";
declare option exist:serialize "method=xhtml media-type=text/html omit-xml-declaration=yes indent=yes";

let $name := request:get-parameter('name', '')
let $title := 'Question 2: Enter Your Favorite Color'

return
<html>
    <head>
        <title>{$title}</title>
    </head>
    <body>
        <h1>{$title}</h1>
        <form action="result.xq">
            <span class="label">Hello {$name}.  Please enter your favorite color:</span>
            <input type="hidden" name="name" value="{$name}"/>
            <input type="text" name="color"/><br/>
            <input type="submit" value="Results"/>
        </form>
    </body>
</html>

請注意,我們將傳入的姓名儲存在表單中的一個隱藏輸入欄位中。隱藏欄位的值必須取自傳入的 {$name} 引數。

最後一頁只獲取兩個輸入引數,並在 HTML 頁面中顯示它們。如果您檢視 URL,它將採用以下格式

  result.xq?name=dan&color=blue

result.xq

xquery version "1.0";
declare option exist:serialize "method=xhtml media-type=text/html omit-xml-declaration=yes indent=yes";

let $name := request:get-parameter('name', '')
let $color := request:get-parameter('color', '')
let $title := 'Result'

return
<html>
    <head>
        <title>{$title}</title>
    </head>
    <body>
        <h1>{$title}</h1>
        <p>Hello {$name}.  Your favorite color is {$color}</p>
    </body>
</html>

這種方法是首選方法,因為它不需要客戶端瀏覽器支援 Cookies。它也不需要使用者登入和管理會話。會話的缺點是,如果使用者在中途被打斷,他們的會話資訊將丟失,他們輸入的所有資料都需要重新輸入。

請注意,雖然第一個“姓名”引數在第二個表單中不可見,但該值在 URL 中可見。因此,“隱藏”一詞不適用於 URL,只適用於表單。

使用 Cookies

[編輯 | 編輯原始碼]

在本例中,我們將使用以下函式來設定和獲取 Cookies

 response:set-cookie($name as xs:string, $value as xs:string) empty()
 request:get-cookie-value($cookie-name as xs:string) xs:string?

第一個表單與上面的示例相同。但是,

xquery version "1.0";
declare option exist:serialize "method=xhtml media-type=text/html omit-xml-declaration=yes indent=yes";

(: get the input and set the name cookie :)
let $name := request:get-parameter('name', '')
let $set-cookie := response:set-cookie('name', $name)

let $title := 'Question 2: Enter Your Favorite Color'

return
<html>
    <head>
        <title>{$title}</title>
    </head>
    <body>
        <h1>{$title}</h1>
        <form action="result.xq">
            <span class="label">Hello {$name}.  Please enter your favorite color:</span>
            <input type="text" name="color"/><br/>
            <input type="submit" value="Results"/>
        </form>
    </body>
</html>

我們的第一個表單將設定第一個 Cookie 值,而第二個表單將讀取姓名 Cookie 的值。

xquery version "1.0";
declare option exist:serialize "method=xhtml media-type=text/html omit-xml-declaration=yes indent=yes";

let $name := request:get-cookie-value('name')
let $color := request:get-parameter('color', '')
let $title := 'Result From Cookies'

return
<html>
    <head>
        <title>{$title}</title>
    </head>
    <body>
        <h1>{$title}</h1>
        <p>Hello {$name}.  Your favorite color is {$color}</p>
    </body>
</html>

使用 Cookies 可能很複雜,您必須非常小心,不要讓來自同一域的其他應用程式更改您的 Cookies。您的設計還必須考慮瀏覽器和使用者停用 Cookies 的事實。

使用會話

[編輯 | 編輯原始碼]

最後一種方法是使用伺服器會話值來儲存鍵值資料。這將與最後一個示例非常相似,但我們將使用 eXist 會話 模組函式來設定和獲取值。

以下是我們需要呼叫的兩個函式

  session:set-attribute($name as xs:string, $value as item()*) empty()
  session:get-attribute($name as xs:string) xs:string*


您只需要更改第二種表單的一行。只需將以下幾行更改為:

  (: get the name and set the session :)
  let $name := request:get-parameter('name', )
  let $set-session := session:set-attribute('name', $name)

在最後的 result 指令碼中,只需從會話中獲取資料即可

  let $name := session:get-attribute('name')

如果您不熟悉會話管理,使用會話也可能很複雜。有很多規則管理會話超時,web 伺服器和資料庫伺服器可能都需要配置,以滿足使用者的需求。會話管理可能也不適合那些禁止在 web 伺服器上收集資訊的公共網站。

權衡分析

[編輯 | 編輯原始碼]

有很多方面需要考慮。在 URL 中儲存資訊有很多優勢,因為使用者可以啟動一個多步驟表單,然後稍後回來完成。只要他們沒有關閉瀏覽器,URL 引數就會保留。

Cookies 會保留在客戶端,直到使用者採取一些操作將其刪除。當您不想讓使用者在每次會話中重新輸入資料時,它們非常有用。當您沒有能力在伺服器上儲存使用者首選項時,Cookies 通常非常適合儲存使用者首選項。

當您讓使用者使用登入進行身份驗證,但資料在使用者登出或會話超時時丟失時,會話最為有用。

華夏公益教科書