ITPub博客

首页 > IT职业 > IT图书 > Beego框架之请求数据处理

Beego框架之请求数据处理

IT图书 作者:limiao 时间:2021-09-09 10:16:58 0 删除 编辑


我们经常需要获取用户传递的数据,包括 Get、POST 等方式的请求,beego 里面会自动解析这些数据,你可以通过如下方式获取数据:

Beego框架之请求数据处理

通过this.Getstring("获取用户输入")获取用户输入

再通过this.Ctx.WriteString("输出用户输入的内容")输出用户输入的内容

Beego框架之请求数据处理

通过Input.Get("")获取用户输入

Beego框架之请求数据处理

package controllers

import (

    "github.com/astaxie/beego"

)

type TestOnceController struct {

    beego.Controller

}

func (c *TestOnceController) Get()  {

    id := c.GetString("id")

    c.Ctx.WriteString("<html>" + id + "<br/>")

    name := c.Input().Get("name")

    c.Ctx.WriteString(name + "</html>")

}

执行结果

Beego框架之请求数据处理

直接解析到struct

Beego框架之请求数据处理

controller testInputController

router beego.Router("/test_input", &controllers.TestInputController{}, "get:Get;post:Post")

package controllers

import (

    "github.com/astaxie/beego"

)

type TestInputController struct {

    beego.Controller

}

type User struct{

    Username string

    Password string

}

func (c *TestInputController) Get(){

    //id := c.GetString("id")

    //c.Ctx.WriteString("<html>" + id + "<br/>")

    //name := c.Input().Get("name")

    //c.Ctx.WriteString(name + "</html>")

    c.Ctx.WriteString(`<html><form action="http://127.0.0.1:8080/test_input" method="post"> 

                            <input type="text" name="Username"/>

                            <input type="password" name="Password"/>

                            <input type="submit" value="提交"/>

                         </form></html>`)

}

func (c *TestInputController) Post(){

    u := User{}

    if err:=c.ParseForm(&u) ; err != nil{

        //process error

    }

    c.Ctx.WriteString("Username:" + u.Username + " Password:" + u.Password)

}

©著作权归作者所有:来自51CTO博客作者PowerMichael的原创作品,如需转载,请注明出处,否则将追究法律责任


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4549/viewspace-2819541/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 欧拉函数
请登录后发表评论 登录
全部评论

注册时间:2019-07-10

  • 博文量
    190
  • 访问量
    1371