#2
#2
Showing
2 changed files
with
160 additions
and
160 deletions
| ... | @@ -30,7 +30,7 @@ Here is the canonical "Hello, world" example app for beego: | ... | @@ -30,7 +30,7 @@ Here is the canonical "Hello, world" example app for beego: |
| 30 | } | 30 | } |
| 31 | 31 | ||
| 32 | func (this *MainController) Get() { | 32 | func (this *MainController) Get() { |
| 33 | this.Ct.WriteString("hello world") | 33 | this.Ctx.WriteString("hello world") |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | func main() { | 36 | func main() { |
| ... | @@ -177,10 +177,10 @@ So you can define ChildStruct method to accomplish the interface's method, now l | ... | @@ -177,10 +177,10 @@ So you can define ChildStruct method to accomplish the interface's method, now l |
| 177 | 177 | ||
| 178 | func (this *AddController) Post() { | 178 | func (this *AddController) Post() { |
| 179 | //data deal with | 179 | //data deal with |
| 180 | this.Ct.Request.ParseForm() | 180 | this.Ctx.Request.ParseForm() |
| 181 | pkgname := this.Ct.Request.Form.Get("pkgname") | 181 | pkgname := this.Ctx.Request.Form.Get("pkgname") |
| 182 | content := this.Ct.Request.Form.Get("content") | 182 | content := this.Ctx.Request.Form.Get("content") |
| 183 | beego.Info(this.Ct.Request.Form) | 183 | beego.Info(this.Ctx.Request.Form) |
| 184 | pk := models.GetCruPkg(pkgname) | 184 | pk := models.GetCruPkg(pkgname) |
| 185 | if pk.Id == 0 { | 185 | if pk.Id == 0 { |
| 186 | var pp models.PkgEntity | 186 | var pp models.PkgEntity |
| ... | @@ -194,7 +194,7 @@ So you can define ChildStruct method to accomplish the interface's method, now l | ... | @@ -194,7 +194,7 @@ So you can define ChildStruct method to accomplish the interface's method, now l |
| 194 | at.Pkgid = pk.Id | 194 | at.Pkgid = pk.Id |
| 195 | at.Content = content | 195 | at.Content = content |
| 196 | models.InsertArticle(at) | 196 | models.InsertArticle(at) |
| 197 | this.Ct.Redirect(302, "/admin/index") | 197 | this.Ctx.Redirect(302, "/admin/index") |
| 198 | } | 198 | } |
| 199 | 199 | ||
| 200 | ## View / Template | 200 | ## View / Template |
| ... | @@ -216,7 +216,7 @@ then beego will find the file in the path:`/views/admin/add.tpl` | ... | @@ -216,7 +216,7 @@ then beego will find the file in the path:`/views/admin/add.tpl` |
| 216 | 216 | ||
| 217 | if you don't set TplNames,beego will find like this: | 217 | if you don't set TplNames,beego will find like this: |
| 218 | 218 | ||
| 219 | c.TplNames = c.ChildName + "/" + c.Ct.Request.Method + "." + c.TplExt | 219 | c.TplNames = c.ChildName + "/" + c.Ctx.Request.Method + "." + c.TplExt |
| 220 | 220 | ||
| 221 | So if the ChildName="AddController",Request Method= "POST",default TplEXT="tpl" | 221 | So if the ChildName="AddController",Request Method= "POST",default TplEXT="tpl" |
| 222 | So beego will file the file in the path:`/view/AddController/POST.tpl` | 222 | So beego will file the file in the path:`/view/AddController/POST.tpl` | ... | ... |
| 1 | package beego | 1 | package beego |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "bytes" | 4 | "bytes" |
| 5 | "encoding/json" | 5 | "encoding/json" |
| 6 | "encoding/xml" | 6 | "encoding/xml" |
| 7 | "html/template" | 7 | "html/template" |
| 8 | "io/ioutil" | 8 | "io/ioutil" |
| 9 | "net/http" | 9 | "net/http" |
| 10 | "net/url" | 10 | "net/url" |
| 11 | "path" | 11 | "path" |
| 12 | "strconv" | 12 | "strconv" |
| 13 | ) | 13 | ) |
| 14 | 14 | ||
| 15 | type Controller struct { | 15 | type Controller struct { |
| 16 | Ct *Context | 16 | Ctx *Context |
| 17 | Tpl *template.Template | 17 | Tpl *template.Template |
| 18 | Data map[interface{}]interface{} | 18 | Data map[interface{}]interface{} |
| 19 | ChildName string | 19 | ChildName string |
| 20 | TplNames string | 20 | TplNames string |
| 21 | Layout string | 21 | Layout string |
| 22 | TplExt string | 22 | TplExt string |
| 23 | } | 23 | } |
| 24 | 24 | ||
| 25 | type ControllerInterface interface { | 25 | type ControllerInterface interface { |
| 26 | Init(ct *Context, cn string) | 26 | Init(ct *Context, cn string) |
| 27 | Prepare() | 27 | Prepare() |
| 28 | Get() | 28 | Get() |
| 29 | Post() | 29 | Post() |
| 30 | Delete() | 30 | Delete() |
| 31 | Put() | 31 | Put() |
| 32 | Head() | 32 | Head() |
| 33 | Patch() | 33 | Patch() |
| 34 | Options() | 34 | Options() |
| 35 | Finish() | 35 | Finish() |
| 36 | Render() error | 36 | Render() error |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | func (c *Controller) Init(ct *Context, cn string) { | 39 | func (c *Controller) Init(ctx *Context, cn string) { |
| 40 | c.Data = make(map[interface{}]interface{}) | 40 | c.Data = make(map[interface{}]interface{}) |
| 41 | c.Tpl = template.New(cn + ct.Request.Method) | 41 | c.Tpl = template.New(cn + ctx.Request.Method) |
| 42 | c.Tpl = c.Tpl.Funcs(beegoTplFuncMap) | 42 | c.Tpl = c.Tpl.Funcs(beegoTplFuncMap) |
| 43 | c.Layout = "" | 43 | c.Layout = "" |
| 44 | c.TplNames = "" | 44 | c.TplNames = "" |
| 45 | c.ChildName = cn | 45 | c.ChildName = cn |
| 46 | c.Ct = ct | 46 | c.Ctx = ctx |
| 47 | c.TplExt = "tpl" | 47 | c.TplExt = "tpl" |
| 48 | 48 | ||
| 49 | } | 49 | } |
| 50 | 50 | ||
| 51 | func (c *Controller) Prepare() { | 51 | func (c *Controller) Prepare() { |
| 52 | 52 | ||
| 53 | } | 53 | } |
| 54 | 54 | ||
| 55 | func (c *Controller) Finish() { | 55 | func (c *Controller) Finish() { |
| 56 | 56 | ||
| 57 | } | 57 | } |
| 58 | 58 | ||
| 59 | func (c *Controller) Get() { | 59 | func (c *Controller) Get() { |
| 60 | http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405) | 60 | http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405) |
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | func (c *Controller) Post() { | 63 | func (c *Controller) Post() { |
| 64 | http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405) | 64 | http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405) |
| 65 | } | 65 | } |
| 66 | 66 | ||
| 67 | func (c *Controller) Delete() { | 67 | func (c *Controller) Delete() { |
| 68 | http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405) | 68 | http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405) |
| 69 | } | 69 | } |
| 70 | 70 | ||
| 71 | func (c *Controller) Put() { | 71 | func (c *Controller) Put() { |
| 72 | http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405) | 72 | http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405) |
| 73 | } | 73 | } |
| 74 | 74 | ||
| 75 | func (c *Controller) Head() { | 75 | func (c *Controller) Head() { |
| 76 | http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405) | 76 | http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405) |
| 77 | } | 77 | } |
| 78 | 78 | ||
| 79 | func (c *Controller) Patch() { | 79 | func (c *Controller) Patch() { |
| 80 | http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405) | 80 | http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405) |
| 81 | } | 81 | } |
| 82 | 82 | ||
| 83 | func (c *Controller) Options() { | 83 | func (c *Controller) Options() { |
| 84 | http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405) | 84 | http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405) |
| 85 | } | 85 | } |
| 86 | 86 | ||
| 87 | func (c *Controller) Render() error { | 87 | func (c *Controller) Render() error { |
| 88 | //if the controller has set layout, then first get the tplname's content set the content to the layout | 88 | //if the controller has set layout, then first get the tplname's content set the content to the layout |
| 89 | if c.Layout != "" { | 89 | if c.Layout != "" { |
| 90 | if c.TplNames == "" { | 90 | if c.TplNames == "" { |
| 91 | c.TplNames = c.ChildName + "/" + c.Ct.Request.Method + "." + c.TplExt | 91 | c.TplNames = c.ChildName + "/" + c.Ctx.Request.Method + "." + c.TplExt |
| 92 | } | 92 | } |
| 93 | t, err := c.Tpl.ParseFiles(path.Join(ViewsPath, c.TplNames), path.Join(ViewsPath, c.Layout)) | 93 | t, err := c.Tpl.ParseFiles(path.Join(ViewsPath, c.TplNames), path.Join(ViewsPath, c.Layout)) |
| 94 | if err != nil { | 94 | if err != nil { |
| 95 | Trace("template ParseFiles err:", err) | 95 | Trace("template ParseFiles err:", err) |
| 96 | } | 96 | } |
| 97 | _, file := path.Split(c.TplNames) | 97 | _, file := path.Split(c.TplNames) |
| 98 | newbytes := bytes.NewBufferString("") | 98 | newbytes := bytes.NewBufferString("") |
| 99 | t.ExecuteTemplate(newbytes, file, c.Data) | 99 | t.ExecuteTemplate(newbytes, file, c.Data) |
| 100 | tplcontent, _ := ioutil.ReadAll(newbytes) | 100 | tplcontent, _ := ioutil.ReadAll(newbytes) |
| 101 | c.Data["LayoutContent"] = template.HTML(string(tplcontent)) | 101 | c.Data["LayoutContent"] = template.HTML(string(tplcontent)) |
| 102 | _, file = path.Split(c.Layout) | 102 | _, file = path.Split(c.Layout) |
| 103 | err = t.ExecuteTemplate(c.Ct.ResponseWriter, file, c.Data) | 103 | err = t.ExecuteTemplate(c.Ctx.ResponseWriter, file, c.Data) |
| 104 | if err != nil { | 104 | if err != nil { |
| 105 | Trace("template Execute err:", err) | 105 | Trace("template Execute err:", err) |
| 106 | } | 106 | } |
| 107 | } else { | 107 | } else { |
| 108 | if c.TplNames == "" { | 108 | if c.TplNames == "" { |
| 109 | c.TplNames = c.ChildName + "/" + c.Ct.Request.Method + "." + c.TplExt | 109 | c.TplNames = c.ChildName + "/" + c.Ctx.Request.Method + "." + c.TplExt |
| 110 | } | 110 | } |
| 111 | t, err := c.Tpl.ParseFiles(path.Join(ViewsPath, c.TplNames)) | 111 | t, err := c.Tpl.ParseFiles(path.Join(ViewsPath, c.TplNames)) |
| 112 | if err != nil { | 112 | if err != nil { |
| 113 | Trace("template ParseFiles err:", err) | 113 | Trace("template ParseFiles err:", err) |
| 114 | } | 114 | } |
| 115 | _, file := path.Split(c.TplNames) | 115 | _, file := path.Split(c.TplNames) |
| 116 | err = t.ExecuteTemplate(c.Ct.ResponseWriter, file, c.Data) | 116 | err = t.ExecuteTemplate(c.Ctx.ResponseWriter, file, c.Data) |
| 117 | if err != nil { | 117 | if err != nil { |
| 118 | Trace("template Execute err:", err) | 118 | Trace("template Execute err:", err) |
| 119 | } | 119 | } |
| 120 | } | 120 | } |
| 121 | return nil | 121 | return nil |
| 122 | } | 122 | } |
| 123 | 123 | ||
| 124 | func (c *Controller) Redirect(url string, code int) { | 124 | func (c *Controller) Redirect(url string, code int) { |
| 125 | c.Ct.Redirect(code, url) | 125 | c.Ctx.Redirect(code, url) |
| 126 | } | 126 | } |
| 127 | 127 | ||
| 128 | func (c *Controller) ServeJson() { | 128 | func (c *Controller) ServeJson() { |
| 129 | content, err := json.MarshalIndent(c.Data, "", " ") | 129 | content, err := json.MarshalIndent(c.Data, "", " ") |
| 130 | if err != nil { | 130 | if err != nil { |
| 131 | http.Error(c.Ct.ResponseWriter, err.Error(), http.StatusInternalServerError) | 131 | http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError) |
| 132 | return | 132 | return |
| 133 | } | 133 | } |
| 134 | c.Ct.SetHeader("Content-Length", strconv.Itoa(len(content)), true) | 134 | c.Ctx.SetHeader("Content-Length", strconv.Itoa(len(content)), true) |
| 135 | c.Ct.ContentType("json") | 135 | c.Ctx.ContentType("json") |
| 136 | c.Ct.ResponseWriter.Write(content) | 136 | c.Ctx.ResponseWriter.Write(content) |
| 137 | } | 137 | } |
| 138 | 138 | ||
| 139 | func (c *Controller) ServeXml() { | 139 | func (c *Controller) ServeXml() { |
| 140 | content, err := xml.Marshal(c.Data) | 140 | content, err := xml.Marshal(c.Data) |
| 141 | if err != nil { | 141 | if err != nil { |
| 142 | http.Error(c.Ct.ResponseWriter, err.Error(), http.StatusInternalServerError) | 142 | http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError) |
| 143 | return | 143 | return |
| 144 | } | 144 | } |
| 145 | c.Ct.SetHeader("Content-Length", strconv.Itoa(len(content)), true) | 145 | c.Ctx.SetHeader("Content-Length", strconv.Itoa(len(content)), true) |
| 146 | c.Ct.ContentType("xml") | 146 | c.Ctx.ContentType("xml") |
| 147 | c.Ct.ResponseWriter.Write(content) | 147 | c.Ctx.ResponseWriter.Write(content) |
| 148 | } | 148 | } |
| 149 | 149 | ||
| 150 | func (c *Controller) Input() url.Values { | 150 | func (c *Controller) Input() url.Values { |
| 151 | c.Ct.Request.ParseForm() | 151 | c.Ctx.Request.ParseForm() |
| 152 | return c.Ct.Request.Form | 152 | return c.Ctx.Request.Form |
| 153 | } | 153 | } | ... | ... |
-
Please register or sign in to post a comment