You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Mike Elliott <hb...@gmail.com> on 2005/04/28 00:41:33 UTC

Does struts really have to construct a new form bean every time?

I'm trying to build an application where:

1) p1.jsp transitions via a link: /do/Something?action=initialize.

2) This causes class app.SelectionAction to have its execute() method invoked.

3) The execute() method, based on the parameter, initializes the form passed it.

4) The execute() method then transitions to p2.jsp, which renders the page.

5) A user changes something, then causes a submit to occur.

6) The submit transitions via /do/Something?action=update

7) This causes class app.SelectionAction to have its execute() method invoked.

8) The execute() method, based on the parameter, stores info from the form.

This is a reasonable scenario, right?

Somewhere between steps 1 and 3, a new form bean is created.  This is
reasonable, I guess.  Anyway, it gets initialized in step 3.  However,
somewhere between steps 4 and 8, a second new form is created, losing
the information so carefully initialized in step 3.  I don't want to
throw away the old bean.  I want to keep using the bean I so painfully
initialized in step 3.

Is that possible?  If so, how do I tell struts not to throw away the
old bean but re-use it?  If not, how does one accomplish this goal?

Here's my setup:

--- struts-config.xml ------------------------------------------
<struts-config>
   <form-beans>
      <form-bean
         name="selectionForm"
         type="app.SelectionForm"/>
   </form-beans>

   <action-mappings>
      <action
         path="/Something"
         type="app.SelectionAction"
         name="selectionForm"
         scope="request"
         validate="false"
         >
         <forward name="success" path="/pages/p2.jsp"/>
      </action>
   </action-mappings>
</struts-config>

--- p1.jsp ------------------------------------------
<%@ taglib uri="struts/html-el" prefix="html" %>
<html>
  <head><title>p1.jsp</title></head>
  <body>
    <html:link page="/do/Something?action=initialize">
      Follow this link
    </html:link>
  </body>
</html>

--- p2.jsp ------------------------------------------
<%@ taglib uri="struts/html-el" prefix="html" %>
<%@ taglib uri="jstl/c" prefix="c" %>
<html>
  <head><title>p2.jsp</title></head>
  <body>
     <html:form action="/Something?action=update">
     <H1>Your last selection:
       <c:out value="${selectionForm.previous_selection}"/>
     </H1>
     <H1>Pick something</H1>
     <html:select property="selected_value"
       onchange="javascript:document.selectionForm.submit()">
       <html:options property="selection_list"/>
     </html:select>
  </html:form>
</body>
</html>

--- SelectionAction.java ------------------------------------------
package app;

import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.util.Arrays;

public class SelectionAction extends Action {
   public ActionForward execute( ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response )
        throws Exception {
      SelectionForm thisForm = (SelectionForm)form;
      String action = request.getParameter( "action" );
      if ("initialize".equals( action )) {
         String[] values = new String[] { "alpha", "beta", "gamma" };
         thisForm.setSelection_list( Arrays.asList( values ) );
         thisForm.setSelected_value( "beta" );
      }
      else if ("update".equals( action )) {
         thisForm.setPrevious_selection( thisForm.getSelected_value() );
         thisForm.setSelected_value( "alpha" );
      }
      return mapping.findForward( "success" );
   }
}

--- SelectionForm.java ------------------------------------------
package app;
import org.apache.struts.action.ActionForm;
import java.util.Arrays;
import java.util.Collection;

public class SelectionForm extends ActionForm {
   public Collection getSelection_list() {
      return selection_list;
   }
   public void setSelection_list( Collection selection_list ) {
      this.selection_list = selection_list;
   }
   public String getSelected_value() {
      return selected_value;
   }
   public void setSelected_value( String selected_value ) {
      this.selected_value = selected_value;
   }
   public String getPrevious_selection() {
      return previous_selection;
   }
   public void setPrevious_selection( String previous_selection ) {
      this.previous_selection = previous_selection;
   }
   public int getSerialNumber() {
      return serialNumber;
   }
   public SelectionForm() {
      serialNumber = nextSerialNumber++;
      System.out.println( "Creating " + serialNumber );
   }
   private Collection selection_list =
      Arrays.asList( new String[] { "red", "white", "blue" } );
   private String selected_value = "blue";
   private String previous_selection = "No previous selection";
   private int serialNumber;
   private static int nextSerialNumber = 0;
}

