When Struts Framework Will Be Activated in The Struts Application

Question:– When Struts framework will be activated in the struts application?

Answer: – When servlet container instantiates ActionServlet class (crates object) the ActionServlet activates Struts framework by using the jar files that are kept in WEB-INF/folder of Struts application.

  • This process takes place either during server startup/during deployment of the web application when <I-O-S> is enables on ActionServlet. If ActionServlet is not enabled with <I-O-S> then the above process takes places when ActionServlet gets 1st request from the browser window.

Note: – When ActionServlet obj. is destroyed the Struts framework will be destroyed.

  • Where traditional html tags based form page is launched on the browser window no form bean class object will be created.
  • When Struts supplied jsp tags based form page is launched on the browser window the related form bean class obj. will be created & the get xxx() will be called on that obj.  By ActionServlet to read default data from form bean class obj. & to place that data as initial values of form components in form page.
  • We can see form bean class object residing in two scopes.

1)      Request scope

2)      Session scope. (Default)

  • When form bean scope is “request” for every operation i.e performs on the form page one form bean class obj. will be created like when form page is launched or when form page is submitted.

Example:- In struts- configu.xml

        <action path = “/register” type = “app.register Action” name = “rf” scope = “request”>

  • If form bean scope is “session” irrespective of no. of operations you are performing on the form page on the form page one form bean class object will be created on one per browser window basis.

Example:- <action path =”/register” type = “app.RegisterAction” name = “rf” scope = “session”>

  • When you launch same form page for multiple times in a single browser window. If you want to see previous request data as the initial values of form components when form page is launched then take form scope as “request”.
  • We cannot place form bean object in application scope. But Struts action class object runs in application scope by default and it cannot be changed. Application scope means only one will be created for multiple request coming from multiple browser windows.

First Struts Application Development – Struts Tutorial

First Struts Application Development:- 

  • In the above app. Form bean is just taken to hold the form data of the form page.
  • The above application registers Action class as taken as model layer resource by directly placing business logic.
  • If form page or first page of Struts application is developed by using Struts supplied jsp tags then enabling <I-O-S> on ActionServlet is mandatory if same page is developed by using traditional html tags then enabling <I-O-S> on ActionServlet is optional.
  • VML parser is a software application i.e able to read & process XML documents.
  • ActionServlet (Struts framework) uses SAXparser to read & process Struts config. Uration file. Lly webserver / Servlet container also uses SAX-parser to read and process web.xml entries.

Note: – These xml parsers stores the data read from xml files in collection framework data Structures like hash table & uses then as needed in application execution.

  • When super class reference variable refers sub class object using that reference variable we can call the only methods of sub class that are present in super class and overridden or implemented sub class.
  • To make other operation also possible (calling direct methods of sub class) we need to type cast reference variable with sub class.
  • While working with Struts Action class always use ActionServlet supplied form bean class object (second parameter of execute method) bc3 this object contains form data of form page do not work with programmer created explicit new form bean object bc3 it never contains form data.

Example: –  public class test

              {

               public void (1)

                 {

                  ———

                  }

            }

      Public

Procedure to develop & deploy:

  • extract Struts-home \ apps \ Struts-blank-1.3.8.war file to temp folder by using winzip tool. (To gather xml, jar files)
  • extract Struts-home \ lib \ Struts-taglib-1.3.8.jar file to temp folder by using winzip tool. (To gather ltd files)
  • prepare the deployment directory structure of Struts application as shown below. 

The IO jar files of WEB-INF \lib folder you can collect from step1 extraction.
Gather Struts-html.ltd file from step2 extraction.

  • add TomCat-home \lib \Servlet-api.jar, Struts-home \lib \Struts-core-1.3.8.jar file to my computer class path variable to make java compiler (javac) recognize & working with Servlet-api, Struts-api.
  • compile the java source files of Struts application.

 Javac-d.*.java

  • development is completed.
  • start comeat server (Tomcat 5+)
  • deploy the Struts application in Tomcat server.

Copy E:\Struts Demo App1 Folder as to Tomcat-home\webapps folder.
Deployment is completed.
Test the application

