You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Santhosh Kumar <Sa...@PLANETASIA.COM> on 2002/02/16 08:22:51 UTC

RE: Form -> Bean conversion - Not getting values

I tried doing this but I'm not getting the form bean attribute values after
the second submit. Its like this. My form bean contains some properties and
a reference to my value bean. The first page submits and action class
forwards to second page after populating form fields except the value object
fields. The second form that contaning the value object fields submits to
the same action class and here I am getting the values from value object.
This part works fine. but I am not getting the form bean values that got
populated at the first submit.

Struts- config :
  <form-beans>

   <form-bean      name="MyForm"
                    type="mypackage.MyForm"/>
   </form-beans>

  <action-mappings>
     <!-- Ejb create -->
    <action    path="/one"
               type="mypackage.MyAction"
               name="MyForm"
              scope="session"
	      validate="false">
      <forward name="next"  path="/second.jsp"/>
      <forward name="success"  path="/confirm.jsp"/>
    </action>

Any clue ? 

Thanks in advance

S

> ----------
> From: 	James Mitchell
> Reply To: 	Struts Users Mailing List
> Sent: 	Saturday, February 16, 2002 11:56 AM
> To: 	'Struts Users Mailing List'
> Subject: 	RE: Form -> Bean conversion
> 
> I have also seen this done as an "embedded bean" object for generic action
> form.
> 
> What you would do is create a generic ActionForm with a setBean() method
> and
> a getBean() method.
> 
> Then in your ActionClass, call setBean( bean ) in the form and pass it to
> the action forward.
> 
> Then in your jsp, always refer to the ActionForm with "bean.<method>"
> 
> This concept forces the struts ActionForm to act as a wrapper to your bean
> classes and you never have to write a single "duplicate" line of code.
> 
> Example:
> Say you have a bean called Customer:
> ---------------------------------------
> public class Customer {
>   private String firstName;
>   private String lastName;
> 
>  public String getFirstName() {
>     return firstName;
>   }
>   public void setFirstName(String firstName) {
>     this.firstName = firstName;
>   }
>   public void setLastName(String lastName) {
>     this.lastName = lastName;
>   }
>   public String getLastName() {
>     return lastName;
>   }
> }
> ----------------------------------------
> 
> Now you want to use it in struts without using some mapping utility or
> writing a bunch of this:
> ...
> ...
> form.setFirstName( bean.getFirstName() );
> form.setLastName( bean.getLastName() );
> ...
> ...
> 
> right?
> 
> Then don't.
> 
> Create one ActionForm (for each bean) like so:
> -------------------------------------------------------
> public class GenericDetailForm extends ActionForm {
> 
>   private Customer bean = null;
> 
>   public void setBean( Customer bean ){
>     this.bean = bean;
>   }
>   public Customer getBean(){
>     return this.bean;
>   }
> ...
> ...
> //rest of required code here
> ...
> ...
> 
> }
> -------------------------------------------------------
> 
> 
> Then in your jsp page.....do something like this:
> 
> ...
> ...
> <tr>
>  <td><html:text property="bean.firstName" size="10"/></td>
> ...
> ...
> 
> Struts will auto-populate your existing bean for you and hand it to you
> (stuffed) in your ActionForm (in the perform() of your Action class)
> 
> 
> 
> *Note - you could even take it a step further and use the same wrapper for
> **ALL** your beans.  Of course that would mean changing your bean field in
> the wrapper class to java.lang.Object and then casting it out of the form
> at
> the top of each jsp page, which isn't much work considering the time saved
> in code reuse. (Let's face it, some people just can't stand
> scriplets....like me)
> 
> 
> James Mitchell
> Software Engineer
> Open-Tools.org
> Home Phone (770) 822-3359
> Cell Phone: (678) 910-8017
> 
> 
> -----Original Message-----
> From: CyberZombie [mailto:CyberZombie@mediaone.net]
> Sent: Friday, February 15, 2002 11:36 PM
> To: Struts Users Mailing List
> Subject: Re: Form -> Bean conversion
> 
> 
> Here's a rudimentary wrapper tool.  The commented out section is used
> when wrapping for Struts 1.0.x -- 1.1 does a much better job there.  I
> still have hardcoded the file masks (I assume all VO's end in
> Value.java).  And I assume that all wrapped getters have corresponding
> setters.  What can I say...I got lazy when I got it working and went
> back to solving the business problems on this app... :)
> 
> John M. Corro wrote:
> 
> >We have a bunch of existing beans that we'd like to use w/ ActionForms.
> For each bean we'll need to obviously expose the getters/setters in the
> corresponding ActionForm.  The ideal scenario we'd like to see happen is
> prevent the ActionForms from having all the corresponding getters/setters
> hardcoded into it.  We're ok w/ coding the validate() method for each
> class
> by hand, but we'd like to avoid having to code each ActionForm w/ a
> getter/setter (even if it is templated out for us by a Struts plug in or
> even a custom written batch file).
> >
> >Has anyone implemented something like this?
> >
> 
> 
> 
> 
> _________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com
> 
> 
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> 

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Form -> Bean conversion - Not getting values

