You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Bhaarat Sharma <bh...@gmail.com> on 2009/07/30 20:39:10 UTC

common error page in struts2 application

Hi,
What is the way to provide a common error page in a struts2 application? and
automatically redirect to the error page when errors occur?

Thanks

Re: common error page in struts2 application

Posted by Greg Lindholm <gr...@gmail.com>.
    <global-exception-mappings>
      <exception-mapping exception="java.lang.Exception" result="error" />
    </global-exception-mappings>


On Thu, Jul 30, 2009 at 2:55 PM, Bhaarat Sharma <bh...@gmail.com> wrote:

> how do we map exceptions to a global result?
>
> On Thu, Jul 30, 2009 at 2:41 PM, Dave Newton <ne...@yahoo.com>
> wrote:
>
> > Bhaarat Sharma wrote:
> >
> >> What is the way to provide a common error page in a struts2 application?
> >> and
> >> automatically redirect to the error page when errors occur?
> >>
> >
> > Map exceptions to a global result.
> >
> > Dave
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
>

Re: common error page in struts2 application

Posted by Bhaarat Sharma <bh...@gmail.com>.
ahhh i was not 'thrwoing' e after catching it >_<

thansk

On Fri, Jul 31, 2009 at 6:55 AM, Bhaarat Sharma <bh...@gmail.com> wrote:

> ....but if i am catching it, it is not showing my the common error page i
> made..
>
>
> On Fri, Jul 31, 2009 at 4:29 AM, <ma...@yahoo.com> wrote:
>
>> I think you can catch it, then do log.error(e.getMessage(),e); and then
>> throw e;
>>
>>
>>
>> ________________________________
>> From: Bhaarat Sharma <bh...@gmail.com>
>> To: Struts Users Mailing List <us...@struts.apache.org>
>> Sent: Friday, July 31, 2009 5:03:34 AM
>> Subject: Re: common error page in struts2 application
>>
>> Sorry I am a little confused.  Basically I need to be able to log the
>> message by doing log.error(e.getMessage(), e);  However, if i catch the
>> error and put it in the catch block then the global error page does not
>> come
>> up (I am assuming because the exception is caught?)
>>
>> If I do not catch the exception, then the error page shows up, however, I
>> have no way to do log.error(e.getMessage(), e);
>>
>> After reading,
>> http://struts.apache.org/2.0.6/docs/exception-configuration.html I though
>> I
>> can have a chainAction so something like.
>> <package...>
>>        <global-results>
>>               <result name="Exception" type="chain">
>>                  <param name="actionName">ErrorPage</param>
>>                 <param name="namespace">/error</param>
>>               </result>
>>        </global-results>
>>
>>        <global-exception-mappings>
>>            <exception-mapping exception="java.lang.Exception"
>> result="Exception"/>
>>        </global-exception-mappings>
>>        <action name="selectionPage" class="reports.ReportSelection">
>>            <result>/reports/SelectionPage.jsp</result>
>>        </action>
>>
>> </package>
>> <package name="secure"  namespace="/error">
>>    <action name="ErrorPage" class="com.myErrorClass">
>>        <result>errorpage.jsp</result>
>>    </action>
>> </package>
>> So finally, if i do not catch the error, the control will go to
>> com.myErrorClass.  Will I be able to do log.error(e.getMessage(), e); in
>> that class?? I dont think it will be available as originally the error was
>> thrown in reports.ReportSelection class.
>>
>> I'd appreciate any help to guide me in right direction.
>>
>> Thanks
>>
>> On Thu, Jul 30, 2009 at 11:40 PM, Bhaarat Sharma <bhaarat.s@gmail.com
>> >wrote:
>>
>> > will the global exception not recognize the exception if I am catching
>> it??
>> > I need to catch it because I want to put it in the logs.  However, in my
>> > example, if i am catching the exception then I am seeing that the error
>> page
>> > defined with global-results is not coming up.
>> > do the global exception mappings only work for 'non caught' exceptions?
>> >
>> >
>> > On Thu, Jul 30, 2009 at 4:07 PM, Dave Newton <newton.dave@yahoo.com
>> >wrote:
>> >
>> >> Bhaarat Sharma wrote:
>> >>
>> >>> Is there a way to just add [exception mapping] somewhere so that all
>> >>> 20 package mappings see it?
>> >>>
>> >>> most of the packing are using struts-default
>> >>>
>> >>
>> >> You could create your own base package and extend from that. Package
>> >> extension is also documented on the Struts 2 wiki (briefly):
>> >>
>> >> http://struts.apache.org/2.1.6/docs/package-configuration.html
>> >>
>> >>
>> >> Dave
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> >> For additional commands, e-mail: user-help@struts.apache.org
>> >>
>> >>
>> >
>>
>
>

