How you will handle errors and exceptions using Struts

Showing Answers 1 - 12 of 12 Answers

abhishek roy

  • Jun 11th, 2005
 

there are various ways to handle exception: 
1) To handle errors server side validation can be used using ActionErrors classes can be used. 
2) The exceptions can be wrapped across different layers to show a user showable exception. 
3)using validators

  Was this answer useful?  Yes

Rahul Pundhir

  • Jul 14th, 2005
 

Can you suggest me some good Tutorial for Struts available on the Net. 
 
With Thanks And Regards 
Rahul Pundhir

  Was this answer useful?  Yes

me

  • Jul 15th, 2005
 

http://struts.apache.org/userGuide/index.html 
http://www.coreservlets.com/Apache-Struts-Tutorial/

  Was this answer useful?  Yes

Anant

  • Oct 4th, 2005
 

Strust provides ActionError object. Store each error msg by converting them to ActionErrors obj. Then save this obj in the request by calling saveError method of the ActionServlet.In the JSP use tag to show the error msg.

  Was this answer useful?  Yes

abdul khalik

  • May 12th, 2006
 

Hi All

I struts we can handle the exceptions using

1) validations: here we validate inputs and contron will be transfered to the input page of the action as per the validation rule voilation or we can simply validate the values in action class and send the contron on to desired page. 

2) using declarative Exception Handling: in deployment descriptor we can declare on which type of exception request should be redirected to which page.

Waiting for comments.

Thaks

Abdul

  Was this answer useful?  Yes

siva kumar reddy

  • Aug 4th, 2006
 

hi,

to handle the errors:

in struts project to handle the error objet by using ActionError object and to handle the errors by using ActionErrors object.

for suppose

ActionError ae1=new ActionError("err.one");

ActionError ae2=new ActionError("err.two");

Action Errors aes=new ActionErrors();

aes.add(ae1);

aes.add(ae2);

saveErrors(request,aes);//store the errors object in request object

to handle exception:

1)using try and cach blocks

2)using declarative exception handling technique

to handle the exceptions by using global exceptons tag in struts-config.xml

<global-exceptions>

<exception key="gen.err" type="java.lang.ClassNotFoundException"  path="/gen.jsp"/>

  whenever that exception will be came it executes the gen.jsp page.            

cheers

siva

Struts provide a very clean way of handling exceptions via ActionError [v1.0] / ActionMessage [v1.1 onwards] classes.

We can add our message to these classes in our action [wherever you think its appropriate] and then get those messages displayed on the view layer. Sample below:-

in controller....

ActionError ae=new ActionError("start.firstMessage");
ActionMessage am new ActionMessage();

inside view...
<div style="color:red">
         <html:errors />
</div>


Thanks,
Vinay

  Was this answer useful?  Yes

Naresh Tuhania

  • Oct 18th, 2011
 

struts provides a small, but effective exception-handling framework for the applications. There are two approaches available for the exception handling in struts.
1. Declarative:- In this approach the exceptions are defined in the struts-config file and in case of the exception occurs the control is automatically passed to the appropriate error page. The tag is used to define the exception in the struts-config.xml file. The following are the attributes of the tag.
1. Key:- The key defines the key present in MessageResources.properties file to describe the exception occurred.
2. Type:- The class of the exception occurred.
3. Path:- The page where the control is to be followed in case exception occurred.
4. Handler:- The exception handler which will be called before passing the control to the file specified in path attribute
There are following two sections in struts-config.xml where you can define the exceptions.
* With Any Action mapping:- In this approach the action class is identified which may throw the exception like password failure, resource access etc. For example:-

Code
  1. <action path="/exception"

  2.             type="com.visualbuilder.ExampleAction"

  3.             parameter="method"

  4.             input="/index.jsp" >

  5.                 <exception key="error.system" type="java.lang.RuntimeException" path="/index.jsp" />

  6.             </action>

  7.  

* Defining the Exceptions Globally for the struts-config.xml :- If the application control is to pass on a single page for all similar type of exception then the exception can be defined globally. So if in any circumstance the exception occurs the control is passed globally to the exception and control is passed to the error page. For Example:-
Code
  1.  <global-exceptions>

  2.                 <exception key="error.system" type="java.lang.RuntimeException" path="/index.jsp" />

  3.             </global-exceptions>

  4.  


2. Programmatic: - In this approach the exceptions are caught using normal java language try/catch block and instead of showing the exception some meaningful messages are displayed. In this approach the flow of control is also maintained by the programs. The main drawback of the approach is the developer has to write the code for the flow of the application.

nitesh

  • Nov 10th, 2011
 

we can handle exception in two ways declarative exception & programmatic exception handling

  Was this answer useful?  Yes

Dadaso V. pawar

  • May 21st, 2012
 

To handle errors server side validation can be used using ActionErrors classes can be used.
or

If you need to handle an exception in a specific way for a certain action you can use the exception-mapping node within the action node.

Example:

Code
  1. <action name="actionspecificexception" class="org.apache.struts.register.action.Register" method="throwSecurityException">

  2.      <exception-mapping exception="org.apache.struts.register.exceptions.SecurityBreachException"

  3.           result="login" />

  4.       <result>/register.jsp</result>

  5.       <result name="login">/login.jsp</result>

  6.    </action>

  7.  



The above action node from the example applications struts.xml file specifies that if method throwSecurityException throws an uncaught exception of type SecurityBreachException the Struts 2 framework should return a result of login. The login result will cause the users browser to be redirected to login.jsp.

You can see that an action-specific exception mapping will take precedence if the same exception is also mapped globally.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions