beego to jump by Redirect method:
1
2
3
|
func
(this *AddController) Get() {
this.Redirect(
"/"
, 302)
}
|
How to terminate the request and throw an exception, beego can do this in the controller:
1
2
3
4
5
6
7
8
9
10
11
12
|
func
(this *MainController) Get() {
this.Abort(
"401"
)
v := this.GetSession(
"asta"
)
if
v == nil {
this.SetSession(
"asta"
, int(1))
this.Data[
"Email"
] = 0
}
else
{
this.SetSession(
"asta"
, v.(int)+1)
this.Data[
"Email"
] = v.(int)
}
this.TplName =
"index.tpl"
}
|
Such this.Abort("401")
code following will not be executed and will be the default page displayed to the user as follows:
beego framework default processing support 401,403,404,500,503 these types of errors. User can define appropriate error handling, e.g. redefining 404 the following page:
1
2
3
4
5
6
7
8
9
10
11
12
|
//定义处理404错误的函数<br>func page_not_found(rw http.ResponseWriter, r *http.Request){
t,_:= template.New(
"404.html"
).ParseFiles(beego.BConfig.WebConfig.ViewsPath+
"/404.html"
)
data :=make(
map
[string]
interface
{})
data[
"content"
] =
"page not found"
t.Execute(rw, data)
}
func
main() {
beego.ErrorHandler(
"404"
,page_not_found)
//如果是404错误返回什么
beego.Router(
"/"
, &controllers.MainController{})
//正常跳转
beego.Run()
}
|
We can custom error pages 404.html
to handle 404 errors.
beego There is also a more humane design that supports user-defined string type of error handling function ,
For example, the following code, the user registration processing a database error page:
1
2
3
4
5
6
7
8
9
10
11
12
|
func
dbError(rw http.ResponseWriter, r *http.Request){
t,_:= template.New(
"dberror.html"
).ParseFiles(beego.BConfig.WebConfig.ViewsPath+
"/dberror.html"
)
data :=make(
map
[string]
interface
{})
data[
"content"
] =
"database is now down"
t.Execute(rw, data)
}
func
main() {
beego.ErrorHandler(
"dbError"
,dbError)
beego.Router(
"/"
, &controllers.MainController{})
beego.Run()
}
|
Once the error-handling code at the entrance registration, then you can encounter any logic in your database error call this.Abort("dbError")
to an exception page processing.
Controller defined Error
From the 1.4.3 version, support the Controller Error way to define error handling function, so you can take advantage of built-in template processing system, as well as context methods.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package
controllers
import
(
"github.com/astaxie/beego"
)
type
ErrorController
struct
{
beego.Controller
}
func
(c *ErrorController) Error404() {
c.Data[
"content"
] =
"page not found"
c.TplName =
"404.tpl"
}
func
(c *ErrorController) Error501() {
c.Data[
"content"
] =
"server error"
c.TplName =
"501.tpl"
}
func
(c *ErrorController) ErrorDb() {
c.Data[
"content"
] =
"database is now down"
c.TplName =
"dberror.tpl"
}
|
通过上面的例子我们可以看到,所有的函数都是有一定规律的,都是 Error
开头,后面的名字就是我们调用 Abort
的名字,
例如 Error404
函数其实调用对应的就是 Abort("404")。
我们就只要在 beego.Run
之前采用 beego.ErrorController
注册这个错误处理函数就可以了
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package
main
import
(
_
"btest/routers"
"btest/controllers"
"github.com/astaxie/beego"
)
func
main() {
beego.ErrorController(&controllers.ErrorController{})
beego.Run()
}
|