我是新手去体验beego。
我试图从以下网址获取发布的表单数据:
<form action="/hello" method="post">
{{.xsrfdata}}
Title:<input name="title" type="text" /><br>
Body:<input name="body" type="text" /><br>
<input type="submit" value="submit" />
</form>
致控制器:
type HelloController struct {
beego.Controller
}
type Note struct {
Id int `form:"-"`
Title string `form:"title"`
Body string `form:"body"`
}
func (this *HelloController) Get() {
this.Data["xsrfdata"]= template.HTML(this.XSRFFormHTML())
this.TplName = "hello.tpl"
}
func (this *HelloController) Post() {
n := &Note{}
if err := this.ParseForm(&n); err != nil {
s := err.Error()
log.Printf("type: %T; value: %q\n", s, s)
}
log.Printf("Your note title is %s" , &n.Title)
log.Printf("Your note body is %s" , &n.Body)
this.Ctx.Redirect(302, "/")
}
但是,不是输入字段的字符串值,我得到:
Your note title is %!s(*string=0xc82027a368)
Your note body is %!s(*string=0xc82027a378)
我在请求处理时跟踪了the docs,但是为什么不能发布字符串留下无能为力。
答案 0 :(得分:1)
从文档中,定义接收器结构的方式应该是使用结构类型,而不是指向该结构的指针:
func (this *MainController) Post() {
u := User{}
if err := this.ParseForm(&u); err != nil {
//handle error
}
}
然后,在你的控制器中,如果你......那就更好了。
func (this *HelloController) Post() {
n := Note{}
...
}
有关指针的更多信息,请访问:https://tour.golang.org/moretypes/1