<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                控制器方法必須返回一個[`revel.Result`](http://gorevel.cn/docs/docs/godoc/results.html#Result), 用來處理響應結果,其接口定義如下: ~~~ type Result interface { Apply(req *Request, resp *Response) } ~~~ [`revel.Controller`](http://gorevel.cn/docs/docs/godoc/controller.html#Controller)?使用以下方法來處理響應結果: * Render, RenderTemplate - 渲染模板, 傳送參數. * RenderJson, RenderXml - 序列化結構體到 json 或 xml. * RenderText - 返回一個明文響應. * Redirect - 重定向到一個控制器方法 或 URL * RenderFile - 返回一個文件, 一般用于響應文件下載. * RenderError - 渲染 errors/500.html 模板來返回一個 500 錯誤. * NotFound - 渲染 errors/404.html 模板來返回一個 404 錯誤. * Todo - 返回一個存根響應(500) 此外,開發者可以自定義revel.Result. ### 設置狀態碼/內容類型 每個內建的 Result 都有一個默認的狀態碼和內容類型,簡單的設置相關屬性就可以修改這些默認值: ~~~ func (c App) Action() revel.Result { c.Response.Status = http.StatusTeapot c.Response.ContentType = "application/dishware" return c.Render() } ~~~ ## 渲染 控制器渲染方法 (e.g. “Controller.Action”),?[`mvc.Controller.Render`](http://gorevel.cn/docs/docs/godoc/controller.html#Controller.Render)?做了兩件事: 1. 添加參數到控制器的 RenderArgs 中, 使用變量的名字作為字典的key. 2. 使用傳送的參數渲染模板 “views/Controller/Action.html”。 如果不成功 (比如,沒有找到模板), 將返回 ErrorResult. 這允許開發人員編寫: ~~~ func (c MyApp) Action() revel.Result { myValue := calculateValue() return c.Render(myValue) } ~~~ 在模板中引用變量 “myValue”。這通常比構建一個明確的map更方便,因為在許多情況下,數據將被作為局部變量處理。 **注意:**?Revel 通過調用的控制器方法名來確定使用哪個模板,查找參數名。因此, c.Render() 只可稱為由操作。 ## RenderJson / RenderXml 應用程序可以調用?[`RenderJson`](http://gorevel.cn/docs/docs/godoc/controller.html#Controller.RenderJson)?或?[`RenderXml`](http://gorevel.cn/docs/docs/godoc/controller.html#Controller.RenderXml)?并傳送任意類型的變量 (通常是一個 struct). Revel 會使用?[`json.Marshal`](http://www.golang.org/pkg/encoding/json/#Marshal)?or[`xml.Marshal`](http://www.golang.org/pkg/encoding/xml/#Marshal)對變量進行序列化操作. 如果?`app.conf`?配置文件中?`results.pretty=true`, 將使用?`MarshalIndent`?進行序列化, 生成漂亮的縮進,方便使用。 ## 重定向 一個輔助函數是用于生成重定向。它有兩種方式使用: 1. 重定向到一個控制器方法(不帶參數): ~~~ return c.Redirect(Hotels.Settings) ~~~ 這種形式是非常有用的,因為它提供了一定程度的路由類型安全性和獨立性(自動生成URL)。 1. 重定向到一個格式化字符串: ~~~ return c.Redirect("/hotels/%d/settings", hotelId) ~~~ 通常用來傳送參數. 它返回 302 (臨時重定向) 狀態碼. ## 添加你自己的 Result 下面是一個添加簡單結果的例子。 創建此類型: ~~~ type Html string func (r Html) Apply(req *Request, resp *Response) { resp.WriteHeader(http.StatusOK, "text/html") resp.Out.Write([]byte(r)) } ~~~ 在一個控制器方法中使用它: ~~~ func (c *App) Action() revel.Result { return Html("<html><body>Hello World</body></html>") } ~~~ ## 狀態碼 每一個Result 都會設置一個默認的狀態碼,你也可以重新設置默認的狀態代碼: ~~~ func (c *App) CreateEntity() revel.Result { c.Response.Status = 201 return c.Render() } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看