Tutorials : Strictly Struts :

Exception Handling Strategy

Whenever your application and the Action classes deal with networked resources, there is a chance of exceptions getting thrown. There are two ways you can handle the exceptions within Struts. One is to catch all those exceptions explicitly within the Action classes and convert them to the appropriate error message using the Struts ActionError class. This works fine, but Struts 1.1 also offers a declarative exception handling mechanism, which is very simple to use.

The advantage of declarative exception handling is that whenever the layers beneath the Struts layer decides to change the exception handling strategy and start throwing different exceptions, you only need to change the configuration file to handle any new exception being thrown. Here is an example how you can do that. Let us say our Student Registration application throws a StudentAlreadyExists exception when you attempt to create a new Student with an already existing user_id. You can configure the StudentAction to handle such exceptions in a declarative way:

<action    path="/studentAction"
               type="StudentAction"
               name="studentForm"
               parameter="methodToCall" 
               scope="request"
               validate="true"
               input="/student.jsp">
 
      
      <exception key=”global.error”
                         type=”StudentAlreadyExistsException”
                        path=”error.jsp”>
       </exception>
       <forward name="success"              path="/success.jsp"/>
    </action>
 

When we declare an <exception> element within the Action class configuration, the <exception> configuration is specific to that Action class. You can also declare global <exception> elements for the exceptions valid across multiple Action classes. In this configuration, we catch a StudentAlreadyExistsException and forwards the application to the error.jsp page when the StudentAction class throws the exception. You can also provide your custom ExceptionHandler class into this declarative exception handling. You will only use that when you need subtle control over what you want to do with the exception being thrown. For example, within an Exceptionhandler class you might want to log the exceptions to a different file for future reference.

Multi page form validation

Multi page forms are often a bit of confusion area for new Struts developers. The fact is often that you want to declare a single form bean that contains all the fields from all the pages comprising the multi-page form and give the form bean a session scope. The session scope ensures that data is retained till you reach the final page and submit the form. There is nothing wrong with it as the data spread across different pages belong to the same logical area. The problem comes when you try to validate the form part by part. If you have a single Action class called from all the pages, then you might want to take two approaches:

  1. Within the Action class, you may first check if a particular form element is a part of the request (this will only happen if the field is present in the actual page) and has a non-null value and then go on performing other validation logics.
  2. You might decide to use the sophisticated declarative validation with Struts Validator framework.

Now we will discuss how you can implement the multi-page form validation using the declarative validation framework. Let us take that the Student registration process comprised of two forms: Form 1 has the name and user_id; Form 2 has the teacher and the department information, also let us assume that the name and teacher are the mandatory information. In order to use the declarative validation, what you need is to use a hidden variable called "page" in your forms and give them proper values. So your Form 1 and Form 2 both will have a hidden variable called "page" with values "1" and "2" respectively. Now you will define the validation rules within the Struts validation.xml file in the following manner.

 
<formset>
  <form name="studentForm">
           <field property="name" depends="required" page="1"/>
           <field property="teacher"   depends="required" page="2"/>
       </form>  
</formset>

Note: For developers who are using DynaActionForm, you must declare a form-property "page" with type java.lang.Integer. If you also define the hidden variable in the form JSP pages with corresponding values set, the Struts framework will automatically populate the "page" property. The Struts framework expects this parameter to be of type Integer, otherwise you will expect a ClassCastException.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.