博客
关于我
beego自定义404、401、403、500、503等页面
阅读量:656 次
发布时间:2019-03-15

本文共 934 字,大约阅读时间需要 3 分钟。

beego 框架默认支持 404、401、403、500、503 这几种错误的处理。用户可以自定义相应的错误处理,从1.4.3版本开始,支持Controller方式定义Error错误处理函数:

step1;在main方法在加入

beego.ErrorController(&controllers.ErrorController{})

在这里插入图片描述

step2:新建一个Error控制器

package controllersimport "github.com/astaxie/beego"/**  该控制器处理页面错误请求 */type ErrorController struct {	beego.Controller}func (c *ErrorController) Error401() {	c.Data["content"] = "未经授权,请求要求验证身份"	c.TplName="error/401.tpl"}func (c *ErrorController) Error403() {	c.Data["content"] = "服务器拒绝请求"	c.TplName="error/403.tpl"}func (c *ErrorController) Error404() {	c.Data["content"] = "很抱歉您访问的地址或者方法不存在"	c.TplName="error/404.tpl"}func (c *ErrorController) Error500() {	c.Data["content"] = "server error"	c.TplName = "error/500.tpl"}func (c *ErrorController) Error503() {	c.Data["content"] = "服务器目前无法使用(由于超载或停机维护)"	c.TplName = "error/503.tpl"}

step3: 在views新建一个error目录,并在目录添加对应文件,这里以404页面为主

    
404

{ {.content}}

step4:实际运行beego项目进行访问测试

在这里插入图片描述
大功告成

转载地址:http://dqvmz.baihongyu.com/

你可能感兴趣的文章
MySQL中使用IN()查询到底走不走索引?
查看>>
Mysql中使用存储过程插入decimal和时间数据递增的模拟数据
查看>>
MySql中关于geometry类型的数据_空的时候如何插入处理_需用null_空字符串插入会报错_Cannot get geometry object from dat---MySql工作笔记003
查看>>
mysql中出现Incorrect DECIMAL value: '0' for column '' at row -1错误解决方案
查看>>
mysql中出现Unit mysql.service could not be found 的解决方法
查看>>
mysql中出现update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在 dpkg: 处理软件包 mysql-server-8.0的解决方法(全)
查看>>
Mysql中各类锁的机制图文详细解析(全)
查看>>