Re: common error page in struts2 application

Posted by Bhaarat Sharma <bh...@gmail.com>.
....but if i am catching it, it is not showing my the common error page i
made..

On Fri, Jul 31, 2009 at 4:29 AM, <ma...@yahoo.com> wrote:

> I think you can catch it, then do log.error(e.getMessage(),e); and then
> throw e;
>
>
>
> ________________________________
> From: Bhaarat Sharma <bh...@gmail.com>
> To: Struts Users Mailing List <us...@struts.apache.org>
> Sent: Friday, July 31, 2009 5:03:34 AM
> Subject: Re: common error page in struts2 application
>
> Sorry I am a little confused.  Basically I need to be able to log the
> message by doing log.error(e.getMessage(), e);  However, if i catch the
> error and put it in the catch block then the global error page does not
> come
> up (I am assuming because the exception is caught?)
>
> If I do not catch the exception, then the error page shows up, however, I
> have no way to do log.error(e.getMessage(), e);
>
> After reading,
> http://struts.apache.org/2.0.6/docs/exception-configuration.html I though
> I
> can have a chainAction so something like.
> <package...>
>        <global-results>
>               <result name="Exception" type="chain">
>                  <param name="actionName">ErrorPage</param>
>                 <param name="namespace">/error</param>
>               </result>
>        </global-results>
>
>        <global-exception-mappings>
>            <exception-mapping exception="java.lang.Exception"
> result="Exception"/>
>        </global-exception-mappings>
>        <action name="selectionPage" class="reports.ReportSelection">
>            <result>/reports/SelectionPage.jsp</result>
>        </action>
>
> </package>
> <package name="secure"  namespace="/error">
>    <action name="ErrorPage" class="com.myErrorClass">
>        <result>errorpage.jsp</result>
>    </action>
> </package>
> So finally, if i do not catch the error, the control will go to
> com.myErrorClass.  Will I be able to do log.error(e.getMessage(), e); in
> that class?? I dont think it will be available as originally the error was
> thrown in reports.ReportSelection class.
>
> I'd appreciate any help to guide me in right direction.
>
> Thanks
>
> On Thu, Jul 30, 2009 at 11:40 PM, Bhaarat Sharma <bhaarat.s@gmail.com
> >wrote:
>
> > will the global exception not recognize the exception if I am catching
> it??
> > I need to catch it because I want to put it in the logs.  However, in my
> > example, if i am catching the exception then I am seeing that the error
> page
> > defined with global-results is not coming up.
> > do the global exception mappings only work for 'non caught' exceptions?
> >
> >
> > On Thu, Jul 30, 2009 at 4:07 PM, Dave Newton <newton.dave@yahoo.com
> >wrote:
> >
> >> Bhaarat Sharma wrote:
> >>
> >>> Is there a way to just add [exception mapping] somewhere so that all
> >>> 20 package mappings see it?
> >>>
> >>> most of the packing are using struts-default
> >>>
> >>
> >> You could create your own base package and extend from that. Package
> >> extension is also documented on the Struts 2 wiki (briefly):
> >>
> >> http://struts.apache.org/2.1.6/docs/package-configuration.html
> >>
> >>
> >> Dave
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> For additional commands, e-mail: user-help@struts.apache.org
> >>
> >>
> >
>