http://local host:2020\StrutsDemoApp1\register.jsp

  • Setup used for the above application development.

      Jdk version (1.4+)

      Struts 1.3.8

      Tomcat 6.0 (5.0+)

  • In Struts application changes done in web.xml file.jsp.html programs will be recognized by underlying server automatically. Where as the changes done in Struts-config file will be recognized only after reloading the web application.
  • The Struts supplied jsp tags internally uses ActionServlet object & other Struts framework facilities during execution. So make sure that ActionServlet object is created by Servlet container before the execution of these tags for this we enable <I-O-S> on ActionServlet.
  • In the traditional html tags will not use ActionServlet obj. & Struts framework facilities during execution. So enabling <I-O-S> on ActionServlet while working with these tags is optional.
  • Since ActionServlet, our Struts application related form bean action classes residency two different packages we must take our form bean & action classes as public classes to make them visible to action Servlet & to allow ActionServlet for creating objects.
  • ActionServlet uses zero argument constructors internally while creating objects for our Form bean. Struts Action classes. So make sure that zero arg. Constructor is available if form bean, Struts Action classes directly / indirectly.
  • Taking care of every operation of a resource (java class) (from birth to death) is called as taking care of lifecycle operations.
  • Servlet container is responsible to take care of a life cycle of the Servlet jsp programs. ActionServlet takes care of the life cycle of Form bean, Struts Action class.
  • The modification done in java source of Struts application will be recognized by server only after recompilation of java source & reloading of the web application.

Difference Between Traditional HTML Tags & Struts Supplied HTML Tags

What is the difference between traditional html tags & Struts supplied html tags?

Traditional html tags Struts Supplied HTML Tags
  1.  Given by W3C organization
  2. Use complexity to wards implementing MVC2 princeples
  3. Default request method of form page of form is “GET”
  4.   While working with Struts application Extension word in the action url cannot be generated dynamically.
  5. Application while working with both java & non-java server side technologies.
  1.  Given by apache foundation
  2. Gives support to implement MVC2 Principles
  3. Default request method of form page is “POST”.
  4. While working with action url extension will be generated dynamically in Struts Application.
  5. Application only in Struts application.

With request to 3rd difference

<form action = “reg.do” method = “GET”>

———–

</form>

<form action = “reg.do” method = “GET”>

———–

</form>

Two forms of execute (-,-,-) method:

Form1     public ActionFormward execute (ActionMapping mapping. ActionForm form, Http Servlet Request req, HttpServletResponce res) throws Exception

Form2     public ActionForward execute (ActionMapping, ActionForm form, ServletRequest req, ServletResponce res) throws Exception

  • These two methods are available in predefined org.apache.Struts.action.Action calss.
  • While developing our methods (form1 recommended) to keep business logic & to process the request.
  • The “mapping” object represents <Action-mapping> to Struts configuration file using this object we can know current request related details about current Action class being from the source code of current action class.
  • This object is also useful to generated action forward object representing result page of action class. This object can also be used to make current Action class comm. With another Action class.
  • The “form” represents Form bean class object i.e associated with current Struts action class. Programmer uses this object to read form data of form page & use the data as input values of business logic.
  • This “req” object is useful for the programmer to gather additional details from request given by client. (browser window) like header values, misllaneous details & etc. this object is also useful to create request attributes while passing data from Action class to result page or Action class to another Action class.
  • “res” object is useful to set response header values in the process of sending Action class generated result directly to browser window. Like setting content type specifying auto-refresh mode, setting content and etc.

Procedure to Use Struts Supplied HTML Tag Library Based Jsp Tags in Our Struts Application

Step1: Gather information about the jsp tags of Struts-html tag library

              Refer Struts-home \ does \ Struts-taglib \index.html file.

Step2: Keep the Struts-html.tld file in WEB-INF folder.

               Gather it by extracting Struts-taglib-1.3.8.jar file by using winzip tool.

Step3:  Keep the html tag library related main & dependent jar files in WEB-INF \ lib folder to recognize & work with tag handler classes.jar files are: Struts-taglib-1.3.8.jar & its dependent jar files.

Step4: Configure this Struts supplied html tag library in web.xml file of Struts application.

                Web.xml

                            <web-app>

                                  <taglib>

                                       <taglib-uri> test </taglib-uri>

                                      <taglib-location> / WEB-INF /Struts-html.ltd </taglib-location>

                                 </taglib>

                           </web-app>

Step5: Use the jsp tags of Struts supplied html tag library in the jsp programs of Struts application.

                            Test.jsp

                                <% @ taglib uri = “test” prefix = “j” %>

                                    <j: form action = “reg.do”>

                                        Username: <j: text property = “username”/> <br>

                                        Password: <j: password property = “password” /> <br>

                                             <j: submit value = “login” />

                                 </j: form>

  • We can use the Struts supplied jsp tag library tags only in our Struts application that means they cannot be used in regular java web applications.

Procedure to Develop Custom JSP Tag Library – Struts Tutorial

Procedure to develop customer JSP tag library:-

Design tag library

             Ram tag library

                         <ABC>

                         <xyz>

Develop tag handler classes (in WEB-INF / classes)

          ABC tag.java

               Public class ABC Tag extends TagSupport

                    {

                             ————– define the functionality of ABCTag.<ABC>

                    }

        xyzTag.java

                    Public class xyz Tag extends TagSupport 

                     {

                     —————–

                         ————–   define the functionality of <xyz>

                     }

Develop .tld file for Tag library:

           WEB-INF\Ram.tld

                  <ABC> ———> ABCTag

                 <xyz> ———-> xyzTag

     Step 1 – step 3 talks about the development of jsp tag library.

configure jsp tag library in web.xml file

          <web-app>

             <taglib>

                 <taglib-uri> demo </taglib-uri>

                <taglib-location> /WEB-INF/ Ram.tld </taglib-location>

           </taglib>

      </web-app>

Note: – Every jsp tag library configured in web xml file will be identified through its taglib-url (like demo of above code)

Use jsp tag library in our jsp programs:

    Test.jsp

           <%@taglib uri = “demo” prefix =”mr” %>

                 <mr: ABC i>

                  @

                <mr: xyz i>

(h): ào/p of <mr: ABC> goes to browser window.

Note: – In the above sample code step number @ – (h) indicates <mr: ABC> Tag related flow of execution.

Step configure jsp tag library in web.xml file, Use jsp tag library in our jsp programs talks about utilization of jsp tag library.

  • Every Tag of jsp tag library must be accessed through prefix. This prefixes word or letter or is programmer choice.
  • If two jsp tags of two different jsp tag libraries are having same name in order to use both the tags in a single jsp program we can differentiate then through prefixes.
  • Struts supplies 5 no. of built-in jsp tag libraries having set of jsp tags while working with these tag libraries we can make the jsp programs of struts application as java codeless jsp programs.

Tag library name         tld file        Recommended profile        jar files having tld files &  Tag handler classes

Html                               struts-html.tld               html                                  struts – taglib – 1.3.8.jar

Bean                              struts-bean.tld                bean                                 struts – taglib – 1.3.8.jar

Logic                              struts-logic.tld                logic                                  struts – taglib – 1.3.8.jar

Nested                          struts-nested.tld            nested                               struts – taglib – 1.3.8.jar

Tiles                               struts-tiles.tld                 tiles                                    struts – tiles – 1.3.8.jar

  • Developing jsp programs java codeless jsp programs is called as implementation of view helper design pattern.
  • The java classes’ jsp programs improve the readability of jsp programs.
  • To make jsp programs of struts application as java codeless jsp programs use the following tags.
  1. Struts supplied jsp tags
  2. Jstl tags
  3. Built-in action tags of jsp technology.
  4. html tags
  5. Customer tags
  • The struts-home \ does \ struts-taglib \ index.html file provides documentation about all the struts supplied jsp tag libraries.

Note: – Struts – taglib – 1.3.8. jar file, Struts – tiles – 1.3.8. jar files are available in Struts home \ lib folder.

  • The Struts supplied html tag library provides jsp tags alternate for plane traditional html tags.

Naming Conversions in Struts Applications – Struts Tutorial

Naming conversions in struts applications:-

  • X.jsp–> form page
  • X form–> Form Bean class name
  • X Action–> Struts Action class name

Example:-

  •   register.jsp–> form page
  •   Register Form–> form bean class name
  •   Register Action–> Struts action class name.

Note: – Naming conversions are given to provide self description to resource names.

  • JSP tag library is a library that contains set of readily available jsp tags.
  • The java class that defines functionality of a jsp tag is called as “Tag handler” class.
  • Each Tag handler class must extend from javax.servlet.jsp.tagext.TagSupport class.
  • Every jsp tag library contains one tld file [tag library descriptor (xml)].this.
  1. Jsp tag name
  2. tag handler class name
  3. attributes names
  4. possible values of each attribute & etc.
  • Programmer develops customer tag library and works with other tag libraries to develop the jsp programs of web application as java codeless jsp programs.

Struts Configuration File Struts XML – Struts Tutorial

Struts configuration file (any filename.xml):-

  • Actionservlet looks to take WEB-INF / struts-config.xml file name & location as the default name & location of struts-configuration file. If any other name or location if chosen it must be specified in web xml file during ActionServlet configuration.
  • Struts configuration file must be prepared based on the DTD rules supplied by apache foundation. (Craters of struts)
  • ActionServlet uses struts config.file entries as guidelines while generating the integration logic dynamically.
  • Struts config.file contains form beans, action class, and action forwards configuration to make them recognized by ActionServlet.
  • The java bean class that extends form predefined Action form class is called as “Form Bean” class.
  • Every Form bean must be configured in struts config.file having logical name.

RegisterForm.java:-

Public class registerform extends org.apache.struts.action.ActionForm

{

       //form bean properties declaration

             Private String username;

             Private string password;

        Public void set xxx (-) 2nos

              {

                   ——-

                  ——–

              }

     Public string get xxx (-) 2nos

                {

                    ———–

                    ——-               

]

  • The number variables of form bean class are called as “Formbean properties”.
  • The names & count of these formbean properties must match with the names & count of form components in formpage.

In struts configuration file:-

          <form-beans>

                 <form-bean name = “ref” type = “registerform”>

         </form-bean>

  • The java class that extends from predefined action class is called as struts Action class. This class contains execute method returning Action forward object pointing to result page.
  • Every struts action class must be configured in struts-config. File having action path & by specifying the related form bean, result pages (action forwards)
  • The process of linking Form bean & action forwards (result pages) with action class is technically called as Action mapping operation.
  • A Form bean is identified through its logical name. An action class is identified its action path.
  • An action forward configuration that points to result page is identified its logical name.

RegisterAction.java:-

  Public class Register Action extends org.apache.struts.action.Action

       {

             Public ActionForward execute (-,-,-,-,)

                {

                         —————

                         —————–

             Returns ActionForward obj (with logical name)

               }

      }    

In struts-config file (xml file):-

    <struts-config>

         <form-beans>

               <form-bean name = “ref” type =”RegisterForm”/>

          </form-beans>

     <action-mappings>

         <action path = “/reg” type = “RegisterAction” na, e =”ref”>

      <forward name = “ok” path = “/result.jsp”/>

       <location>

       <location-mapping>

     </struts-config>

1)  “ref” is logical name of frombean configuration

2) Register form is javabean i.e acting as formbean class.

3)  “/ref” is the action path of action class.

4)      Register action is java class i.e acting as struts Action class.

5) Form Bean logic name “ref” here it is used to link form bean with action class.

6) “ok” Action Forward configuration logic name

7) “result.jsp” is the result page of current struts Action class (Register Form)

IOC and Dependency Injection – Struts Tutorial

IOC / Dependency injection:-

Dependency lookup:-

In this process resource spends rimes to search & gather its dependent values from other resource of the application. In this process the resource pulls the values from other resources.

Example: –   The way JDBC data source object is gathered from registry software through JNDI comes under dependency lookup.

Example: –   If student gets its course material from institute staff only by asking / demanding for it comes under dependency lookup.

Dependency injection:-

  • If the underlying s/w or framework or container or server or special resource of the application is assigning values to our resource dynamically or automatically then that process is called as “Dependency Injection”.
  • In this process the underlying s/w processes values to the resource.

Real life example: – The way institute staff person assigns material to students immediately after is registration for course comes under dependency injection.

Technical example: – The way ActionServlet writes form data of form page to form bean class object dynamically comes under dependency injection.

MVC2 in Struts:-

  • This design pattern allows the programmer to develop the web application by having multiple layers & clean separation of logics.
  • Struts application also contains multiple layers like view controller & model and also allows clean separation of logics.

Abstract Controller – Struts Tutorial

The helper java class of front controller servlet that makes source code of front controller servlet flexible to modify with out disturbing front controller is called as “Abstract controller”.

This Abstract controller is not a servlet program it is a helper java class for the front controller servlet program / jsp program.

In struts application the predefined java class called “org.apache.struts.action.requestprocessor” class acts as helper class to the front controller Action servlet. That means this class will take care of lots of operations on behalf of ActionServlet.

Similarly this class can also be used to pass addition instructions to ActionServlet.

VO DTO Class in Struts – Struts Tutorial

VO DTO Class in Struts:-

  • While transferring huge amount of data from one layer to another layer instead of transferring them one by one for multiple times it is recommended to combine these multiple values into single object and send that object from source layer to destination layer. This process reduces round trips b/w source & destination layers and makes sending & receiving operations as easy operations. Here the class of that single object that combines multiple values is called as “V.O. class /D.T.O class”.

Note: –   In struts application the formbean class acts as V.O. class /D.T.O class.

  • In struts application ActionServlet reads the multiple values of from page and write to single form bean class object and sends that form bean class object visible to struts Action class.
  • In the Action class reads & uses the form data of form page as input values while processing the request that means controller layer ActionServlet is sending multiple values of form page to the Action class of model layer by keeping in the single object of form bean class. So this form bean class is called as “V.O. class / D.T.O. class”.