--- web.xml ------------------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
  <display-name>pr2004</display-name>

  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>application</param-name>
      <param-value>ApplicationResources</param-value>
    </init-param>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <!-- Action Servlet Mapping -->
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>/do/*</url-pattern>
  </servlet-mapping>

  <!-- The Welcome File List -->
  <welcome-file-list>
    <welcome-file>pages/p1.jsp</welcome-file>
  </welcome-file-list>

  <!-- tag libs -->
  <taglib>
    <taglib-uri>struts/bean-el</taglib-uri>
    <taglib-location>/WEB-INF/taglib/struts-bean-el.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>struts/html-el</taglib-uri>
    <taglib-location>/WEB-INF/taglib/struts-html-el.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>struts/logic-el</taglib-uri>
    <taglib-location>/WEB-INF/taglib/struts-logic-el.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>jstl/c</taglib-uri>
    <taglib-location>/WEB-INF/taglib/c.tld</taglib-location>
  </taglib>

</web-app>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Does struts really have to construct a new form bean every time?

Posted by Mike Elliott <hb...@gmail.com>.
On 4/27/05, Michael Jouravlev <jm...@gmail.com> wrote:

>         scope="request"   <=========== should be scope="session"

Thanks!  I knew it had to be something simple like that!  And I've
wasted a day and a half finding it.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Does struts really have to construct a new form bean every time?

Posted by Mike Elliott <hb...@gmail.com>.
On 4/27/05, Michael Jouravlev <jm...@gmail.com> wrote:
>    <action-mappings>
>      <action
>         path="/Something"
>         type="app.SelectionAction"
>         name="selectionForm"
>         scope="request"   <=========== should be scope="session"
>         validate="false"
>         >
>         <forward name="success" path="/pages/p2.jsp"/>
>      </action>
>   </action-mappings>
> 
> On 4/27/05, Mike Elliott <hb...@gmail.com> wrote:
> > I'm trying to build an application where:
> >
> > 1) p1.jsp transitions via a link: /do/Something?action=initialize.
> >
> > 2) This causes class app.SelectionAction to have its execute() method invoked.
> >
> > 3) The execute() method, based on the parameter, initializes the form passed it.
> >
> > 4) The execute() method then transitions to p2.jsp, which renders the page.
> >
> > 5) A user changes something, then causes a submit to occur.
> >
> > 6) The submit transitions via /do/Something?action=update
> >
> > 7) This causes class app.SelectionAction to have its execute() method invoked.
> >
> > 8) The execute() method, based on the parameter, stores info from the form.
> >
> > This is a reasonable scenario, right?
> >
> > Somewhere between steps 1 and 3, a new form bean is created.  This is
> > reasonable, I guess.  Anyway, it gets initialized in step 3.  However,
> > somewhere between steps 4 and 8, a second new form is created, losing
> > the information so carefully initialized in step 3.  I don't want to
> > throw away the old bean.  I want to keep using the bean I so painfully
> > initialized in step 3.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Does struts really have to construct a new form bean every time?

Posted by Michael Jouravlev <jm...@gmail.com>.
   <action-mappings>
     <action
        path="/Something"
        type="app.SelectionAction"
        name="selectionForm"
        scope="request"   <=========== should be scope="session"
        validate="false"
        >
        <forward name="success" path="/pages/p2.jsp"/>
     </action>
  </action-mappings>


On 4/27/05, Mike Elliott <hb...@gmail.com> wrote:
> I'm trying to build an application where:
> 
> 1) p1.jsp transitions via a link: /do/Something?action=initialize.
> 
> 2) This causes class app.SelectionAction to have its execute() method invoked.
> 
> 3) The execute() method, based on the parameter, initializes the form passed it.
> 
> 4) The execute() method then transitions to p2.jsp, which renders the page.
> 
> 5) A user changes something, then causes a submit to occur.
> 
> 6) The submit transitions via /do/Something?action=update
> 
> 7) This causes class app.SelectionAction to have its execute() method invoked.
> 
> 8) The execute() method, based on the parameter, stores info from the form.
> 
> This is a reasonable scenario, right?
> 
> Somewhere between steps 1 and 3, a new form bean is created.  This is
> reasonable, I guess.  Anyway, it gets initialized in step 3.  However,
> somewhere between steps 4 and 8, a second new form is created, losing
> the information so carefully initialized in step 3.  I don't want to
> throw away the old bean.  I want to keep using the bean I so painfully
> initialized in step 3.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org