Pass Data from Struts Action class to Result JSP Pages

How to pass data from Struts Action class to result jsp pages?

  • If Struts Action class & its result page are using same req. & response object use request attribute other wise you can use session attribute or application attributes. (servlet context)
  • In every Struts Action class the ActionServlet class is cisible with fixed name “servlet”. (It is predefined instance variable of org.apache.Struts.action.Action).

Example code on passing data from Struts Action class to Result pages.

With respect to first Struts application:-

Step1:- create attributes having data in the execute method.

In execute () method of RegisterAction class.

            Int a = 10;

            Int b =a*a;

            Int c = a*a*a;

      String res1= “how are you”. Substring (1, 5);

     //create attributes to send result to result pages

     Req.setAttribute (“attr1”, new Integer (b)); //req attribute

     Http Session ses = req.getSession ();

   ses.setAttribute (“attr2”, new Integer(c)); //Session attribute

    Servlet context sc. = servlet.getServletContext ();

    sc.setAttribute (“attr3”, rest); //Servset sc attribute.

if (User.eq———-

                   —————–)

Step2:- to do observe how result pages are configure for Struts Action class in Struts configuration file.

         <action path = “/register” type = “app.RegisterAction” name = “rf”>

                <forward name = “success” path = “/success.jsp” redirection = “false”/>

               <forward name = “failure” path = “/failure.jsp” redirection = “true”/>

             <location>

Step3:- write following code in success.jsp / failure.jsp to read the attribute values.

Success.jsp / failure.jsp:-

   <b>attr1 (req) value is : <lb> <% = request.getAttribute (“attr1”) %>

          <br>

   <b>attr2 (ses) value is : <lb> <% = Session.getAttribute (“attr2”) %>

         <br>

<b>attr3 (application) value is : <lb> <% = application.getAttribute (“attr3”) %>

      <br>

</body>

</html>

Note: – failure.jsp cannot give request value where as success.jsp can read all the three attribute values.

  • <bean: write> tag can be to read any scope attribute values.
  • To check whether certain scope attribute is holding null value or not we can use <logic: notEmpty> tag.
  • These 2 tags are available in Struts supplied bean, logic tag libraries.
  • It always recommended to develop jsp programs of web application as java codeless jsp programs.

Developing success.jsp, failure.jsp program as java codeless jsp program while reading attribute values.

Success.jsp / failure.jsp:

  <% @ taglib uri = “http://Struts.apache.org / tags-bean” prefix = “bean” %>

<% @ taglib uri = “http://Struts.apache.org / tags-logic” prefix = “logic” %>

       <html>

          <body>

                 —————-

                  ————–

        </center>

    <b> attr1 (req) value is : </b>

           <logic: notEmpty name = “attr1” scope = “request”>

           <bean: write name = “attr1” scope = “request” format =” “/>

            </ logic: notEmpty>

           <br>

     <b> attr2 (ses) value is : </b>

          <logic: notEmpty name = “attr2” scope = “Session”>

          <bean: write name = “attr2” scope = “Session” format =” ”/>

           </ logic: notEmpty>

           <br>

         <b> attr3 (application) value is : </b>

           <logic: notEmpty name = “attr3” scope = “application”>

           <bean: write name = “attr3” scope = “application” />

            </ logic: notEmpty>

       </body>

      </html>

  • Specifying scope attribute in <logic: notEmpty> & <bean: write> tag is aptional beans when scope attribute is not specifying these tags internally uses page context.findAttribute () to search available of given attributes. This method can search for the given attribute in multiple scopes like page scope, request scope, session scope & application scope.

   <b> attr1 (req) value is : </b>

       <logic: notEmpty name = “attr1” >

           <bean: write name = “attr1” format = “” />

            </ logic: notEmpty>

  • We can use <bean: write> to even read form data from form bean class objects form bean properties.

In success.jsp / failure.jsp:-

<br> <br> <b> the given form page form data is: </b>

    <bean: write name = “rf” property = “username”/>

    <bean: write name = “rf” property = “password”/>

Question: Why scope attribute is given in <action> of Struts-config file to specify form bean class object scope?

Answer: –  When multiple action class are using single form bean to specify that form bean class scope with request to each Struts Action class the scope attribute is given in Action tag instead of in form-bean tag.

<form-beans>

        <form-beans name = “of” type = “app.RegisterForm” />

</ form-beans>

<action-mappings>

        <action path = “/xyz1” type = “MyAction1” name = “rf” scope = “request”>

               ————————

                —————————–

         </action>

   <action path = “/xyz2” type = “MyAction2” name = “rf” scope = “session”> 

              ————————–

                —————————–

     </action>

  </action-mappings>

Note: – from bean class object scope is no way related while choosing attribute scopes to send result data form Struts Action class to result pages.

Question:–  How to display the struts Action class generated result on the form page itself using which request is generated.

With respect to Struts DemoApp1 application

Step1: configure form page (request.jsp) as result page for Struts Action class (RegisterAction)

In Struts-config.xml

      < action path = “/register” type = “app.Register” name = “rf”>

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

        </action>

Step2: write following code in the execute () of StrutsAction class to store the business logic generated result as requestAttribute values.

In the execute () of RegisterAction class:-

s.o.pm (“in Reg.Action () execute ()”);

        RegisterForm fm = (RegisterForm) form;

        String user = fm.get username ();

        String pass = fm.get password ();

If (user.equals (“Ramu”) && pass.equals (“Jalli”))

     {

          Req.setAttribute (“msg”, “valid credentials”);

     }

Else {

         Req.setAttribute (“msg”, “invalid credentials”);

      }

   Return mapping.findForward (“result1”);

  }

}

Step3: write following code at the end of register.jsp to read & display the requestAttribute value.

   Result is:

<logic: notEmpty name = “msg”>

   <beans: write name = “msg” />

       </logic: notEmpty>

  • To make Struts application interacting with D.b.s/w adds jdbl code in Struts Action class.

Leave a Reply

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