从0开始学go第七天

gin获取表单from中的数据

模拟简单登录页面:

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">

    <title>login</title>
</head>

<body>
    
    <form action="/login" method="post">
        <div>
            <label for="username">username:</label>
            <input type="text" name="username" id="username">
        </div>

        <div>
            <label for="password">password:</label>
            <input type="text" name="password" id="password">
        </div>

        <div>
            //当submit被点击以后,会向服务端发送请求,发送方式为post
            <input type="submit" value="登录">
        </div>

    </form>

</body>

</html>

对于POST的返回页面:

Go 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">

    <title>index</title>
</head>

<body>
    <h1>Hello , {{ .Name }}!</h1>
    <h1>你的密码是 : {{ .Password }}</h1>

</body>

</html>

获取方法一代码:

Go 复制代码
package main

//from表单提交的参数
import (
	"net/http"

	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()

	r.LoadHTMLFiles("./login.html", "./index.html")

	r.GET("/login", func(c *gin.Context) {
		c.HTML(http.StatusOK, "login.html", nil)
	})
	//POST请求
	r.POST("/login", func(c *gin.Context) {
		username := c.PostForm("username")
		password := c.PostForm("password")
		c.HTML(http.StatusOK, "index.html", gin.H{
			"Name":     username,
			"Password": password,
		})
	})

	r.Run(":9090")
}

获取方法二代码:

带默认值

Go 复制代码
package main

//from表单提交的参数
import (
	"net/http"

	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()

	r.LoadHTMLFiles("./login.html", "./index.html")

	r.GET("/login", func(c *gin.Context) {
		c.HTML(http.StatusOK, "login.html", nil)
	})
	//POST请求
	r.POST("/login", func(c *gin.Context) {
		// username := c.PostForm("username")
		// password := c.PostForm("password")
		username := c.DefaultPostForm("username", "somebody")
		password := c.DefaultPostForm("password", "*******")

		c.HTML(http.StatusOK, "index.html", gin.H{
			"Name":     username,
			"Password": password,
		})
	})

	r.Run(":9090")
}

方法三:GetPostFrom

Go 复制代码
package main

//from表单提交的参数
import (
	"net/http"

	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()

	r.LoadHTMLFiles("./login.html", "./index.html")

	r.GET("/login", func(c *gin.Context) {
		c.HTML(http.StatusOK, "login.html", nil)
	})
	//POST请求
	r.POST("/login", func(c *gin.Context) {
		// username := c.PostForm("username")
		// password := c.PostForm("password")
		//username := c.DefaultPostForm("username", "somebody")
		//password := c.DefaultPostForm("password", "*******")

		username,ok := c.GetPostForm("username")
		if !ok {
			username = "sb"
		}
		password,ok := c.GetPostForm("password")
		if !ok {
			password = "******"
		}

		c.HTML(http.StatusOK, "index.html", gin.H{
			"Name":     username,
			"Password": password,
		})
	})

	r.Run(":9090")
}
相关推荐
一个天蝎座 白勺 程序猿8 分钟前
Python练习(1)Python基础类型操作语法实战:20道实战题解与案例分析(上)
开发语言·python·学习
重生之后端学习15 分钟前
day08-Elasticsearch
后端·elasticsearch·spring cloud·中间件·全文检索·jenkins
lightqjx18 分钟前
【数据结构】顺序表(sequential list)
c语言·开发语言·数据结构·算法
巨人张27 分钟前
信息素养Python编程题
开发语言·python
志辉AI编程1 小时前
别人还在入门,你已经精通!Claude Code进阶必备14招
后端·ai编程
阿猿收手吧!1 小时前
【计算机网络】HTTP1.0 HTTP1.1 HTTP2.0 QUIC HTTP3 究极总结
开发语言·计算机网络
JAVA学习通1 小时前
图书管理系统(完结版)
java·开发语言
代码老y1 小时前
Spring Boot项目中大文件上传的高级实践与性能优化
spring boot·后端·性能优化
paishishaba1 小时前
处理Web请求路径参数
java·开发语言·后端