Tomcat 404 Page With Spring Form - spring

In my web.xml page I have the 404 error mapped to a jsp page as:
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/jsp/errorPages/error404.jsp</location>
</error-page>
The redirect on 404 works fine. I get to error404.jsp. The problem is that I have a spring form on the page (a search form that is on EVERY page) which causes errors because the target object is not in the model. I get the following exception:
Caused by: java.lang.IllegalStateException: Neither BindingResult nor
plain target object for bean name 'searchCriteria' available as request attribute
I'd like to have this search form on every page, including the error pages. Is there any way to do this? I realize that 404 as I have it configured above doesn't go to a servlet... so how do I get the request attribute into the model?

One solution is to code the search form as plain HTML in your JSP/template, so that you remove any dependency on the form binding object. For a simple search form with a text input and submit button, this should not be a problem.

The way to do this is to map the error pages to a servlet. Within the servlet you can add things to your model just like you can in any other servlet. To map error pages to a servlet, add something like the following to your web.xml file:
<error-page>
<error-code>404</error-code>
<location>/error/generalError</location>
</error-page>
where "error" is the servlet name and "generalError" is the request mapping.

Related

org.omnifaces.EXCEPTION_TYPES_TO_UNWRAP doesn't work for non-ajax requests

I use FullAjaxExceptionHandlerFactory to handle exceptions during both ajax and non-ajax requests.
The OmniFaces showcase page says
The FullAjaxExceptionHandler will transparently handle exceptions during ajax requests exactly the same way as exceptions during synchronous (non-ajax) requests.
I noticed that if an exception happened during a synchronous request, findExceptionRootCause (the method that actually does unwrapping) doesn't get called. Therefore, the rules defined in the web.xml aren't being applied since they rely on FullAjaxExceptionHandler unwrapping.
Does it mean I need to extend the FullAjaxExceptionHandler or is there something I am missing?
Stack trace :
(non-ajax request)
javax.servlet.ServletException:
Caused by: javax.faces.view.facelets.TagAttributeException
Caused by: javax.el.ELException
Caused by: xxx.MyException
web.xml:
<context-param>
<param-name>org.omnifaces.EXCEPTION_TYPES_TO_UNWRAP</param-name>
<param-value>javax.servlet.ServletException,javax.faces.view.facelets.TagAttributeException,javax.el.ELException</param-value>
</context-param>
...
<error-page>
<exception-type>xxx.MyException</exception-type>
<location>/xxx/page-not-found.xhtml</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/xxx/internal-server-error.xhtml</location>
</error-page>
*I remember ELException may be omitted since it will be included anyway. For the sake of certainty, I included every type preceded my exception in the stack trace.
Result:
/xxx/internal-server-error.xhtml is shown
Expected:
/xxx/page-not-found.xhtml is shown
Update:
For the exception mentioned above, if I define <error-page> like
<error-page>
<exception-type>javax.faces.view.facelets.TagAttributeException</exception-type>
<location>/blueglue/templates/error/page-not-found.xhtml</location>
</error-page>
or
<error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/blueglue/templates/error/page-not-found.xhtml</location>
</error-page>
I will get the expected outcome - the 404 page.
For javax.el.ELException and xxx.MyException, the exception results in the 500 page which isn't what I am expecting.
I use FullAjaxExceptionHandler to handle exceptions during both ajax and non-ajax requests.
The FullAjaxExceptionHandler does not handle exceptions during non-ajax requests. Its sole purpose is to handle exceptions during ajax requests the same way as non-ajax requests (namely, showing an error page defined in web.xml).
Further in the showcase page which you linked you can find this section:
Normal requests
Note that the FullAjaxExceptionHandler does not deal with normal (non-ajax) requests at all. To properly handle JSF and EL exceptions on normal requests as well, you need an additional FacesExceptionFilter. This will extract the root cause from a wrapped FacesException and ELException before delegating the ServletException further to the container (the container will namely use the first root cause of ServletException to match an error page by exception in web.xml).
So, all you need to do is to install the FacesExceptionFilter in order to get the same unwrapping behavior as FullAjaxExceptionHandler.

How to redirect from error-page preRenderView in JSF 2.2?

