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"] = "[email protected]"
    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()
}

原文地址:https://www.cnblogs.com/dfsxh/p/10274321.html

时间: 2024-07-31 10:49:38

beego数据输出的相关文章

通过g++查询到的mysql中文数据输出是乱码

这几天遇到一个问题: 通过g++查询到的mysql中文数据输出是乱码,而通过g++添加到mysql中的中文数据也是乱码,但单独通过g++输出正常,查询通过mysql添加的中文数据显示也正常. 在网查查询了很久,纠结了半天也没有找到答案.后来通过在linux吧发帖终于找到了解决方案.原来是mysql和g++默认字符集不匹配产生的问题.通过在代码中添加 int mysql_set_character_set(MYSQL *mysql, char *csname)函数终于解决了问题. 源码如下: #i

数据库数据输出到excel

1 SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=ArchiveSys;Integrated Security=True"); 2 //SQLHelper sqlhelper = new SQLHelper(); 3 string sql = "select * from StuFeedback"; 4 using (SqlCommand cmd = new SqlComm

把数据输出到Word (非插件形式)

项目开发过程中,我们要把数据以各种各样的形式展现给客户.把数据以文档的形式展现给客户相信是一种比较头疼的问题,如果没有好的方法会 使得我的开发繁琐,而且满足不了客户的需求.接下来我会通过两种开发方式介绍如何将数据输出到Word 文档上.我会分两篇文章介绍,第一篇 介绍不使用插件的情况下操作word,第二篇文章将介绍一种强大的插件操作word.下面开始第一篇文章.[本次实例源代码从这里下载] 文章梗概: ? 不使用模板将数据输出到 word ? 输出数据到 word 在后端设置输出内容 ? 输出数

Spark Structured Streaming框架(3)之数据输出源详解

Spark Structured streaming API支持的输出源有:Console.Memory.File和Foreach.其中Console在前两篇博文中已有详述,而Memory使用非常简单.本文着重介绍File和Foreach两种方式,并介绍如何在源码基本扩展新的输出方式. 1. File Structured Streaming支持将数据以File形式保存起来,其中支持的文件格式有四种:json.text.csv和parquet.其使用方式也非常简单只需设置checkpointLo

将matlab中数据输出保存为txt或dat格式

总结网上各大论坛,主要有三种方法. 第一种方法:save(最简单基本的) 具体的命令是:用save *.txt -ascii x x为变量*.txt为文件名,该文件存储于当前工作目录下,再打开就可以 打开后,数据有可能是以指数形式保存的. 例子: a =[17 24 1 8 15;23 5 7 14 16 ;4 6 13 20 22 ;10 12 19 21 3 ;11 18 25 2 9 ]:save afile.txt -ascii a afile.txt打开之后,是这样的:1.700000

tp5数据输出

法一:系统配置 'default_return_type'=>'json' 法二:输出设置 namespace app\index\controller; class Index { public function index() { $data = ['name'=>'thinkphp','url'=>'thinkphp.cn']; // 指定json数据输出 return json(['data'=>$data,'code'=>1,'message'=>'操作完成'

jquery: json树组数据输出到表格Dom树的处理方法

项目背景 项目中需要把表格重排显示 处理方法 思路主要是用历遍Json数组把json数据一个个append到4个表格里,还要给每个单元格绑定个单击弹出自定义对话框,表格分了单双行,第一行最后还要改rowspan呵呵,程序还没优化运行正常先给客户展示先:) 1,表格数据->json数组 var keyArr = new Array(); var jsonArr = new Array(); $list.find("thead th").each(function () { keyA

把存储过程获取的数据输出到报表的html模板中

制作报表的html模板 <HTML><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><TITLE>报表模板</TITLE> <BODY> <TABLE BORDER="1" cellpadding="0" cellspacing="0"> <

django--问题描述:后端数据输出正常,但前端不显示数据

Django爬坑记录 背景:普通的django项目,一个新闻类的网站,前后端半分离. 问题描述:后端数据输出正常,但前端不显示数据 前端数据不显示 后端传数据正常 前端js也正常 问题解决:前端res响应未发现 原文地址:https://www.cnblogs.com/xjl-dwy/p/10769678.html