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()
}

 

beego数据输出的更多相关文章

  1. 把数据输出到Word (组件形式)

    上一篇的文章中我们介绍了在不使用第三方组件的方式,多种数据输出出到 word的方式,最后我们也提到了不使用组件的弊端,就是复杂的word我们要提前设置模板.编码不易控制.循环输出数据更是难以控制.接下 ...

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

    项目开发过程中,我们要把数据以各种各样的形式展现给客户.把数据以文档的形式展现给客户相信是一种比较头疼的问题,如果没有好的方法会 使得我的开发繁琐,而且满足不了客户的需求.接下来我会通过两种开发方式介 ...

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

    项目背景 项目中需要把表格重排显示 处理方法 思路主要是用历遍Json数组把json数据一个个append到5个表格里,还要给每个单元格绑定个单击弹出自定义对话框,表格分了单双行,第一行最后还要改ro ...

  4. 【matlab】将matlab中数据输出保存为txt或dat格式

    将matlab中数据输出保存为txt或dat格式 总结网上各大论坛,主要有三种方法. 第一种方法:save(最简单基本的) 具体的命令是:用save *.txt -ascii x x为变量 *.txt ...

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

    :FID= FOPEN(filename,permission) 用指定的方式打开文件 FID=+N(N是正整数):表示文件打开成功,文件代号是N. FID=-1            : 表示文件打 ...

  6. ffmpeg 从内存中读取数据(或将数据输出到内存)

    更新记录(2014.7.24): 1.为了使本文更通俗易懂,更新了部分内容,将例子改为从内存中打开. 2.增加了将数据输出到内存的方法. 从内存中读取数据 ffmpeg一般情况下支持打开一个本地文件, ...

  7. 使用MapReduce查询Hbase表指定列簇的全部数据输出到HDFS(一)

    package com.bank.service; import java.io.IOException; import org.apache.hadoop.conf.Configuration;im ...

  8. 《物联网框架ServerSuperIO教程》- 22.动态数据接口增加缓存,提高数据输出到OPCServer和(实时)数据库的效率

     22.1   概述及要解决的问题 设备驱动有DeviceDynamic接口,可以继承并增加新的实时数据属性,每次通讯完成后更新这些属性数据.原来是通过DeviceDynamic接口实体类反射的方式获 ...

  9. 《物联网框架ServerSuperIO教程》- 23.动态数据接口增加缓存,提高数据输出到OPCServer和(实时)数据库的效率

     22.1   概述及要解决的问题 设备驱动有DeviceDynamic接口,可以继承并增加新的实时数据属性,每次通讯完成后更新这些属性数据.原来是通过DeviceDynamic接口实体类反射的方式获 ...

随机推荐

  1. iptables打开22,80,8080,3306等端口

    systemctl stop firewalld systemctl mask firewalld Then, install the iptables-services package: yum i ...

  2. EOJ - 3631 Delivery Service 2018.8华师大月赛(树链剖分+贪心)

    链接:https://acm.ecnu.edu.cn/contest/103/problem/D/ 题意:给你一棵无向边连接的树,边的权值可以任意互换.有m次运输,每次的花费是点u到v路径上边的权值和 ...

  3. 【JavaScript】键盘控制小球

    参考: 1.Simple Canvas Game 2.javaScript 事件监听 <!DOCTYPE html> <html> <head> <meta ...

  4. 20165101刘天野 2018-2019-2《网络对抗技术》第1周 Kali的安装

    20165101刘天野 2018-2019-2<网络对抗技术>第1周 Kali的安装 一.实验要求 Kali下载 安装 网络 共享 软件源 二.实验步骤 1.下载 从Kali官网中下载相应 ...

  5. Java 四大作用域总结

    一.ServletContext 1.生命周期:当Web应用被加载进容器时创建代表整个web应用的ServletContext对象,当服务器关闭或Web应用被移除时,ServletContext对象跟 ...

  6. Boostnote:适合程序员的笔记软件【转】

    本文转载自:https://blog.csdn.net/u013553529/article/details/70306899 Boostnote:适合程序员的笔记软件 注意: Boostnote正在 ...

  7. Linux图形化界面下使用命令进行截图的方法

    以前在LINUX里面截图都是直接按print screen键或者 alt + print screen. 但是print screen是整个屏幕, alt + print screen是当前窗口. 想 ...

  8. linux新手学习之Arch Linux入门经验分享

    我一直是以Ubuntu与Fedora作为新手入门的系统,但是其实我真正想推荐的是Arch,经过前面的学习,或许你对Linux已经有了一个大致的了解,现在如果你想加速你的步伐,也许可以看看本文.如果要问 ...

  9. 让Tomcat供外网访问

    使用Tomcat+花生壳部署一个Javaweb网站,步骤分为三步. 第一,花生壳配置. 下载花生壳,并默认安装.申请动态域名,激活护照,登录.(都是常规操作,不必多说) 第二,Tomcat设置. 找到 ...

  10. fabric生产环境代码包发布管理