Re: common error page in struts2 application

Posted by ma...@yahoo.com.
I think you can catch it, then do log.error(e.getMessage(),e); and then throw e;



________________________________
From: Bhaarat Sharma <bh...@gmail.com>
To: Struts Users Mailing List <us...@struts.apache.org>
Sent: Friday, July 31, 2009 5:03:34 AM
Subject: Re: common error page in struts2 application

Sorry I am a little confused.  Basically I need to be able to log the
message by doing log.error(e.getMessage(), e);  However, if i catch the
error and put it in the catch block then the global error page does not come
up (I am assuming because the exception is caught?)

If I do not catch the exception, then the error page shows up, however, I
have no way to do log.error(e.getMessage(), e);

After reading,
http://struts.apache.org/2.0.6/docs/exception-configuration.html I though I
can have a chainAction so something like.
<package...>
        <global-results>
               <result name="Exception" type="chain">
                  <param name="actionName">ErrorPage</param>
                 <param name="namespace">/error</param>
               </result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception"
result="Exception"/>
        </global-exception-mappings>
        <action name="selectionPage" class="reports.ReportSelection">
            <result>/reports/SelectionPage.jsp</result>
        </action>

</package>
<package name="secure"  namespace="/error">
    <action name="ErrorPage" class="com.myErrorClass">
        <result>errorpage.jsp</result>
    </action>
</package>
So finally, if i do not catch the error, the control will go to
com.myErrorClass.  Will I be able to do log.error(e.getMessage(), e); in
that class?? I dont think it will be available as originally the error was
thrown in reports.ReportSelection class.

I'd appreciate any help to guide me in right direction.

Thanks

On Thu, Jul 30, 2009 at 11:40 PM, Bhaarat Sharma <bh...@gmail.com>wrote:

> will the global exception not recognize the exception if I am catching it??
> I need to catch it because I want to put it in the logs.  However, in my
> example, if i am catching the exception then I am seeing that the error page
> defined with global-results is not coming up.
> do the global exception mappings only work for 'non caught' exceptions?
>
>
> On Thu, Jul 30, 2009 at 4:07 PM, Dave Newton <ne...@yahoo.com>wrote:
>
>> Bhaarat Sharma wrote:
>>
>>> Is there a way to just add [exception mapping] somewhere so that all
>>> 20 package mappings see it?
>>>
>>> most of the packing are using struts-default
>>>
>>
>> You could create your own base package and extend from that. Package
>> extension is also documented on the Struts 2 wiki (briefly):
>>
>> http://struts.apache.org/2.1.6/docs/package-configuration.html
>>
>>
>> Dave
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>

Re: common error page in struts2 application

Posted by Greg Lindholm <gr...@gmail.com>.
What I do, instead of chaining the global error result to an error action,
is I use ognl in my error.jsp page to call a method and pass the exception
so it can be logged etc.  In my base action class I have a
notifyAdmin(Exception) method.

In error.jsp:
<%-- Dummy expression to notify the admin --%>
<s:if test="notifyAdmin(exception)"></s:if>


On Fri, Jul 31, 2009 at 12:03 AM, Bhaarat Sharma <bh...@gmail.com>wrote:

