Friday, November 15, 2013

Sample Struts Login Application Flow

I don't go right away in to minute details
I will explain in my own way.


1)In login.jsp There are 2 text fields
    a)uname
    b)pword

2)Generally you submit to a page or servlet... Right...?
   But if you observe action value in form tag there is some thing ending with .do
   ./login.do

3)So here comes the question... To which page the login.jsp need to be submitted

4)In struts... here comes the use of a configuration file called struts-config.xml.
    It should be in WEB-INF folder of your webapplication.

5)So how is struts-config.xml going to help me to find out the page to which login.jsp
   need to be submitted.

6)When submit button is executed...,the url in the browser would be
   webapplicationname/login.do

7)Whenever request url ends with .do,There is a servlet called ActionServlet which
   takes the path after webapplicationname i.e login.do removes .do and checks
   in struts-config.xml,if there is any action element with path as /login.

8)So in our example,it will find the following action element because its path = /login
   <action path="/login" name="loginform" type="org.jlearners.actions.LoginAction" scope="session">
           <forward name="success" path="/success.jsp"/> 
           <forward name="failure" path="/failure.jsp"/>
       
        </action>
  

9)So, an object of LoginAction is created and method called execute gets automatically called.

10)Action class is nothing but any javaclass which extends org.apache.struts.action.Action.

11)In our example LoginAction extends org.apache.struts.action.Action, we have to override
     the extends method.

12)Generally when a page is submitted to servlet or jsp, we will get the values submitted using
     request.getParameter right ?

     In Struts... if You create a class which extends ActionForm and having properties which
     exactly match the html input elements in login.jsp,an instance of your class will be instantiated
     and struts autopopulates the properties with the values from the login.jsp

13)You have to specify... the class which extends ActionForm in struts-config.xml in
     <form-beans> section.

     <form-beans>
        <form-bean name="loginform" type="org.jlearners.forms.LoginForm"/>
    </form-beans>

     <action-mappings>
        <action path="/Welcome" forward="/welcomeStruts.jsp"/>
        <action path="/login" name="loginform" type="org.jlearners.actions.LoginAction" scope="session">
           <forward name="success" path="/success.jsp"/> 
           <forward name="failure" path="/failure.jsp"/>
       
        </action>
    </action-mappings>

14)Before the execute method in LoginAction gets executed,the value of name attribute is picked up,
     in this case it is loginform right... so, it will go to <form-beans> section and see the form-bean
    with name loginform.. and instantiates the class specified in type attributes value,So automatically
    LoginForm instance is created and populated with the values submitted from login.jsp

15)That populated instance will be passed as one argument to execute method.


16)So.. you are in execute method... and you can get submitted values by calling
     logForm.getUname() or ,logForm.getPword()

17)You call the method in LoginDAO with the values submitted from login.jsp and
      if the user exists you will say mapping.findforward("success").What is success ?
     It is not jsp.. so to which page it should be forwarded

18)So, struts checks if there is any forward element declared for that action(LoginAction).

19)<action path="/login" name="loginform" type="org.jlearners.actions.LoginAction" scope="session">
           <forward name="success" path="/success.jsp"/> 
           <forward name="failure" path="/failure.jsp"/>
       
        </action>

20)So if you say mapping.findForward("success") we will be forwareded to success.jsp

21)If you say mapping.findForward("failure") it will check the forward element in LoginAction
   with name failure... and forwards to the page specified as path attribute value i.e failure.jsp


So if I summarize the above steps

1)When  a page is submitted it truncates .do from login.do
2)Checks if there is any action with path value as /login
3)Finds the formbean in to which the submitted values will be loaded with the help of
    name attribute in action element
4)Populates formbean and execute in actionclass(LoginAction) gets executed.. and
   you have to write business logic here and forward to the page which displays UI.


Hope you guys understood..... :








No comments:

Post a Comment