1.创建项目用new,我们创建完成后bee run,如果出现报错就关闭go mod模式bee run
2.用c.Data绑定数据

TplName绑定的是视图,是vivews里面的文件
1.路由
1.我们在router.go中设置路由,我们可以通过 ‘/’ 符号来设置,我们默认访问的是get方法。

如果需要设定自己访问的方法那么我们要在这里指定

这里的post是方法的类型,这里的Xml是控制器里面的方法!!!!
2.动态路由

匹配:id为正则类型。匹配cms_123,html这样的url,但是/api失败。/api/200才能匹配成功
2.1在控制器中获取动态路由的值
这里的id必须和路由中的值相同,路由中是id这里就必须是id!!!!!
id:=c.Ctx.Input.Param(":id")
3.正则路由:
在路由中,get是查询,delete是删除值,post是新增加值,put是修改值

需要学习正则表达式,看官方文档
正则路由有什么用?
做了一个路由的伪静态,看起来像一个静态的其实是动态的
4.路由跳转

/代表跳转到最初的位置302代表重定向
1.获取get传过来的值

1.1在控制器中普普通通直接获取
//获取get传值
id,err:=c.GetInt("id")
if err!=nil{
beego.Info(err)
c.Ctx.WriteString("传入参数错误")
return
}
fmt.Printf("值:%v,类型:%T",id,id)
2.post触发
用post方法提交到action后面这个地址上面去 :/user/doAdd
<form action="/user/doAdd" method="post">
I D <input type="text" name="id" /><br><br>
用户名 <input type="text" name="username" /><br><br>
密 码 <input type="password" name="password" /><br><br>
爱 好 <input type="checkbox" value=1 id="lable1" name="hobby" /><lablel for="lable1">吃饭lablel>
<input type="checkbox" value=2 id="lable2" name="hobby" /><lablel for="lable2">睡觉lablel>
<input type="checkbox" value=3 id="lable3" name="hobby" /><lablel for="lable3">敲代码lablel>
<input type="submit" value="提交">
form>
2.1处理post请求,将表单数据提交到后台

func (c *UserController) DoAddUser() {
//接收post请求,获取提交的数据
//这里接受的是int
id,err:=c.GetInt("id")
if err!=nil{
c.Ctx.WriteString("id必须是int类型")
return
}
//key后面名字必须和视图中名字一样!!!!!!!
username:=c.GetString("username")
password:=c.GetString("password")
//这里的hobby接受的是切片
hobby:=c.GetStrings("hobby")
fmt.Printf("值:%v---类型:%T",hobby,hobby)
//id先转化为字符串,不然没办法拼接
c.Ctx.WriteString("用户中心--"+strconv.Itoa(id)+username +password)
}
2.2用ParseForm获取,将表单获取的数据放在结构体中
//定义一个User的结构体,接收数据必须要这个结构体
type User struct {
//如果我们需要重新命名怎么办,比如说首字母小写
//Username string `form:"username" json:"username"`
//from后面的名字必须和html文件里面的是一样
Username string `form:"username"`
Password string `form:"password"`
Hobby []string `form:"hobby"`
}
func (c *UserController) DoEditUser() {
//controller里面解析
//在beego中如果我们需要返回一个json数据的话,需要把数据放在结构体中
//第一步就是实例化结构体
u:=User{}
//一定是要传地址进去
if err:=c.ParseForm(&u);err!=nil{
c.Ctx.WriteString("post提交失败")
return
}
fmt.Printf("%#v",u)
c.Ctx.WriteString("解析post数据成功")
}
2.3在requestbody中获取post提交的xml数据
如果需要接收xml文件,那么我们需要在配置文件里面将copyrequestbody=true,改完配置要重新运行项目
copyrequestbody=true
type Product struct{
Title string `form :"title" xml:"title"`
Content string `form:"content" xml:"content"`
}
//定义一个方法接收post传过过来的xml数据,xml里面的数据都是成对出现的
func (c *GoodsController) Xml() {//post
//打印数据,RequestBody返回的是一个切片,我们转化成string类型就可以打印了
p:=Product{}
str:=string(c.Ctx.Input.RequestBody)
beego.Info(str)
c.Ctx.WriteString(str)
//如何解析xml数据
var err error
if e:=xml.Unmarshal(c.Ctx.Input.RequestBody,&p);e!=nil{
//转化失败
c.Data["json"]=err.Error()
c.ServeJSON()
}else {
//如果成功了就先把xml数据转化成结构体再转化成json数据
fmt.Printf("%#v",p)
c.Data["json"]=p
c.ServeJSON()
}
}
3.返回json数据,需要将数据放在结构体中
//在beego中,如果我们需要返回一个json的话,需要把数据放在结构体中,必须要先实例化
func (c *UserController) GetUser() {
u:=User{
"张三",
"123456",
[]string{"1","2"},
}
//返回一个json数据就这两句话
c.Data["json"]=u
c.ServeJSON()
}
put修改数据
可以用普通的方法

也可以用ParseForm
type Product struct{
Title string `form :"title" xml:"title"`
Content string `form:"content" xml:"content"`
}
func (c *GoodsController) DoEdit() {//put
// title:=c.GetString("title")
p:=Product{}
if err:=c.ParseForm(&p);err!=nil{
c.Ctx.WriteString("获取数据失败")
}
fmt.Printf("%#v",p)
c.Ctx.WriteString("执行修改操作")
}