beego數據輸出


概覽

直接輸出字符串

通過beego.Controller.Ctx.WriteString()方法可以直接向http response body中輸出字符串

beego中的函數定義如下:

// WriteString Write string to response body.
// it sends response body.
func (ctx *Context) WriteString(content string) {
    ctx.ResponseWriter.Write([]byte(content))
}

示例:直接在response body中輸出Hello World!

package controllers

import (
    "github.com/astaxie/beego"
)

type MainController struct {
    beego.Controller
}

func (c *MainController) Get() {
    c.Ctx.WriteString("Hello World!")
}

打開http跟蹤可以看到,在http response body中只有Hello World!,都沒有html標簽。

模板數據輸出

靜態模板數據輸出

通過簡單的指定beego.Controller.TplName模板文件,http response body將輸出模板文件對應的內容。

示例:

package controllers

import (
    "github.com/astaxie/beego"
)

type MainController struct {
    beego.Controller
}

func (c *MainController) Get() {
    c.TplName = "hello.tpl"
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
</head>
<body>
    <h1>Hello World!</h1>
</body>
</html>

動態模板數據輸出

在web中大部分的內容是靜態的,只有少部分數據是動態的。為了復用模板的代碼,需要能夠把動態的數據插入到模板中,這需要特出的語法。

beego中模板通過{{}}包含需要被替換的字段,同時需要把要替換的內容添加到Controller的Data中,這樣Controller執行時會自動匹配渲染模板。

示例:

package controllers

import (
    "github.com/astaxie/beego"
)

type MainController struct {
    beego.Controller
}

func (c *MainController) Get() {
    c.Data["Email"] = "arestrack@163.com"
    c.TplName = "hello.tpl"
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
</head>
<body>
    <h1>Hello World!</h1>
    Contact me:
    <a class="email" href="mailto:{{.Email}}">{{.Email}}</a>
</body>
</html>

json格式數據輸出

通過把要輸出的數據放到Data["json"]中,然后調用ServeJSON()進行渲染,就可以把數據進行JSON序列化輸出。

beego中ServeJSON()函數定義如下:

// ServeJSON sends a json response with encoding charset.
func (c *Controller) ServeJSON(encoding ...bool) {
    var (
        hasIndent   = true
        hasEncoding = false
    )
    if BConfig.RunMode == PROD {
        hasIndent = false
    }
    if len(encoding) > 0 && encoding[0] {
        hasEncoding = true
    }
    c.Ctx.Output.JSON(c.Data["json"], hasIndent, hasEncoding)
}

示例:

type JSONStruct struct {
    Code int
    Msg  string
}

func (c *MainController) Get() {
    mystruct := &JSONStruct{0, "hello"}
    c.Data["json"] = mystruct
    c.ServeJSON()
}

xml格式數據輸出

通過把要輸出的數據放到Data["xml"]中,然后調用ServeXML()進行渲染,就可以把數據進行XML序列化輸出。

beego中ServeXML()函數定義如下:

// ServeXML sends xml response.
func (c *Controller) ServeXML() {
    hasIndent := true
    if BConfig.RunMode == PROD {
        hasIndent = false
    }
    c.Ctx.Output.XML(c.Data["xml"], hasIndent)
}

示例:

type XMLStruct struct {
    Code int
    Msg  string
}

func (c *MainController) Get() {
    mystruct := &XMLStruct{0, "hello"}
    c.Data["xml"] = mystruct
    c.ServeXML()
}

jsonp調用

通過把要輸出的數據放到Data["jsonp"]中,然后調用ServeJSONP()進行渲染,會設置content-typeapplication/javascript,然后同時把數據進行JSON序列化,然后根據請求的callback參數設置jsonp輸出。

beego中ServeJSONP()函數定義如下:

// ServeJSONP sends a jsonp response.
func (c *Controller) ServeJSONP() {
    hasIndent := true
    if BConfig.RunMode == PROD {
        hasIndent = false
    }
    c.Ctx.Output.JSONP(c.Data["jsonp"], hasIndent)
}

示例:

type JSONStruct struct {
    Code int
    Msg  string
}

func (c *MainController) Get() {
    mystruct := &JSONStruct{0, "hello"}
    c.Data["jsonp"] = mystruct
    c.ServeJSONP()
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM