Struts 1.2 Tutorial

Struts 1.2 is the most famous (MVC) framework. Now a days, it’s a dead framework. However, with the huge success of Struts 1.x in early days, there are still hundreds of thousand companies are implemented Struts 1.x, and never consider upgrade, so the Struts 1.x is still causing many maintainability.

Struts 1.x is a complete web framework(MVC and, provides complete all the web form components like validator, internalization, error handling, tiles layout and it is easy to implement it. In this tutorials, it provides some basic questions and explanations on using Struts 1.2 MVC framework.

What are form-beans?

This is a bean class extending the ActionForm class. It get values from HTML form created with Struts tags and link with ActionForm member class variables.

 

Types of ActionForm

Static form bean

You need to write a Java class by extending one of the above mentioned Static Forms.

You need to declare the fields same as JSP forms.

You need to write the set/get methods for all the fields.

If you want to validate form data you need to override the validate() method in the Form Bean class.

If you want to reset your form fields ,you need to override the reset() method in the Form Bean class.

Dynamic form bean

Write the field declaration inside the struts configuration file(No need to write Java class with private field and set/get method).

DynamicForms will reset automatically to default values(No need to override reset method).

Dynamic forms will use validation framework for the form validations (No need to override the validate method)

 

What are global forwards?

If some time you have to perform same action again and again. For example

and global forward looks like

What is action Mapping?

If u are submitting Html form with some link in it then what action has to be performed with it

path — value of the path the application maps to the action.

type — The full qualified name of Action class.

name — The name of the <form-bean> element that’s used with the action.

scope – value of the scope.

validate – true or false , if you want to validation for that formbean then the value is “true” otherwise “false”.

What is <message-resources parameter=”ApplicationResources”/> ?

It is used to configure property file with our application

How we can add tiles to our application?

By Adding

<controller processorClass=”org.apache.struts.tiles.TilesRequestProcessor” />

In struts-config.xml

And adding plug in

How can you make your first page as tile?

Define <action path=”/start” forward=”tile.klogin”/> in struts-config.xml

      2. Define these lines in tiles-def-common.xml   — in struts-config.xml file

    3. In path /layouts/kloginLayout.jsp

<tiles:insert attribute=”appBody”/>

How exception are handled in it?

Using try catch

Using tags in struts-config.xml

<exception key=”stockdataBase.error.invalidCurrencyType” path=”/AvailbleCurrency.jsp” type=”Stock.account.illegalCurrencyTypeException”> </exception>

Key: The key represents the key present in MessageResource.properties file to describe the exception.

Type: The class of the exception occurred.

Path: The page where the controls are to be followed is case exception occurred.

How validation can be performed in Struts?

validator-rules.xml file: which contains the default struts pluggable validator definitions. You can add new validation rules by adding an entry in this file. This was the original beauty of struts which makes it highly configurable.

Validation.xml files which contain details regarding the validation routines that are applied to the different Form Beans.

<plug-in className=”org.apache.struts.validator.ValidatorPlugIn”> <set-property property=”pathnames” value=”/WEB-INF/validator-rules.xml,/WEB-INF/validation-highered.xml”/> </plug-in>

What is the difference between validate() and reset()?

Validate() is used to validate the properties after they are populated in the form and this method is called before the data is being passed to action class.It returns action error class instance.

Reset() method is called on each request

Explain Struts MVC flow.

1) A request comes in from a Java Server Page into the ActionServlet.

2) The ActionServlet having already read the struts-config.xml file knows which form bean relates to this JSP, and delegates work to the validate method of that form bean.

3) The form bean performs the validate method to determine if all required fields have been entered, and performs whatever other types of field validations that need to be performed.

4) If any required field has not been entered, or any field does not pass validation, the form bean generates ActionErrors, and after checking all fields returns back to the ActionServlet.

5) The ActionServlet checks the ActionErrors that were returned from the form beans validate method to determine if any errors have occurred. If errors have occurred, it returns to the originating JSP displaying the appropriate errors.

6) If no errors occurred in the validate method of the form bean, the ActionServlet passes control to the appropriate Action class.

7) The Action class performs any necessary business logic, and then forwards to the next appropriate action (probably another JSP).

Please follow and like us:

Post Author: Ambrish Rajput

Leave a Reply

Your email address will not be published. Required fields are marked *