如何将HTML数据移动到Golang中的结构体中

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

How to move html data to struct in Golang

问题

我是新手,正在尝试将数据从一个操作字段移动到一个结构体中。

我的主函数是:

func main() {
    http.HandleFunc("/public/", func(w http.ResponseWriter, r *http.Request) {
        http.ServeFile(w, r, r.URL.Path[1:])
    })
    http.HandleFunc("/public/new_user/", signup)
    http.ListenAndServe(":8080", nil)
}

接收来自signup.html的注册数据的结构体如下:

type Signup struct {
    name  string
    email string
}

将数据接收到上述结构体的函数如下:

func signup(w http.ResponseWriter, r *http.Request) {
    var S Signup

    S.name = r.FormValue("name")
    S.email = r.FormValue("email")

    // 仅用于测试输出
    fmt.Println("%v\n", S.name)
    fmt.Println("%v\n", S.email)
}

我在代码中使用了http.HandleFunc("/public/new_user/", signup)来渲染signup函数。然而,当我路由到public/new_user时,我收到一个空白的HTML页面,而不是从signup函数接收到的结构体。

上述代码中的S.name = r.FormValue("name")S.email = r.FormValue("email")这两行是从以下signup.html页面获取信息的:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Sign Up</title>
</head>

<body>

<h1>Sign Up</h1>
<br/>

<form action="/public/new_user" method="post" id="form1">

<label>Name:</label>
<input type="string" name="name" />
</br>
</br>
<label>Email:</label>
<input type="string" name="email" />
</form>

</br>

<button type="submit" form="form1" value="Submit">Submit</button>

</body>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

</html>

正如你所看到的,该页面在提交后路由到public/new_user。请问为什么public/new_user是空白的?我已经在代码中使用了http.HandleFunc("/public/new_user/", signup)来渲染signup函数。谢谢!

英文:

I'm new to Golang and I am trying to move data from an action field to a struct.

My func main is:

func main () {
    http.HandleFunc(&quot;/public/&quot;, func (w http.ResponseWriter, r *http.Request) {
        http.ServeFile(w, r, r.URL.Path[1:]) })
    http.HandleFunc(&quot;/public/new_user/&quot;, signup) 
    http.ListenAndServe(&quot;:8080&quot;, nil)
}


// struct for recieving Signup data input from signup.html
type Signup struct {
    name  string
    email string
}

// func to receive data into struc above FROM signup.html
func signup(w http.ResponseWriter, r *http.Request) {
	var S Signup 

	S.name = r.FormValue(&quot;name&quot;)
	S.email = r.FormValue(&quot;email&quot;)

	//this is just to test the output
    fmt.Println(&quot;%v\n&quot;, S.name)
    fmt.Println(&quot;%v\n&quot;, S.email)
}

I have http.HandleFunc(&quot;public/new_user/&quot;, signup) which should render the signup function. However, I am receiving a BLANK html page when I route to public/new_user instead of receiving the struct from func signup.

The S.name = r.FormValue(&quot;name&quot;) and S.email = r.FormValue(&quot;email&quot;) lines ABOVE are getting their info from the signup page which is:

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;title&gt;Sign Up&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

&lt;h1&gt;Sign Up&lt;/h1&gt;
&lt;br/&gt;

&lt;form action=&quot;/public/new_user&quot; method=&quot;post&quot; id=&quot;form1&quot;&gt;

&lt;label&gt;Name:&lt;/label&gt;
&lt;input type=&quot;string&quot; name=&quot;name&quot; /&gt;
&lt;/br&gt;
&lt;/br&gt;
&lt;label&gt;Email:&lt;/label&gt;
&lt;input type=&quot;string&quot; name=&quot;email&quot; /&gt;
&lt;/form&gt;

&lt;/br&gt;

&lt;button type=&quot;submit&quot; form=&quot;form1&quot; value=&quot;Submit&quot;&gt;Submit&lt;/button&gt;

&lt;/body&gt;

&lt;!-- Latest compiled and minified CSS --&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css&quot;&gt;

&lt;!-- jQuery library --&gt;
&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;!-- Latest compiled JavaScript --&gt;
&lt;script src=&quot;http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js&quot;&gt;&lt;/script&gt;

&lt;/html&gt;

As you can see, this page routes to public/new_user upon submit. Can anyone please tell me why public/new_user is blank seeing as how I have public/new_user in:

http.HandleFunc(&quot;/public/new_user/&quot;, signup) 

Which should render the signup function? Thanks

答案1

得分: 1

第一个问题是你将处理程序注册为模式"/public/new_user/",但你将表单提交到/public/new_user

你需要将HTML的<form>更改为使用这个URL:

<form action="/public/new_user/" method="post" id="form1">

这样做的重要性在于,即使访问URL/public/new_user可以工作,因为会自动重定向(301 Moved Permanently响应),但提交的表单在浏览器发出第二个请求到新URL(以斜杠结尾)时不会被“携带”过去。

在这样做之后,你仍然会收到一个空白页面,因为你的signup()函数没有写入任何响应(尽管你现在可以在控制台上看到提交和打印的表单数据)。

如果你想看到一个响应,要么写入一些内容,要么重定向到另一个页面。

还要注意,fmt.Println()没有格式字符串参数,它只会打印你传递给它的所有内容。如果你想使用格式字符串,可以使用fmt.Printf()。另外,你可以使用%+v格式字符串打印带有字段名的完整结构体。

例如:

func signup(w http.ResponseWriter, r *http.Request) {
    var S Signup
    S.name = r.FormValue("name")
    S.email = r.FormValue("email")

    fmt.Printf("%+v\n", S)

    fmt.Fprintf(w, "Form received, you used name: %s", S.name)
}
英文:

The first problem is that you registered your handler for the pattern &quot;/public/new_user/&quot; but you direct your form post to /public/new_user.

You have to change your HTML &lt;form&gt; to use this URL:

&lt;form action=&quot;/public/new_user/&quot; method=&quot;post&quot; id=&quot;form1&quot;&gt;

The importance of this is that even though visiting the URL /public/new_user works because an automatic redirect (301 Moved permanently response) happens, but the submitted form will not be "carried" over when your browser makes the 2nd request to the new URL (ending with a slash).

After doing this you will still receiving a blank page because your signup() function does not write any response (although you will now see the submitted and printed form data on your console).

If you want to see a response, either write something or redirect to another page.

Also note that fmt.Println() does not have a format string parameter, it just prints everything you pass to it. Use fmt.Printf() if you want to use a format string. Also you can use the %+v format string to print a complete struct with field names.

For example:

func signup(w http.ResponseWriter, r *http.Request) {
	var S Signup
	S.name = r.FormValue(&quot;name&quot;)
	S.email = r.FormValue(&quot;email&quot;)

	fmt.Printf(&quot;%+v\n&quot;, S)

    fmt.Fprintf(w, &quot;Form received, you used name: %s&quot;, S.name)
}

答案2

得分: 0

我认为你正在跳过表单验证。你必须检查客户端输入,并且应该添加令牌参数以防止XSRF攻击。

你可以使用这个包来验证用户的输入。

表单验证器

英文:

i think you are skipping form validation. You must check client input and you should put token parameter for prevent Xsrf attack.

You can use this package for validate user's inputs.

Form validator

huangapple
  • 本文由 发表于 2015年7月7日 13:15:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/31260231.html
匿名

发表评论

匿名网友

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

确定