Welcome to Java4u

A Single Place for all Java Resources

Looking for something?

Subscribe to this blog!

Receive the latest posts by email.

.Just enter your email below if you want to subscribe!

Email

Tuesday, August 31, 2010

Struts FAQs

Pre 1 2 3 4 Next >
Q13. How you will handle exceptions in Struts?
In Struts you can handle the exceptions in two ways:

   a) Declarative Exception Handling: You can either define global exception handling tags in your struts-config.xml or define the exception handling tags within
<action>..</action> tag.

Example:
<exception
      key="database.error.duplicate"
      path="/UserExists.jsp"
      type="mybank.account.DuplicateUserException"/> 


   b) Programmatic Exception Handling: Here you can use try{}catch{} block to handle the exception.



Q14. What are the different kinds of actions in Struts?
 
The different kinds of actions in Struts are:

ForwardAction,   IncludeAction,   DispatchAction,  LookupDispatchAction,    SwitchAction  

Q15. What is DispatchAction?
 
       The DispatchAction class is used to group related actions into one class. Using this class, you can have a method for each logical action compared than a single execute method. The DispatchAction dispatches to one of the logical actions represented by the methods. It picks a method to invoke based on an incoming request parameter. The value of the incoming parameter is the name of the method that the DispatchAction will invoke.


Q16. How to use DispatchAction? 

To use the DispatchAction, follow these steps :
  1. Create a class that extends DispatchAction (instead of Action)
  2. In a new class, add a method for every function you need to perform on the service – The method has the same signature as the execute() method of an Action class.
  3. Do not override execute() method – Because DispatchAction class itself provides execute() method.
  4. Add an entry to struts-config.xml

    Q17. What is LookupDispatchAction? 

    The LookupDispatchAction is a subclass of DispatchAction. 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.

    Q18. What is the use of LookupDispatchAction?
     
    LookupDispatchAction is useful if the method name in the Action is not driven by its name in the front end, but by the Locale independent key into the resource bundle. Since the key is always the same, the LookupDispatchAction shields your application from the side effects of I18N.

    Q19. What is difference between LookupDispatchAction and DispatchAction?
     
    The difference between LookupDispatchAction and DispatchAction is that the actual method that gets called in LookupDispatchAction is based on a lookup of a key value instead of specifying the method name directly.

    Q20. What is SwitchAction?
     
    The SwitchAction class provides a means to switch from a resource in one module to another resource in a different module. SwitchAction is useful only if you have multiple modules in your Struts application. The SwitchAction class can be used as is, without extending.


    Q21. What if <action> element has <forward> declaration with same name as global forward? 


    In this case the global forward is not used. Instead the <action> element’s <forward> takes precendence.

    Q22. What is difference between ActionForm and DynaActionForm?
     
    An ActionForm represents an HTML form that the user interacts with over one or more pages.
    You will provide properties to hold the state of the form with getters and setters to access them. Whereas, using DynaActionForm there is no need of providing properties to hold the state. Instead these properties and their type are declared in the struts-config.xml.

    The DynaActionForm bloats up the Struts config file with the xml based definition. This gets annoying as the Struts Config file grow larger.

    The DynaActionForm is not strongly typed as the ActionForm. This means there is no compile time checking for the form fields. Detecting them at runtime is painful and makes you go through redeployment.
    ActionForm can be cleanly organized in packages as against the flat organization in the Struts Config file.
    ActionForm were designed to act as a Firewall between HTTP and the Action classes, i.e. isolate and encapsulate the HTTP request parameters from direct use in Actions. With DynaActionForm, the property access is no different than using request.get Parameter( .. ).
    • DynaActionForm construction at runtime requires a lot of Java Reflection (Introspection) machinery that can be avoided.
    Q23. What is the life cycle of ActionForm?
     
    The lifecycle of ActionForm invoked by the RequestProcessor 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



    Pre 1 2 3 4 Next >


      0 comments: