beego(三)get与post请求

/ beego / 没有评论 / 1945浏览

tree

get请求

添加要实现的路由

新的路由是/abc

router.go

package routers

import (
	"class/controllers"
	"github.com/astaxie/beego"
)

func init() {
	beego.Router("/", &controllers.MainController{})
	beego.Router("/abc", &controllers.MainController{})
}

重写get方法

default.go

 func (c *MainController) Get() {
/* 	c.Data["Website"] = "beego.me"
	c.Data["Email"] = "astaxie@gmail.com"  */
	c.Data["data"] = "home页"
	c.TplName = "test.html"
} 

返回视图

default.go

 func (c *MainController) Get() {
/* 	c.Data["Website"] = "beego.me"
	c.Data["Email"] = "astaxie@gmail.com"  */
	c.Data["data"] = "home页"
	// 返回视图
	c.TplName = "test.html"
} 

post请求

用from表单发送post请求

test.html

<form method="post" action="/abc">

在router.go里面去实现对应的这个路由

	beego.Router("/abc", &controllers.MainController{})

重写post方法

default.go

package controllers

import (
	"github.com/astaxie/beego"
)

type MainController struct {
	beego.Controller
}

 func (c *MainController) Get() {
/* 	c.Data["Website"] = "beego.me"
	c.Data["Email"] = "astaxie@gmail.com"  */
	c.Data["data"] = "home页"
	c.TplName = "test.html"
} 
// 重写post方法
func (c *MainController) Post() {
/* 	 	c.Data["Website"] = "beego.me"
		c.Data["Email"] = "astaxie@gmail.com"  */
		c.Data["data"] = "abc页面"
		c.TplName = "test.html"
	}

返回视图

c.TplName = "test.html"

demo