> Sorry I am a little confused.  Basically I need to be able to log the
> message by doing log.error(e.getMessage(), e);  However, if i catch the
> error and put it in the catch block then the global error page does not
> come
> up (I am assuming because the exception is caught?)
>
> If I do not catch the exception, then the error page shows up, however, I
> have no way to do log.error(e.getMessage(), e);
>
> After reading,
> http://struts.apache.org/2.0.6/docs/exception-configuration.html I though
> I
> can have a chainAction so something like.
> <package...>
>        <global-results>
>               <result name="Exception" type="chain">
>                  <param name="actionName">ErrorPage</param>
>                 <param name="namespace">/error</param>
>               </result>
>        </global-results>
>
>        <global-exception-mappings>
>            <exception-mapping exception="java.lang.Exception"
> result="Exception"/>
>        </global-exception-mappings>
>        <action name="selectionPage" class="reports.ReportSelection">
>            <result>/reports/SelectionPage.jsp</result>
>        </action>
>
> </package>
> <package name="secure"  namespace="/error">
>    <action name="ErrorPage" class="com.myErrorClass">
>        <result>errorpage.jsp</result>
>    </action>
> </package>
> So finally, if i do not catch the error, the control will go to
> com.myErrorClass.  Will I be able to do log.error(e.getMessage(), e); in
> that class?? I dont think it will be available as originally the error was
> thrown in reports.ReportSelection class.
>
> I'd appreciate any help to guide me in right direction.
>
> Thanks
>

Re: common error page in struts2 application

Posted by Bhaarat Sharma <bh...@gmail.com>.
Sorry I am a little confused.  Basically I need to be able to log the
message by doing log.error(e.getMessage(), e);  However, if i catch the
error and put it in the catch block then the global error page does not come
up (I am assuming because the exception is caught?)

If I do not catch the exception, then the error page shows up, however, I
have no way to do log.error(e.getMessage(), e);

After reading,
http://struts.apache.org/2.0.6/docs/exception-configuration.html I though I
can have a chainAction so something like.
<package...>
        <global-results>
               <result name="Exception" type="chain">
                  <param name="actionName">ErrorPage</param>
                 <param name="namespace">/error</param>
               </result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception"
result="Exception"/>
        </global-exception-mappings>
        <action name="selectionPage" class="reports.ReportSelection">
            <result>/reports/SelectionPage.jsp</result>
        </action>

</package>
<package name="secure"  namespace="/error">
    <action name="ErrorPage" class="com.myErrorClass">
        <result>errorpage.jsp</result>
    </action>
</package>
So finally, if i do not catch the error, the control will go to
com.myErrorClass.  Will I be able to do log.error(e.getMessage(), e); in
that class?? I dont think it will be available as originally the error was
thrown in reports.ReportSelection class.

I'd appreciate any help to guide me in right direction.

Thanks

On Thu, Jul 30, 2009 at 11:40 PM, Bhaarat Sharma <bh...@gmail.com>wrote:

> will the global exception not recognize the exception if I am catching it??
> I need to catch it because I want to put it in the logs.  However, in my
> example, if i am catching the exception then I am seeing that the error page
> defined with global-results is not coming up.
> do the global exception mappings only work for 'non caught' exceptions?
>
>
> On Thu, Jul 30, 2009 at 4:07 PM, Dave Newton <ne...@yahoo.com>wrote:
>
>> Bhaarat Sharma wrote:
>>
>>> Is there a way to just add [exception mapping] somewhere so that all
>>> 20 package mappings see it?
>>>
>>> most of the packing are using struts-default
>>>
>>
>> You could create your own base package and extend from that. Package
>> extension is also documented on the Struts 2 wiki (briefly):
>>
>> http://struts.apache.org/2.1.6/docs/package-configuration.html
>>
>>
>> Dave
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>

Re: common error page in struts2 application

Posted by Bhaarat Sharma <bh...@gmail.com>.
will the global exception not recognize the exception if I am catching it??
I need to catch it because I want to put it in the logs.  However, in my
example, if i am catching the exception then I am seeing that the error page
defined with global-results is not coming up.
do the global exception mappings only work for 'non caught' exceptions?

On Thu, Jul 30, 2009 at 4:07 PM, Dave Newton <ne...@yahoo.com> wrote:

> Bhaarat Sharma wrote:
>
>> Is there a way to just add [exception mapping] somewhere so that all
>> 20 package mappings see it?
>>
>> most of the packing are using struts-default
>>
>
> You could create your own base package and extend from that. Package
> extension is also documented on the Struts 2 wiki (briefly):
>
> http://struts.apache.org/2.1.6/docs/package-configuration.html
>
>
> Dave
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: common error page in struts2 application

