马提尼绑定似乎无法正常工作。

huangapple go评论70阅读模式
英文:

Martini binding doesn't appear to be working

问题

我正在玩Martini,但是出现了一些问题,无法让contrib binding包正常工作。

我的结构体没有绑定值。我已经将代码简化到最简形式,但仍然无法工作。

有人能看出我做错了什么吗?

package main

import (
	"github.com/go-martini/martini"
	"github.com/martini-contrib/binding"
	"net/http"
)

var html string = `<form method="POST" enctype="application/x-www-form-urlencoded"><input name="un" type="text" /><input type="submit" value="Some button" /></form>`

type FormViewModel struct {
	Username string `form:"un"`
}

func main() {
	m := martini.Classic()

	m.Get("/", func(w http.ResponseWriter) {
		w.Header().Add("content-type", "text/html")
		w.Write([]byte(html))
	})

	m.Post("/", binding.Form(FormViewModel{}), func(vm FormViewModel) string {
		return "You entered: " + vm.Username
	})

	m.Run()
}
英文:

I'm playing around with Martini, and for some reason I can't get the contrib binding package to work.

My struct isn't having the values bound to. I've reduced the code down to it's simplest form, but it still doesn't work.

Can anyone see what I'm doing wrong?

package main

import (
	&quot;github.com/go-martini/martini&quot;
	&quot;github.com/martini-contrib/binding&quot;
	&quot;net/http&quot;
)

var html string = `&lt;form method=&quot;POST&quot; enctype=&quot;application/x-www-form-urlencoded&quot;&gt;&lt;input name=&quot;un&quot; type=&quot;text&quot; /&gt;&lt;input type=&quot;submit&quot; value=&quot;Some button&quot; /&gt;&lt;/form&gt;`

type FormViewModel struct {
	Username string `form: &quot;un&quot;`
}

func main() {
	m := martini.Classic()

	m.Get(&quot;/&quot;, func(w http.ResponseWriter) {
		w.Header().Add(&quot;content-type&quot;, &quot;text/html&quot;)
		w.Write([]byte(html))
	})

	m.Post(&quot;/&quot;, binding.Form(FormViewModel{}), func(vm FormViewModel) string {
		return &quot;You entered: &quot; + vm.Username
	})

	m.Run()
}

答案1

得分: 1

这只是一个在与结构体字段相关的标签定义中的解析问题。

你需要在 form: 后面删除空格字符。

如果你将结构体定义如下:

type FormViewModel struct {
    Username string `form:"un"`   // 在 form: 后面没有空格
}

...应该会工作得更好。

Go语言规范中说:

按照惯例,标签字符串是可选的以空格分隔的 key:"value" 对的串联。每个 key 是一个非空字符串,由非控制字符组成,除了空格(U+0020 ' ')、引号(U+0022 '"')和冒号(U+003A ':')。每个 value 使用 U+0022 '"' 字符和 Go 字符串字面值语法进行引用。

显然,在reflect包中实现的解析器不允许在冒号后面有空格。

英文:

It is just a parsing issue in the definition of the tag associated to the field of the structure.

You need to remove the blank character after form:

If you write the structure as follows:

type FormViewModel struct {
    Username string `form:&quot;un&quot;`   // No blank after form:
}

... it should work better.

The Go language specification says:

By convention, tag strings are a concatenation of optionally space-separated key:"value" pairs. Each key is a non-empty string consisting of non-control characters other than space (U+0020 ' '), quote (U+0022 '"'), and colon (U+003A ':'). Each value is quoted using U+0022 '"' characters and Go string literal syntax.

Apparently, the parser implemented in the reflect package does not tolerate a space after the colon.

huangapple
  • 本文由 发表于 2014年8月27日 02:54:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/25513243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定