显示“上下文初始化失败”后的设置

发布于 2024-11-03 15:47:45 字数 570 浏览 1 评论 0原文

我现在正在寻找以下问题的答案几天:

我有一个 Spring 2.5 Web 应用程序,如果 spring 上下文的初始化失败,我想显示某个设置屏幕。在此设置屏幕中,他们可以查看服务器未启动的原因,并可能进行一些更改(上传新的 config.properties 文件),

但是我如何以智能方式实现此操作? Spring 是否已经有了类似的东西,或者我是否需要扩展 ContextLoader?

我在 web.xml 中尝试了如下操作:但这似乎不起作用:

   <error-page>
        <error-code>404</error-code>
        <location>/public/setup.jsp</location>
    </error-page>

解决方案:

我从默认的 web.xml 开始,设置完成后,我将 web.xml 替换为正确的“应用程序”web.xml。由于 web.xml 被替换,服务器将重新启动。这很好用。再次感谢您的回答。

I'm looking now for a couple of days for the answer to the following question:

I have a Spring 2.5 webapplication and I want to show a certain setup screen if the initialization of the spring context has failed. In this setup screen they can look why the server doesn't startup and maybe make some changes (upload new config.properties file)

But how can I implemented this on a smart way? Has Spring already something like this or do I need to extend the ContextLoader for example?

I tried something in the web.xml like this: but this doesn't seems to work:

   <error-page>
        <error-code>404</error-code>
        <location>/public/setup.jsp</location>
    </error-page>

Solution:

I start with a default web.xml and after the setup is done I replace the web.xml with the right 'application' web.xml. Because the web.xml is replaced the servers restarts. This works great. Thanks again for your answers.

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

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

发布评论

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

评论(1

夏见 2024-11-10 15:47:45

这里有三个想法:

  • 修改上下文加载器以捕获异常,并将 servlet/映射添加到容器,将所有相关映射重定向到动态加载的 servlet。查看有关如何创建动态 servlet 的堆栈溢出线程:动态添加一个 servlet 到 servletConfig
  • 或者,您可以定义一个标准 servlet 来处理所有请求并将它们转发到配置页面。然后,您可以使用一个 spring bean,在完成初始化后从上下文中删除该 servlet 和映射(您可能希望将该代码放入 spring bean 的 postInitalize 挂钩中。)
  • 您还可以尝试创建一个侦听器来检查是否存在有效的应用程序上下文并删除“默认”映射/servlet 退出。

我不认为存在用于添加/删除 servlet 以及从容器映射的标准机制。但看起来大多数容器都有一些 API 可以做到这一点。

还有第三种方法,正如您所暗示的。假设如果发生 404 错误,则 servlet 无法启动。如果你沿着这条路线走下去,我想你会遇到 404 错误可能发生的问题,因为用户粗暴地手指了 URL。

Here are three ideas:

  • Modify the context loader to catch the exception, and add a servlet/mapping to the container the that redirects all the relevant mappings to the dynamically loaded servlet. Check out this stack overflow thread of instructions on how to create a dynamic servlet: Dynamically add a servlet to the servletConfig
  • Alternatively you could have a standard servlet defined that handles all requests and forwards them to the config page. You can then have a spring bean that remove thats servlet and mapping from the context when it's finished initalizing (you might want to put that code in the postInitalize hook of a spring bean.)
  • You could also try creating a listener that checks to see if a valid application context exists and removes a "default" mapping /servlet exits.

I don't think there are standard mechanisms for adding/removing servlets and mapping from the container. But it looks like most containers have some APIs that do this.

There is a third way, which you were hinting. Which is to assume that if a 404 error occured then servlet failed to start. If you go down that route I think you will run into the issue that 404 errors can occurs just because the user fat fingered the url.

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