Posted by Dave Newton <ne...@yahoo.com>.
Bhaarat Sharma wrote:
> Is there a way to just add [exception mapping] somewhere so that all
> 20 package mappings see it?
> 
> most of the packing are using struts-default

You could create your own base package and extend from that. Package
extension is also documented on the Struts 2 wiki (briefly):

http://struts.apache.org/2.1.6/docs/package-configuration.html

Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: common error page in struts2 application

Posted by Bhaarat Sharma <bh...@gmail.com>.
one last question:
lets say my mapping comes out to be this

<global-results>
<result name="error">/chapterFour/Error.jsp</result>
</global-results>

<global-exception-mappings>
<exception-mapping exception="java.lang.Exception"
result="error" />
</global-exception-mappings>

so will i add that in each package in my struts.xml.  I have over 20 right
now.

Is there a way to just add it somewhere so that all 20 package mappings see
it?

most of the packing are using struts-default

On Thu, Jul 30, 2009 at 3:26 PM, Bhaarat Sharma <bh...@gmail.com> wrote:

> thanks
>
>
> On Thu, Jul 30, 2009 at 3:21 PM, Dave Newton <ne...@yahoo.com>wrote:
>
>> Bhaarat Sharma wrote:
>>
>>> how do we map exceptions to a global result?
>>>
>>
>> You may not know about the Struts 2 documentation wiki that has a page
>> covering exception configuration:
>>
>> http://struts.apache.org/2.1.6/docs/exception-configuration.html
>>
>>
>> Dave
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>

Re: common error page in struts2 application

Posted by Bhaarat Sharma <bh...@gmail.com>.
thanks

On Thu, Jul 30, 2009 at 3:21 PM, Dave Newton <ne...@yahoo.com> wrote:

> Bhaarat Sharma wrote:
>
>> how do we map exceptions to a global result?
>>
>
> You may not know about the Struts 2 documentation wiki that has a page
> covering exception configuration:
>
> http://struts.apache.org/2.1.6/docs/exception-configuration.html
>
>
> Dave
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: common error page in struts2 application

Posted by Dave Newton <ne...@yahoo.com>.
Bhaarat Sharma wrote:
> how do we map exceptions to a global result?

You may not know about the Struts 2 documentation wiki that has a page 
covering exception configuration:

http://struts.apache.org/2.1.6/docs/exception-configuration.html

Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: common error page in struts2 application

Posted by Bhaarat Sharma <bh...@gmail.com>.
how do we map exceptions to a global result?

On Thu, Jul 30, 2009 at 2:41 PM, Dave Newton <ne...@yahoo.com> wrote:

> Bhaarat Sharma wrote:
>
>> What is the way to provide a common error page in a struts2 application?
>> and
>> automatically redirect to the error page when errors occur?
>>
>
> Map exceptions to a global result.
>
> Dave
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: common error page in struts2 application

Posted by Wes Wannemacher <we...@wantii.com>.
On Thu, Jul 30, 2009 at 2:41 PM, Dave Newton<ne...@yahoo.com> wrote:
> Bhaarat Sharma wrote:
>>
>> What is the way to provide a common error page in a struts2 application?
>> and
>> automatically redirect to the error page when errors occur?
>
> Map exceptions to a global result.
>
> Dave
>


Make sure you set error-page in your web.xml as well. Exceptions
mapped to global results are good for errors that occur in app code,
but you should also consider things like 404s.

-Wes
-- 
Wes Wannemacher

Head Engineer, WanTii, Inc.
Need Training? Struts, Spring, Maven, Tomcat...
Ask me for a quote!

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: common error page in struts2 application

Posted by Dave Newton <ne...@yahoo.com>.
Bhaarat Sharma wrote:
> What is the way to provide a common error page in a struts2 application? and
> automatically redirect to the error page when errors occur?

Map exceptions to a global result.

Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org