Posted by James Mitchell <jm...@yahoo.com>.
I apologize for not getting back to you sooner.
I realized that as I began responding to you last post, my response was
quickly becoming a full blown tutorial on "wizards in struts".
I think I'll publish a tutorial on my web site in a few days, but for
now.... here is a summary what I wrote....


If I understand correctly:
1. You are trying to persist input values across multiple
   pages (such as a 3-step-wizard might do)
2. You are wanting to reuse existing bean classes within struts framework
(perhaps and Entity Bean???)


**Requirements - User must be able to hit next and previous (submit buttons
on page  **Not Browser buttons**)
and system should maintain state across pages. (Without using session or db
persistence)




I have written several wizards in struts and I have found that
the best way to handle the persistance (on the servlet container side) is to
create your wizard in such a way that you use the same jsp and same action
class
for each step in the process.  Just use a different section of the page.
(Separated by logic:equal tags)

Doing this will:
1. Allow you to have better consistency across your pages (look and feel)
2. Allow for Code reuse (my personal favorite)

Lets assume this is a 3 step wizard.
Step 1. - fill in first name and last name
Step 2. = fill in address
Step 3. - check an "I agree" on a terms page.

The ActionForms/Beans and fields we want to use are as follows:
(assuming they have accessor methods implemented)

com.mybean.MyActionForm
  boolean agreed

com.mybean.MyExistingCustomerBean (could be an Entity bean)
  String firstName
  String lastName
  String address1
  String address2
  String city
  String state
  String zipCode


Be sure to expose all fields into the html output (even if it's not used,
just put it in a hidden input tag).  Doing this will
persist your ActionForm and Bean fields from step1 to the end, and allows
the user to hit previous and back "submit" buttons.

---------------------------------------------
--------------------------------------------------

