From 4a8e355b737ee03f5db958a2a9915cbe8450cb00 Mon Sep 17 00:00:00 2001 From: nannanwu Date: Sun, 30 Nov 2025 22:43:10 +0800 Subject: [PATCH] =?UTF-8?q?Get=20Post=E4=BB=A5=E5=8F=8A=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E8=B7=AF=E7=94=B1=E4=BC=A0=E5=80=BC=E3=80=81Get=20Post?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E8=A7=A3=E6=9E=90=E5=88=B0=E7=BB=93=E6=9E=84?= =?UTF-8?q?=E4=BD=93=E3=80=81Post=20Xml=E6=95=B0=E6=8D=AE=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E5=88=B0=E7=BB=93=E6=9E=84=E4=BD=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 74 +++++++++++++++++++++++++++++++++++++-- templates/pages/user.html | 27 ++++++++++++++ 2 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 templates/pages/user.html diff --git a/main.go b/main.go index 2865e03..2f30cf8 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,18 @@ type Article struct { Title string // 文章标题 Content string // 文章内容 } +type UserInfo struct { + Username string `json:"username" form:"username"` // 用户名 + Password string `json:"password" form:"password"` // 密码 + Age int `json:"age" form:"age"` // 年龄 +} + +// UserInfoXML 处理xml的结构体 +type UserInfoXML struct { + Username string `json:"username" xml:"username"` // 用户名 + Password string `json:"password" xml:"password"` // 密码 + Age int `json:"age" xml:"age"` // 年龄 +} // UnixToTime 将Unix时间戳转换为格式化的日期时间字符串 // 参数: @@ -71,11 +83,69 @@ func main() { }) r.GET("/test", func(c *gin.Context) { - c.HTML(200, "pages/test", gin.H{ - "title": "Test website", + username := c.Query("username") + age := c.Query("age") + page := c.DefaultQuery("page", "1") + + //Get请求值传递 + //http://0.0.0.0:8088/test?username=%E9%9A%8F%E4%BE%BF&age=%E6%88%91%E6%98%AF&page=0 + c.JSON(200, gin.H{ + "username": username, + "age": age, + "page": page, }) }) + // 渲染user.html模板 + r.GET("/user", func(c *gin.Context) { + c.HTML(200, "pages/user", gin.H{ + "title": "用户页面", + }) + }) + + //Get post请求值传递 + r.POST("/addUser", func(c *gin.Context) { + username := c.PostForm("username") + passwd := c.PostForm("password") + age := c.PostForm("age") + page := c.DefaultPostForm("page", "1") + + //Post请求值传递 + //http://0.0.0.0:8088/test?username=%E9%9A%8F%E4%BE%BF&age=%E6%88%91%E6%98%AF&page=0 + c.JSON(200, gin.H{ + "username": username, + "age": age, + "page": page, + "password": passwd, + }) + }) + + //获取post到结构体 + r.POST("/addUser2", func(c *gin.Context) { + var user UserInfo + err := c.Bind(&user) + if err != nil { + return + } + c.JSON(200, user) + }) + + //获取post到xml转换为json结构体 + r.POST("/addUserXML", func(c *gin.Context) { + var user UserInfoXML + err := c.BindXML(&user) + if err != nil { + return + } + c.JSON(200, user) + }) + + //动态路由传值 + r.GET("/list/:cid", func(c *gin.Context) { + cid := c.Param("cid") + c.String(200, "%v", cid) + }) + // 启动HTTP服务器,监听在8081端口 err := r.Run(":8088") if err != nil { diff --git a/templates/pages/user.html b/templates/pages/user.html new file mode 100644 index 0000000..8d1b201 --- /dev/null +++ b/templates/pages/user.html @@ -0,0 +1,27 @@ +{{/* 定义index页面模板 */}} +{{define "pages/user"}} + + + + + + {{.title}} + + + +
+ +
+ + +
+ + +
+ + +
+ + + +{{end}} \ No newline at end of file