从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")
}
相关推荐
yi.Ist1 分钟前
数据结构 —— 栈(stack)在算法思维中的巧妙运用
开发语言·数据结构
[纳川]12 分钟前
把word中表格转成excle文件
开发语言·c#·word
一块plus28 分钟前
深度详解 Revive 和 Precompile 技术路径
后端·设计模式·架构
iOS开发上架哦32 分钟前
没有Mac如何完成iOS 上架:iOS App 上架App Store流程
后端
晴空月明33 分钟前
分布式系统高可用性设计-负载均衡与容错机制深度解析
后端
s1533535 分钟前
C++STL-deque
开发语言·c++
Honyee1 小时前
java使用UCanAccess操作Access
java·后端
Devil枫1 小时前
Kotlin项目实战与总结
开发语言·jvm·kotlin
八苦1 小时前
留个VKProxy性能测试记录
后端
SimonKing1 小时前
你的Redis分布式锁还在裸奔?看门狗机制让锁更安全!
java·后端·程序员