从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")
}
相关推荐
oak隔壁找我2 分钟前
公司级 Maven Parent POM 设计指南
java·后端
Determined_man3 分钟前
注解
后端
11来了6 分钟前
04-Agent 武器库-集成百炼MCP(Spring AI Alibaba)
后端
TeamDev7 分钟前
使用 Shadcn UI 构建 C# 桌面应用
前端·后端·.net
uhakadotcom11 分钟前
如何从阿里云的sls日志中清洗出有价值的信息?
后端·面试·github
李昊哲小课15 分钟前
spring 中 HttpStatus 与 ResponseEntity
spring boot·后端·spring·http·spring cloud·restful
夜晚中的人海27 分钟前
【C++】二分查找算法习题
开发语言·c++·算法
间彧36 分钟前
Java 堆、栈、方法区详解与项目实战
后端
间彧39 分钟前
Java内存区域详解与项目实战
后端