Struts is open source software used to develop java based web page.
Struts uses Jakarta Packages, Java Servlets, JavaBeans, ResourceBundles, and XML Struts takes the help of Model View Controller (MVC) architecture. Where Model is referring to business or database, View is referring to the Page Design Code, and Controller is referring to navigational code.
Struts Validator Framework enables us to validate the data of both client side and server side.
When some data validation is not present in the Validator framework, then programmer can generate own validation logic, this User Defined Validation logic can be bind with Validation Framework.
Validation Framework consist of two XML configuration Files:
Validator-Rules.xml file
Validation.xml file
We need Struts in Java because of following reasons:
Helps in creation and maintenance of the application.
Make use of Model View Controller (MVC) architecture. Where Model is referring to business or database, View is referring to the Page Design Code, and Controller is referring to navigational code.
Enables developer to make use of Jakarta Packages, Java Servlets, JavaBeans, ResourceBundles, and XML
The IncludeAction is used to integrate the one action file in another action file.
It is same as ForwardAction but the only difference is that the resource is present in HTTP response.
Is used to combine the Struts functionality and control into an application that uses Servlets.
Use the IncludeAction class to include another resource in the response to the request being processed.
An Action class in the struts application is used to handle the request.
It acts as interface or communication medium between the HTTP request coming to it and business logic used to develop the application. Action class consists of RequestProcessor which act as controller. This controller will choose the best action for each incoming request, generate the instance of that action and execute that action. This should be in thread-safe manner, because RequestProcessor uses the same instance for no. of requests at same time.
Exceptions are handled in struts by using any one of the following two ways:
Programmatically handling : In this exception are handled by using try and catch block in program. Using this programmer can define how to handle the situation when exception arises.
Declarative handling : In this exception handling is done by using the XML file. Programmer defines the exception handling logic in the XML file. There are two ways of defining the exception handling logic in the XML file:
Global Action Specific Exception Handler Definition.
Local Action Specific Exception Handler Definition.
Struts Framework consists of following classes:
Action Servlets : used to control the response for each incoming request.
Action Class : used to handle the request.
Action Form : it is java bean, used to referred to forms and associated with action mapping
Action Mapping : used for mapping between object and action.
Action Forward : used to forward the result from controller to destination.
Model View Controller (MVC) is a design pattern used to perform changes in the application.
Model : Model is referring to business or database. It stores the state of the application. Model has no knowledge of the View and Controller components.
View : View is referring to the Page Design Code. It is responsible for the showing the result of the user’s query. View modifies itself when any changes in the model happen.
Controller : Controller is referring to navigational code. Controller will chose the best action for each incoming request, generate the instance of that action and execute that action.
There are following types of design patterns are used in Struts :
Service to Worker
Dispatcher View
Composite View (Struts Tiles)
Front Controller
View Helper
Synchronizer Token
In Request Scope, values of FormBean are available to current request but in Session Scope, values of FormBean are available throughout the session.
Validate() Method : this method is used to validate the properties after they are explored by the application.
Validate method is Called before FormBean is handed to Action.
This method returns a collection of ActionError.
Syntax of Validate() Method:
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)
Reset() Method : this method is called by the Struts Framework with each request that uses the defined ActionForm.
Used to reset all the data from the ActionForm
Syntax of Reset() Method:
public void reset() {}
The different kinds of actions in Struts are:
ForwardAction
IncludeAction
DispatchAction
LookupDispatchAction
SwitchAction
The DispatchAction enable the programmer to combine together related function or class.
Using Dispatch Action programmer can combine the user related action into a single UserAction. like add user, delete user and update user.
DispatchAction execute the action based on the parameter value it receives from the user.
We can use the Dispatch Action we executing following steps:
Create a class that extends DispatchAction.
In a new class, add a method: method has the same signature as the execute() method of an Action class.
Do not override execute() method.
Add an entry to struts-config.xml
The difference between LookupDispatchAction and DispatchAction are given below :
LookupDispatchAction is the subclass of the DispatchAction
Actual method that gets called in LookupDispatchAction whereas DispatchAction dispatches the action based on the parameter value.
The various Struts tag libraries are:
HTML Tags : used to create the struts input forms and GUI of web page.
Bean Tags : used to access bean and their properties.
Logic Tags : used to perform the logical operation like comparison.
Template Tags : used to changes the layout and view.
Nested Tags : used to perform the nested functionality.
Tiles Tags : used to manages the tiles of the application.
The lifecycle of ActionForm is as follows:
Retrieve or Create Form Bean associated with Action
"Store" FormBean in appropriate scope (request or session)
Reset the properties of the FormBean
Populate the properties of the FormBean
Validate the properties of the FormBean
Pass FormBean to Action
IncludeAction is used when any other action is going to intake that action whereas ForwardAction is used move the request from one resource to another resource.
The ForwardAction is used when we want to combine Struts with existing application.
Used when we want to transfer the control form JSP to local server.
Used to integrate with struts in order to take benefit of struts functionality, without writing the Servlets again.
Use to forward a request to another resource in your application
The LookupDispatchAction class is a subclass of DispatchAction
The LookupDispatchAction is used to call the actual method.
For using LookupDispatchAction, first we should generate a subclass with a set of methods.
It control the forwarding of the request to the best resource in its subclass
It does a reverse lookup on the resource bundle to get the key and then gets the method whose name is associated with the key into the Resource Bundle.
Difference between HTML tag and Struts specific HTML tags are:
HTML tags are static in nature but Struts specific HTML tags are dynamic in nature.
HTML tags are not User Defined whereas Struts tags can be user defined.
HTML tags provide the different templates and themes to the programmer whereas Struts specific HTML tag provides the integrating the property value with the Formbean properties.
HTML tags are integral part of Struts whereas Struts have HTML tag libraries.
In Request Scope, values of FormBean are available to current request but in Session Scope, values of FormBean are available throughout the session.
In action mapping is the mapping of the action performed by the user or client on the application.
We specify the action class for a specific user’s action. Like we provide the path or URL and different view based on user event.
We can also define where control of the page deviate in case of validation error in the form.
We can include ActionMapping in code like this:
<action-mappings>
<action path="/ a" type =myclasse.A name="myForm">
<forward name="Login" path="/login.jsp"/>
<forward name="error" path="/error.jsp"/>
Validation error: Validation error are those error which arises when user or client enters the invalid format data into the form. For this validation of data struts enables the programmer with the Validator() method which validates both the data from client side and the server side.
We can display all error in the JSP page by using the following syntax in the code.
SYNTAX: <html:error/>
The drawbacks of Struts are following:
Only one single controller Servlets is used.
Bigger learning curve
Worst documentation
No exception present in this framework
Less transparent
Rigid approach.
With struts 1, embedding application into JSP can’t be prevented.
Non-XML compliance of JSP syntax
An Action class in the struts application is used to handle the request.
It acts as interface or communication medium between the HTTP request coming to it and business logic used to develop the application.
Action class consists of RequestProcessor which act as controller. This controller will choose the best action for each incoming request, generate the instance of that action and execute that action.
This should be in thread-safe manner, because RequestProcessor uses the same instance for no. of requests at same time.
We can use the ForwarAction to restrict the Struts application to Model View Controller by following coding:
<global-forwards>
<statements>
<forward name="CitizenDetailsPage"
path="/gotoCitizenDetails.do" />
<action-mappings>
<statements>
<action path=”/gotoCitizenDetails”
parameter=”/CitizenDetails.jsp”
type=”org.apache.struts.actions.ForwardAction” />
We can combine Struts and Velocity template by performing following steps:
Set classpath to Velocity JARs
Make web.xml file to identify the Velocity servlet
Select Velocity toolbox.xml in WEB-INF directory.
Modify struts-config to point its views to Velocity templates instead of JSPs.
Create a Velocity template for each page you want to render.
Using refresh button
By clicking submit button more than once before the server sent back the response.
By clicking back navigation button present in browser.
The browser is restores to submit the form again.
By clicking multiple times on a transaction that is delayed than usual.