Show setup after "Context initialization failed"
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:
开发者_Python百科 <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.
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.
精彩评论