I'm not sure how you want to validate the ActionForm or Bean class at any
given step, but:
1. Change the ActionForm scope to "request"
2. Add a field in your ActionForm called "step" and put a hidden field in
the jsp named "step".
3. Put two submit buttons (each named "action") in your jsp page (one is
"<<Previous" and the other is "Next>>"

So when you are in the action class you will know where you are coming from
by getting the form.getStep() and you will know where you should forward
to by getting the form.getAction().
Therefore:
if ( form.getStep().equals( "1" ) && ( form.getAction().equals(
"Next>>" ) ){
  form.setStep( "2" );
}

if ( form.getStep().equals( "2" ) && ( form.getAction().equals(
"Next>>" ) ){
  form.setStep( "3" );
}else{
  form.setStep( "1" );
}

// if the action is not "Finish" then forward to "next" (see action mapping
forward below)
...
...



------------------------------- struts-config.xml -----------

 <form-beans>
   <form-bean  name="MyForm"  type="mypackage.MyForm"/>
 </form-beans>

  <action-mappings>
    <action    path="/doWizard"
               type="mypackage.MyAction"
               name="MyForm"
               scope="request"          <- make this request
	           validate="false">
      <forward name="next"     path="/wizard.jsp"/>
      <forward name="success"  path="/wizardcomplete.jsp"/>
    </action>



------------------------------- wizard.jsp -------------------
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">

<html:html locale="true">

<jsp:include page="/includes/header.jsp" flush="true"/>


<html:form action="/doWizard">

	<!-- This is step one -->
	<logic:equal name="yourForm" property="step"
                   scope="request" value="1">        <- make sure your
ActionForm.getStep() retuns "1" as a default.
      First Name: <html:text property="bean.firstName"
                             size="16" maxlength="16"/><br>
      Last Name:  <html:text property="bean.lastName"
                             size="16" maxlength="16"/><br>
      <input type="submit" name="action" value="Next>>">
	</logic:equal>

	<!-- This is step two -->
	<logic:equal name="yourForm" property="step"
                   scope="request" value="2">

      Address1: <html:text property="bean.address1" /><br>
      Address2: <html:text property="bean.address2" /><br>
      city:     <html:text property="bean.city"     /><br>
      state:    <html:text property="bean.state"    /><br>
      zipCode:  <html:text property="bean.zipCode"  /><br>

      <input type="submit" name="action" value="<<Previous">
      <input type="submit" name="action" value="Next>>">
	</logic:equal>

	<!-- This is step three -->
	<logic:equal name="yourForm" property="step"
                   scope="request" value="3">
      <html:link page="/viewLegal.do">
        <bean:message key="global.legal"/>
      </html:link>
      <html:checkbox property="agreed"/>
      <input type="submit" name="action" value="<<Previous">
      <input type="submit" name="action" value="Finish">
	</logic:equal>

</html:form>



<html:errors/>

<jsp:include page="/includes/footer.jsp" flush="true"/>

</html:html>

----------------------------------------------------------------------------
---




** Note to users of this code **
I did not complile or test any of this code, so use it at your own risk.
This is mainly to used as a guide to progamming in best practices for Java.



James Mitchell
Software Engineer
Open-Tools.org
Home Phone (770) 822-3359
Cell Phone: (678) 910-8017


-----Original Message-----
From: Santhosh Kumar [mailto:SanthoshK@PLANETASIA.COM]
Sent: Saturday, February 16, 2002 2:23 AM
To: 'Struts Users Mailing List'
Subject: RE: Form -> Bean conversion - Not getting values


I tried doing this but I'm not getting the form bean attribute values after
the second submit. Its like this. My form bean contains some properties and
a reference to my value bean. The first page submits and action class
forwards to second page after populating form fields except the value object
fields. The second form that contaning the value object fields submits to
the same action class and here I am getting the values from value object.
This part works fine. but I am not getting the form bean values that got
populated at the first submit.

Struts- config :
  <form-beans>

   <form-bean      name="MyForm"
                    type="mypackage.MyForm"/>
   </form-beans>

  <action-mappings>
     <!-- Ejb create -->
    <action    path="/one"
               type="mypackage.MyAction"
               name="MyForm"
              scope="session"
	      validate="false">
      <forward name="next"  path="/second.jsp"/>
      <forward name="success"  path="/confirm.jsp"/>
    </action>

Any clue ?

Thanks in advance

S

> ----------
> From: 	James Mitchell
> Reply To: 	Struts Users Mailing List
> Sent: 	Saturday, February 16, 2002 11:56 AM
> To: 	'Struts Users Mailing List'
> Subject: 	RE: Form -> Bean conversion
>
> I have also seen this done as an "embedded bean" object for generic action
> form.
>
> What you would do is create a generic ActionForm with a setBean() method
> and
> a getBean() method.
>
> Then in your ActionClass, call setBean( bean ) in the form and pass it to
> the action forward.
>
> Then in your jsp, always refer to the ActionForm with "bean.<method>"
>
> This concept forces the struts ActionForm to act as a wrapper to your bean
> classes and you never have to write a single "duplicate" line of code.
>
> Example:
> Say you have a bean called Customer:
> ---------------------------------------
> public class Customer {
>   private String firstName;
>   private String lastName;
>
>  public String getFirstName() {
>     return firstName;
>   }
>   public void setFirstName(String firstName) {
>     this.firstName = firstName;
>   }
>   public void setLastName(String lastName) {
>     this.lastName = lastName;
>   }
>   public String getLastName() {
>     return lastName;
>   }
> }
> ----------------------------------------
>
> Now you want to use it in struts without using some mapping utility or
> writing a bunch of this:
> ...
> ...
> form.setFirstName( bean.getFirstName() );
> form.setLastName( bean.getLastName() );
> ...
> ...
>
> right?
>
> Then don't.
>
> Create one ActionForm (for each bean) like so:
> -------------------------------------------------------
> public class GenericDetailForm extends ActionForm {
>
>   private Customer bean = null;
>
>   public void setBean( Customer bean ){
>     this.bean = bean;
>   }
>   public Customer getBean(){
>     return this.bean;
>   }
> ...
> ...
> //rest of required code here
> ...
> ...
>
> }
> -------------------------------------------------------
>
>
> Then in your jsp page.....do something like this:
>
> ...
> ...
> <tr>
>  <td><html:text property="bean.firstName" size="10"/></td>
> ...
> ...
>
> Struts will auto-populate your existing bean for you and hand it to you
> (stuffed) in your ActionForm (in the perform() of your Action class)
>
>
>
> *Note - you could even take it a step further and use the same wrapper for
> **ALL** your beans.  Of course that would mean changing your bean field in
> the wrapper class to java.lang.Object and then casting it out of the form
> at
> the top of each jsp page, which isn't much work considering the time saved
> in code reuse. (Let's face it, some people just can't stand
> scriplets....like me)
>
>
> James Mitchell
> Software Engineer
> Open-Tools.org
> Home Phone (770) 822-3359
> Cell Phone: (678) 910-8017
>
>
> -----Original Message-----
> From: CyberZombie [mailto:CyberZombie@mediaone.net]
> Sent: Friday, February 15, 2002 11:36 PM
> To: Struts Users Mailing List
> Subject: Re: Form -> Bean conversion
>
>
> Here's a rudimentary wrapper tool.  The commented out section is used
> when wrapping for Struts 1.0.x -- 1.1 does a much better job there.  I
> still have hardcoded the file masks (I assume all VO's end in
> Value.java).  And I assume that all wrapped getters have corresponding
> setters.  What can I say...I got lazy when I got it working and went
> back to solving the business problems on this app... :)
>
> John M. Corro wrote:
>
> >We have a bunch of existing beans that we'd like to use w/ ActionForms.
> For each bean we'll need to obviously expose the getters/setters in the
> corresponding ActionForm.  The ideal scenario we'd like to see happen is
> prevent the ActionForms from having all the corresponding getters/setters
> hardcoded into it.  We're ok w/ coding the validate() method for each
> class
> by hand, but we'd like to avoid having to code each ActionForm w/ a
> getter/setter (even if it is templated out for us by a Struts plug in or
> even a custom written batch file).
> >
> >Has anyone implemented something like this?
> >
>
>
>
>
> _________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com
>
>
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>

--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>