Beego框架之请求数据处理

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

Beego框架之请求数据处理

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

Beego框架之请求数据处理

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

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

猜你喜欢

转载自blog.51cto.com/huwho/2322340
今日推荐