MVC-VC 1> 新建一個user.go控制器,其代碼如下: 2> 在路由器文件router.go中添加路由配置,其代碼如下: 3> 在views文件夾下添加2html頁面,分別為home.html,edit.html 4> Home.html頁面的代碼如下: 5>edit.html頁面如下: 6 ...
MVC-VC
1> 新建一個user.go控制器,其代碼如下:
package controllers import ( "webapp/models" "strconv" "fmt" "github.com/astaxie/beego" "github.com/astaxie/beego/orm" _"github.com/go-sql-driver/mysql" ) //控制器 type HomeController struct { beego.Controller } //get請求用戶列表 執行的方法 func (c *HomeController) Get() { o := orm.NewOrm() o.Using("default") //用戶列表 userlist:=models.DataList() fmt.Println("用戶列表數據:") fmt.Println(userlist) /*返回json數據*/ c.Data["datas"]=userlist c.TplName = "home.html" } //刪除方法 type DeleteHomeController struct { beego.Controller } func (c *DeleteHomeController) Get() { o := orm.NewOrm() o.Using("default") id, _ := c.GetInt64("Id") fmt.Println(id) isdelete:=models.DeleteById(id) if isdelete{ fmt.Println("刪除成功") }else{ fmt.Println("刪除失敗") } //路徑的跳轉 c.Redirect("/", 302) } //get請求用戶列表 執行的方法 type EditHomeController struct { beego.Controller } func (c *EditHomeController) Get() { o := orm.NewOrm() o.Using("default") //用戶列表 idval,errId:=strconv.ParseInt(c.GetString("Id"),10,64); if errId!=nil{ fmt.Println("缺少參數id"); } user,err:=models.QueryById(idval) if err==true{ fmt.Println("獲取模型失敗"); fmt.Println(err); }else{ fmt.Println("獲取模型成功"); } /*返回json數據*/ c.Data["data"]=user c.TplName = "edit.html" } //編輯新增/更新方法 type UpdateHomeController struct { beego.Controller } func (c *UpdateHomeController) Post() { o := orm.NewOrm() o.Using("default") fields := make(map[string] interface{}) //獲取參數 id, _ := c.GetInt("Id") name:=c.GetString("name") nickname:=c.GetString("nickname") pwd:=c.GetString("pwd") email:=c.GetString("email") sex:=c.GetString("sex") phone:=c.GetString("phone") roleid:="1"; status,_:=strconv.ParseInt("1",10,64); //新增用戶 if id==0 { idval,_:=strconv.ParseInt("0",10,64); //新增一條數據,並給模型賦值 user :=models.Create(idval,name,nickname,pwd,email,sex,roleid,status,phone) fmt.Println(user) }else{ fields["Id"]=id name:= c.GetString("name") fields["Name"]=name nickname:= c.GetString("nickname") fields["Nickname"]=nickname pwd:= c.GetString("pwd") fields["Pwd"]=pwd sex:= c.GetString("sex") fields["Sex"]=sex email:= c.GetString("email") fields["Email"]=email //更新 models.UpdateById(id,"user", fields) } //路徑的跳轉 c.Redirect("/Home/List", 302) } //控制器 type UserController struct { beego.Controller } //get請求用戶分頁列表 執行的方法 func (c *UserController) Get() { o := orm.NewOrm() o.Using("default") //得到當前分頁html的數據 pa,err:=c.GetInt("page") if err!=nil{ println(err) } pre_page:=3 totals:=models.GetDataNum() res := models.Paginator(pa, pre_page, totals) //得到分頁user的數據 userlist:=models.LimitList(3,pa) c.Data["datas"] = userlist //用戶的數據 c.Data["paginator"] = res //分頁的數據 c.Data["totals"] = totals //分頁的數據 c.TplName="list.html" }
2> 在路由器文件router.go中添加路由配置,其代碼如下:
package routers import ( "firstweb/controllers" "github.com/astaxie/beego" ) func init() { //beego.Router("/", &controllers.MainController{}) beego.Router("/", &controllers.HomeController{}) beego.Router("/Home/Edit", &controllers.EditHomeController{}) beego.Router("/Home/Update", &controllers.UpdateHomeController{}) beego.Router("/Home/Delete", &controllers.DeleteHomeController{}) beego.Router("/Home/List", &controllers.UserController{}) }
3> 在views文件夾下添加2html頁面,分別為home.html,edit.html
4> Home.html頁面的代碼如下:
<!DOCTYPE html> <html> <head> <title>首頁 - 用戶列表頁面</title> <link rel="shortcut icon" href="/static/img/favicon.png" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <div class="row pull-right" style="margin-bottom: 20px;margin-right: 45px;text-align:right;"> <a class="btn btn-success" href="/Home/Edit?Id=0">添加</a> </div> <table class="table table-striped table-hover table-bordered "> <thead> <th style="text-align: center">ID</th> <th style="text-align: center">名稱</th> <th style="text-align: center">昵稱</th> <th style="text-align: center">密碼</th> <th style="text-align: center">Email</th> <th style="text-align: center">性別</th> <th style="text-align: center">手機號</th> <th style="text-align: center">操作</th> </thead> <tbody id="sortable"> {{range $index, $item := .datas}} <tr class="sort-item" id="module_{{$item.Id}}" value="{{$item.Id}}"> <td style="text-align: center;width: 150px;"><span class="label label-default" >{{$item.Id}}</span></td> <td style="text-align: center;width: 240px;" ><strong>{{$item.Name}}</strong></td> <td style="text-align: center;width: 240px;" ><strong>{{$item.Nickname}}</strong></td> <td style="text-align: center;width: 240px;" ><strong>{{$item.Pwd}}</strong></td> <td style="text-align: center;width: 240px;" ><strong>{{$item.Email}}</strong></td> <td style="text-align: center;width: 240px;" ><strong>{{$item.Sex}}</strong></td> <td style="text-align: center;width: 240px;" ><strong>{{$item.Phone}}</strong></td> <td style="text-align: center;width: 150px;"> <a href="/Home/Edit?Id={{$item.Id}}" class="label label-info" title="修改" >修改</a> <a href="/Home/Delete?Id={{$item.Id}}" class="label label-info" title="刪除">刪除</a> </td> </tr> {{end}} </tbody> </table> <script type="text/javascript" src="http://cdn.staticfile.org/jquery/2.0.3/jquery.min.js"></script> </body> </html>
5>edit.html頁面如下:
<div class="col-xs-6 col-sm-9"> <form class="form-horizontal" method="post" id="mainPageContent" action="/Home/Update" enctype="multipart/form-data"> {{if .data}} <input type="hidden" name="Id" value="{{.data.Id}}"> {{end}} <div class="form-group has-success"> <label class="col-sm-2 control-label">名稱</label> <div class="col-sm-3"> <input type="text" class="form-control" id="name" name="name" placeholder="姓名" {{if .data}}value="{{.data.Name}}"{{end}}> </div> </div> <div class="form-group has-success"> <label class="col-sm-2 control-label">昵稱</label> <div class="col-sm-3"> <input type="text" class="form-control" id="nickname" name="nickname" placeholder="昵稱" {{if .data}}value="{{.data.Nickname}}"{{end}}> </div> </div> <div class="form-group has-success"> <label class="col-sm-2 control-label">密碼</label> <div class="col-sm-3"> <input type="text" class="form-control" id="pwd" name="pwd" placeholder="密碼" {{if .data}}value="{{.data.Pwd}}"{{end}}> </div> </div> <div class="form-group has-success"> <label class="col-sm-2 control-label">Email</label> <div class="col-sm-3"> <input type="text" class="form-control" id="email" name="email" placeholder="Email" {{if .data}}value="{{.data.Email}}"{{end}}> </div> </div> <div class="form-group has-success"> <label class="col-sm-2 control-label">性別</label> <div class="col-sm-3"> <input type="text" class="form-control" id="sex" name="sex" placeholder="Sex" {{if .data}}value="{{.data.Sex}}"{{end}}> </div> </div> <div class="form-group has-success"> <label class="col-sm-2 control-label">手機號</label> <div class="col-sm-3"> <input type="text" class="form-control" id="phone" name="phone" placeholder="Phone" {{if .data}}value="{{.data.Phone}}"{{end}}> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-info">確定</button> </div> </div> </form> </div>
6> 運行起來的效果如下:
1->列表頁面如下:
2->編輯頁面如下:
3->新增頁面如下:
4->刪除效果
7> 下一回實現分頁的效果