Your message was: #{msg} ^{pageBody pc} |] instance RenderMessage App FormMessage where renderMessage _ _ = defaultFormMessage getHomeR :: Handler Html getHomeR = defaultLayout [whamlet|
Set your name Say hello |] -- Display the set name form getSetNameR :: Handler Html getSetNameR = defaultLayout [whamlet| My name is # . # |] -- Retrieve the submitted name from the user postSetNameR :: Handler () postSetNameR = do -- Get the submitted name and set it in the session name <- runInputPost $ ireq textField "name" setSession "name" name -- After we get a name, redirect to the ultimate destination. -- If no destination is set, default to the homepage redirectUltDest HomeR getSayHelloR :: Handler Html getSayHelloR = do -- Lookup the name value set in the session mname <- lookupSession "name" case mname of Nothing -> do -- No name in the session, set the current page as -- the ultimate destination and redirect to the -- SetName page setUltDestCurrent setMessage "Please tell me your name" redirect SetNameR Just name -> defaultLayout [whamlet|Welcome #{name}|] main :: IO () main = warp 3000 App ``` ## 结语 Session是我们绕过HTTP强加的无状态的主要手段。我们不应该认为这是一个逃避方式来执行我们想要的任何行动:Web应用程序中的无状态是一种好事,我们应该尽可能地尊重它。但是,在某些特定情况下,保留某些状态至关重要。 Yesod中的会话API非常简单。它提供了一个键值对存储,并为常见用例构建了一些便利功能。如果使用得当,具有较小的有效负载,会话应该是Web开发中不显眼的部分。
Say hello |] -- Display the set name form getSetNameR :: Handler Html getSetNameR = defaultLayout [whamlet| My name is # . # |] -- Retrieve the submitted name from the user postSetNameR :: Handler () postSetNameR = do -- Get the submitted name and set it in the session name <- runInputPost $ ireq textField "name" setSession "name" name -- After we get a name, redirect to the ultimate destination. -- If no destination is set, default to the homepage redirectUltDest HomeR getSayHelloR :: Handler Html getSayHelloR = do -- Lookup the name value set in the session mname <- lookupSession "name" case mname of Nothing -> do -- No name in the session, set the current page as -- the ultimate destination and redirect to the -- SetName page setUltDestCurrent setMessage "Please tell me your name" redirect SetNameR Just name -> defaultLayout [whamlet|Welcome #{name}|] main :: IO () main = warp 3000 App ``` ## 结语 Session是我们绕过HTTP强加的无状态的主要手段。我们不应该认为这是一个逃避方式来执行我们想要的任何行动:Web应用程序中的无状态是一种好事,我们应该尽可能地尊重它。但是,在某些特定情况下,保留某些状态至关重要。 Yesod中的会话API非常简单。它提供了一个键值对存储,并为常见用例构建了一些便利功能。如果使用得当,具有较小的有效负载,会话应该是Web开发中不显眼的部分。
Welcome #{name}|] main :: IO () main = warp 3000 App ``` ## 结语 Session是我们绕过HTTP强加的无状态的主要手段。我们不应该认为这是一个逃避方式来执行我们想要的任何行动:Web应用程序中的无状态是一种好事,我们应该尽可能地尊重它。但是,在某些特定情况下,保留某些状态至关重要。 Yesod中的会话API非常简单。它提供了一个键值对存储,并为常见用例构建了一些便利功能。如果使用得当,具有较小的有效负载,会话应该是Web开发中不显眼的部分。