Search This Blog

Showing posts with label Struts. Show all posts
Showing posts with label Struts. Show all posts

Friday, April 18, 2008

How to handle duplicate submits in Struts?

The duplicate form submission occurs-When a user clicks the Submit button more than once before the response is sent back or- When a client accesses a view by returning to a previously bookmarked page.It may result in inconsistent transactions and must be avoided.In Struts this problem can be handled by using the saveToken() and isTokenValid() methods of Action class. saveToken() method creates a token (a unique string) and saves that in the user's current session, while isTokenValid() checks if the token stored in the user's current session is the same as that was passed as the request parameter.It can be done by loading JSP through an Action and before loading the JSP call saveToken() to save the token in the user session. When the form is submitted, check the token against that in the session by calling isTokenValid

Thursday, April 17, 2008

How you will handle errors and exceptions in Struts?

An efficient error and exception handling makes an application behave gracefully under abnormal conditions.Struts has errors and exception handling done in different ways.The form validations using Struts require a proper mechanism.For handling errors in Struts,it has two objects ActionError and ActionErrors.Whenever a form is submitted then cotroller receives request and then create ActionForm object which calls reset() method and stores ActionForm object to required scope and then it loads ActionForm object from request and calls validate() method.If validate method fails then errors are displayed on the form itself through html:errors tags.
Exception Handling can be done in following ways:

-try-catch block within -Using declarative exception handling.In struts-config.xml we can declare on which type of exception, a request should be redirected to.Use Global Exceptions tag in struts-config.xml

global-exceptions

"<"exception key="errors.MyException" type="java.lang.MyException" path="/myExcption.jsp"/ ">"

So whenever MyException occurs then Struts framework will display 'myException.jsp' page.The interpretation of this is that if MyException is caught by Struts' ActionServlet then it should redirect to myExcption.jsp. The key is as usual a pointer to the message resource file.