I wanted to upgrade JSF 2.1 (myfaces) to 2.2 in my application. I just changed two myfaces....jar files in TomEE/lib folder (TomEE-1.7.5). Everything seems to work well except one thing. I have an error page configured in web.xml:
<error-page>
<error-code>500</error-code>
<location>/error/500.jsf</location>
</error-page>
In order to catch error in case of ajax post, omnifaces' FullAjaxExceptionHandlerFactory is declared in faces-config.xml:
<factory>
<exception-handler-factory>org.omnifaces.exceptionhandler.FullAjaxExceptionHandlerFactory</exception-handler-factory>
</factory>
In error page there is a preRenderView function, in which there is a conditional redirect. In JSF 2.1 it was working perfectly. But with JSF 2.2 in case of redirection, because of some other exception I get this message:
Oops!
A problem occurred during processing the ajax request. Subsequently,
another problem occurred during processing the error page which should
inform you about that problem.
The other exception in log:
SEVERE: FullAjaxExceptionHandler: Well, another exception occurred
during rendering error page '/error/500.jsf'. Trying to render a
hardcoded error page now. java.lang.IllegalStateException: Cannot
change buffer size after data has been written
I tried to use something other event handler instead of preRenderView (I thought maybe the redirection should be made at an earlier point of the lifecycle(?)). With postAddToView event the result is the same, other events are not called at all.
Also instead of f:event I tried to use f:viewAction of JSF 2.2, but in case of ajax post none of the possible viewAction events is called on the error page (onPostback="true" parameter also did not help).
Questions:
Why I get the other exception on error page in case of redirection? (There was no any problem with myfaces 2.1)
Why viewAction function is not called at all on error page? (when the error resulted to go to the error page occured in an ajax request)
So how to make (conditional) redirect from error-page with JSF (myfaces) 2.2?

spring - how to deny the direct access to servlet mapping(POST method used) from the url

For example, I have a servlet mapping /servlet, and actually it is requested by a form using POST Method. If I type in the mapping directly without using the form in the browser, the server will give me a 405 page.
HTTP Status 405 - Request method 'GET' not supported
What I want to do is to catch this exception using spring security. I was thinking to restrict this servelet request by using POST method only so that the direct url access will be denied. However, I searched on the internet but I didn't find an answer. Please guide me how to do this, thanks.
If you want to override default error message you should create your own error page and map it into web.xml like this
<error-page>
<error-code>405</error-code>
<location>/internal-server-error.htm</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/405.html</location>
</error-page>
and your 405.html will contain your personalized error message for the user.
Read more: http://mrbool.com/how-to-create-error-page-in-j2ee/26770

HTTP Status 500 customise through webapp error page

I have customized error code 403 (client error) through webapp i.e. adding entry in web.xml as below which works fine.
<error-page>
<error-code>403</error-code>
<location>/access-denied.xhtml</location>
</error-page>
I try to customise error code 500 (server error) with the above approach as below.
<error-page>
<error-code>500</error-code>
<location>/error-500.xhtml</location>
</error-page>
but this doesn’t work (getting jboss's original error message for error code 500 instead of error-500.xhtml) and I have no clue why, so I am left with only option to customize it through apache httpd and vhost config.
Any idea why this is not working?
The reason for not working is because the webapp has implemented ExceptionMapper that catches the exception instead of letting the custom error page to be resolved from web.xml
The resolution would be to remove the ExceptionMapper impl class.

how to get Requested URL on custom error page

I got next problem with my application.
I have to make my own 404 page and I'd like to write there "Page {requestedPage} not found" but I have no idea how to get this value.
I created Controller with RequestMapping
#RequestMapping("404")
public String handle404(Locale locale, Model model ,HttpServletResponse response, HttpServletRequest request){
...
}
and set this page in web.xml as 404 error page
<error-page>
<error-code>404</error-code>
<location>/404</location>
</error-page>
I found the same question on spring forum
http://forum.springsource.org/showthread.php?87670-Knowing-the-requested-URL-during-a-404
but without answer
I looked for all HttpSerletRequest methods and fields but not found anything what can help.
Does anyone know how to do this?

Resources