如何根据环境进行一些URL映射?

发布于 2024-09-03 09:38:42 字数 174 浏览 4 评论 0原文

当得到HTTP状态码500时,我想根据运行环境显示2个不同的页面。

在开发模式,我想显示 stackStrace 页面(如默认的 Grails 500 错误页面),在生产模式,我想显示正式的“内部错误”页面。

这可能吗?我该怎么做?

When getting an HTTP status code 500, I want to display 2 different pages according to the running environment.

In development mode, I want to display a stackStrace page (like the default Grails 500 error page) and in production mode, I want to display a formal "internal error" page.

Is it possible and how can I do that ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹过旳痕迹 2024-09-10 09:38:43

您可以在 UrlMappings.groovy

grails.util.GrailsUtil 中进行环境特定的映射来救援

它并不漂亮,但我认为它会解决您的问题

例如

import grails.util.GrailsUtil

class UrlMappings {
    static mappings = {


        if(GrailsUtil.getEnvironment() == "development") {

             "/$controller/$action?/$id?"{
                constraints {
                    // apply constraints here
                }
            }

            "/"(view:"/devIndex")
            "500"(view:'/error')
        }

        if(GrailsUtil.getEnvironment() == "test") {
            "/$controller/$action?/$id?"{
                constraints {
                    // apply constraints here
                }
            }

            "/"(view:"/testIndex")
            "500"(view:'/error')

        }



        if(GrailsUtil.getEnvironment() == "production") {
            "/$controller/$action?/$id?"{
                constraints {
                    // apply constraints here
                }
            }

            "/"(view:"/prodIndex")
            "500"(view:'/error')

        }
    }
}

You can do environment specific mappings within UrlMappings.groovy

grails.util.GrailsUtil to the rescue

Its not pretty, but I think it will solve your issue

E.g

import grails.util.GrailsUtil

class UrlMappings {
    static mappings = {


        if(GrailsUtil.getEnvironment() == "development") {

             "/$controller/$action?/$id?"{
                constraints {
                    // apply constraints here
                }
            }

            "/"(view:"/devIndex")
            "500"(view:'/error')
        }

        if(GrailsUtil.getEnvironment() == "test") {
            "/$controller/$action?/$id?"{
                constraints {
                    // apply constraints here
                }
            }

            "/"(view:"/testIndex")
            "500"(view:'/error')

        }



        if(GrailsUtil.getEnvironment() == "production") {
            "/$controller/$action?/$id?"{
                constraints {
                    // apply constraints here
                }
            }

            "/"(view:"/prodIndex")
            "500"(view:'/error')

        }
    }
}
久隐师 2024-09-10 09:38:43

可能有一种更干净的方法来做到这一点,但我需要将错误代码映射到控制器并处理那里的逻辑:

class UrlMappings {

   static mappings = {

      "/$controller/$action?/$id?" { constraints {} }

      "/"(view:"/index")

      "403"(controller: "errors", action: "accessDenied")
      "404"(controller: "errors", action: "notFound")
      "405"(controller: "errors", action: "notAllowed")
      "500"(view: '/error')
   }
}

然后创建相应的控制器(grails-app/conf/controllers/ErrorsController.groovy)

import grails.util.Environment

class ErrorsController extends AbstractController {

   def accessDenied = {}

   def notFound = {}

   def notAllowed = {}

   def serverError = {
      if (Environment.current == Environment.DEVELOPMENT) {
         render view: '/error'
      }
      else {
         render view: '/errorProd'
      }
   }
}

:需要在 grails-app/views/errors 中创建相应的 GSP(accessDenied.gsp、notFound.gsp 等)以及新的 grails-app/views/errorProd.gsp。通过路由到所有错误代码的控制器方法,您可以更轻松地在将来向其他错误代码处理程序添加逻辑。

There may be a cleaner way to do this, but I'd got with mapping the error code to a controller and handling the logic there:

class UrlMappings {

   static mappings = {

      "/$controller/$action?/$id?" { constraints {} }

      "/"(view:"/index")

      "403"(controller: "errors", action: "accessDenied")
      "404"(controller: "errors", action: "notFound")
      "405"(controller: "errors", action: "notAllowed")
      "500"(view: '/error')
   }
}

and then create the corresponding controller (grails-app/conf/controllers/ErrorsController.groovy):

import grails.util.Environment

class ErrorsController extends AbstractController {

   def accessDenied = {}

   def notFound = {}

   def notAllowed = {}

   def serverError = {
      if (Environment.current == Environment.DEVELOPMENT) {
         render view: '/error'
      }
      else {
         render view: '/errorProd'
      }
   }
}

You'll need to create the corresponding GSPs in grails-app/views/errors (accessDenied.gsp, notFound.gsp, etc.) and also the new grails-app/views/errorProd.gsp. By routing to a controller method for all error codes you make it easier to add logic to the other error code handlers in the future.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文