You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Sachin Chowdhary <cs...@pisoftek.com> on 2003/04/26 14:19:37 UTC

newbie:Displaying data in HTML page by prepopulating ActionForm(Help Me Plz)

 I am trying to enter data into actionForm so that input textbox will
populate by the data in ActionForm

So i have entered data into ActionForm by
request.setAttribute("secondSimpleForm",secondSimpleForm); and it si also
showing data on form but not in input field.this is beacuse ActionForm will
be created by struts when form is submitted.but is there any diff way of
doing this can anyone help me i have gone through the article but it is too
confusing for me http://jakarta.apache.org/struts/faqs/newbie.html

So can anyone help Me.

I have searched many sites but i have not got answer..Net Proviodes similar
feature there form is loaded on pageload but in struts how can we do that
for display of data in HTML page .

Please if any help or suggesstion i will be very greatful to you

Thanks

Sachin


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>

  <!-- ========== Form Bean Definitions
=================================== -->
  <form-beans>
    <form-bean name="firstSimpleForm" type="struts.forms.FirstSimpleForm"/>
    <form-bean name="secondSimpleForm"
type="struts.forms.SecondSimpleForm"/>
  </form-beans>
  <!-- ========== Action Mapping Definitions
============================== -->
  <action-mappings>
    <action path="/forwardToNext" type="struts.actions.ForwardToNext"

input="/jsp/Name.jsp">
        <forward name="next" path="/jsp/FullName.jsp"/>
    </action>
  </action-mappings>
</struts-config>

ForwardToNext.java

public class ForwardToNext extends Action {
    public ActionForward execute(ActionMapping mapping,ActionForm form,
                          HttpServletRequest request,HttpServletResponse
response) throws IOException,ServletException {

        SecondSimpleForm secondSimpleForm=new SecondSimpleForm();
        secondSimpleForm.setFullName("Sachin Chowdhary");

        request.setAttribute("secondSimpleForm",secondSimpleForm);

        return(mapping.findForward("next"));
    }

}

SecondSimpleForm.java

public class SecondSimpleForm extends ActionForm {
    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    String fullName;

}

Name.jsp

<html:html>
    <html:form action="/forwardToNext.do" name="firstSimpleForm"
type="struts.forms.FirstSimpleForm">
        Enter your Name
        <html:text property="userName" />
        <html:submit />
    </html:form>
</html:html>



FullName.jsp

<html:html>
    <html:form action="/ScforwardToNext.do" name="secondSimpleForm"
type="struts.forms.SecondSimpleForm">
        Enter your Name
        <jsp:useBean id="secondSimpleForm" scope="request"
type="struts.forms.SecondSimpleForm"/>
        <bean:write name="secondSimpleForm" property="fullName"/>
        <html:text property="fullName" />
    </html:form>
</html:html>



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


RE: newbie:Displaying data in HTML page by prepopulating ActionForm(Help Me Plz)

Posted by Sachin Chowdhary <cs...@pisoftek.com>.
Hi Phil,
          Thanks for explianing me that example that really not full info on
page you gave example of registration.
What i interpret from this is that Like:

editRegistration is only creating ActionForm then why we can not create
ActionForm manully(like when any one give some input like login in web site
and then there must be login Form on login page which will be pass to next
page suppose if it shows profile of user who logs in OK

Now when users login after validation i want to show profile of user and if
he want to edit it he can.

Now according to this code what i have to do is pass Control to first
editRegistration simply by forward to / Which will

<  action path=/loginAction name="loginForm".....>
	<forward name="edit" path="/editRegistration.do"/>
</action>

create ActionForm 'registrationForm' and in execute Method of
editRegistration fetch data from database and Prepopulate ActionForm and Now
page will show data in input text fields Automatically.

So why can't we simplly create an ActionForm in LoginAction.pass it to
registrationPage by request.setAttribute("",registrationForm)

What is really diff by taking one

now i will work on this code and if have any query  i will ask you.Really
your expaination really help Me.

but what is diff from my code and this code and Mine if same ActionForm of
same name is created by you.


 )


but now editregistration is one page and user

-----Original Message-----
From: Phil Steitz [mailto:phil@steitz.com]
Sent: Monday, April 28, 2003 7:02 PM
To: Struts Users Mailing List
Subject: Re: newbie:Displaying data in HTML page by prepopulating
ActionForm(Help Me Plz)


Sachin Chowdhary wrote:
> Hi Phil,
>
>     So it is possible that if will set data in ActionForm the it will be
> displayed in <html:text Automatically or i have to use value="" tag for
> displaying it.
>
>    but problem is that struts is creating form of previous page and i want
> to
>
>   set data in Next Form in Advance and display it automatically in
> coressponding <html:text property="">
>   without using value="<%=name%>" .
>
>   Is it possible because struts is creating bean to pass it to Action of
> that page only
>

As Andrew has pointed out, the key idea in struts is that all user
requests should go through the controller, get dispatched to an Action
and then to the view.  The form beans get created and put into the
session or request when Actions are dispatched, not when pages "load".
Struts takes a controller-centric, rather than a page-centric view.  The
taglibs enable you to effectively bind HTML elements to properties of
form beans.  As a concrete example, here is the sequenece of events in
the EditRegistration example described in
http://jakarta.apache.org/struts/faqs/newbie.html#prepopulate

1. The user submits a request with path /editRegistration.  Since the
EditRegistration action has name="registrationForm" in its declaration,
struts creates a new RegistrationForm and puts it into the request
before calling the EditRegistration Action's execute method.
2. The EditRegistration action pre-populates the form and then forwards
to registration.jsp.  HTML elements on this page are "linked" to the
form bean properties via the html tags.
3. The user edits the page and submits the form.  The page posts to
/saveRegistration.  This action also expects a RegistrationForm.  Struts
again creates a new form, but this time there are Request parameters
that match some of the form bean properties and struts uses these to
populate the RegistrationForm passed in to SaveRegistration's execute
method. Here again the "linkage" between request parameters and form
bean properties is automatic. SaveRegistration uses these data to update
the user's information.

It may help to review the "walking tour" included in the struts-example
application.

Phil



> -----Original Message-----
> From: Phil Steitz [mailto:phil@steitz.com]
> Sent: Saturday, April 26, 2003 10:02 PM
> To: Struts Users Mailing List
> Subject: Re: newbie:Displaying data in HTML page by prepopulating
> ActionForm(Help Me Plz)
>
>
> Sachin Chowdhary wrote:
>
>> I am trying to enter data into actionForm so that input textbox will
>>populate by the data in ActionForm
>>
>>So i have entered data into ActionForm by
>>request.setAttribute("secondSimpleForm",secondSimpleForm); and it si also
>>showing data on form but not in input field.this is beacuse ActionForm
>
> will
>
>>be created by struts when form is submitted.but is there any diff way of
>>doing this can anyone help me i have gone through the article but it is
>
> too
>
>>confusing for me http://jakarta.apache.org/struts/faqs/newbie.html
>>
>>So can anyone help Me.
>>
>>I have searched many sites but i have not got answer..Net Proviodes
>
> similar
>
>>feature there form is loaded on pageload but in struts how can we do that
>>for display of data in HTML page .
>>
>>Please if any help or suggesstion i will be very greatful to you
>>
>>Thanks
>>
>>Sachin
>>
>>
>
> Sachin,
>
> The method described in the newbie FAQ really is the simplest way to
> handle form pre-population.  It's worth the effort to study the sample
> app carefully and start experimenting by modifying it to get a feel for
> how struts works.  The problem with your code below is that you are
> trying to do "manually" what the framework does for you.  See intermixed.
>
> -Phil
>
>
>><?xml version="1.0" encoding="UTF-8"?>
>><!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts
>>Configuration 1.1//EN"
>>"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
>><struts-config>
>>
>>  <!-- ========== Form Bean Definitions
>>=================================== -->
>>  <form-beans>
>>    <form-bean name="firstSimpleForm"
>
> type="struts.forms.FirstSimpleForm"/>
>
>>    <form-bean name="secondSimpleForm"
>>type="struts.forms.SecondSimpleForm"/>
>>  </form-beans>
>>  <!-- ========== Action Mapping Definitions
>>============================== -->
>>  <action-mappings>
>>    <action path="/forwardToNext" type="struts.actions.ForwardToNext"
>>
>>input="/jsp/Name.jsp">
>>        <forward name="next" path="/jsp/FullName.jsp"/>
>>    </action>
>>  </action-mappings>
>></struts-config>
>>
>>ForwardToNext.java
>>
>>public class ForwardToNext extends Action {
>>    public ActionForward execute(ActionMapping mapping,ActionForm form,
>>                          HttpServletRequest request,HttpServletResponse
>>response) throws IOException,ServletException {
>>
>>        SecondSimpleForm secondSimpleForm=new SecondSimpleForm();
>
> Here you should use the form that struts has already created for you and
> passed in via the form parameter, casting it to your form bean type:
>
> (but problem is that form created by Struts is form from the previous page
> and i want to
>   set data in Next Form in Advance by using this
> )
>            SecondSimpleForm secondSimpleForm = (SecondSimpleForm) form;
>
>
>>        secondSimpleForm.setFullName("Sachin Chowdhary");
>>
>>        request.setAttribute("secondSimpleForm",secondSimpleForm);
>
> The line above should be removed. Struts does this for you.
>
>
>>        return(mapping.findForward("next"));
>>    }
>>
>>}
>>
>>SecondSimpleForm.java
>>
>>public class SecondSimpleForm extends ActionForm {
>>    public String getFullName() {
>>        return fullName;
>>    }
>>
>>    public void setFullName(String fullName) {
>>        this.fullName = fullName;
>>    }
>>
>>    String fullName;
>>
>>}
>>
>>Name.jsp
>>
>><html:html>
>>    <html:form action="/forwardToNext.do" name="firstSimpleForm"
>>type="struts.forms.FirstSimpleForm">
>>        Enter your Name
>>        <html:text property="userName" />
>>        <html:submit />
>>    </html:form>
>></html:html>
>>
>>
>>
>>FullName.jsp
>>
>><html:html>
>>    <html:form action="/ScforwardToNext.do" name="secondSimpleForm"
>>type="struts.forms.SecondSimpleForm">
>>        Enter your Name
>>        <jsp:useBean id="secondSimpleForm" scope="request"
>>type="struts.forms.SecondSimpleForm"/>
>
> The useBean above should be removed. Struts puts the form bean in scope
> for you.
>
>
>>        <bean:write name="secondSimpleForm" property="fullName"/>
>>        <html:text property="fullName" />
>>    </html:form>
>></html:html>
>>
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: struts-user-help@jakarta.apache.org
>>
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>




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



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


Re: newbie:Displaying data in HTML page by prepopulating ActionForm(Help Me Plz)

Posted by Phil Steitz <ph...@steitz.com>.
Sachin Chowdhary wrote:
> Hi Phil,
> 
>     So it is possible that if will set data in ActionForm the it will be
> displayed in <html:text Automatically or i have to use value="" tag for
> displaying it.
> 
>    but problem is that struts is creating form of previous page and i want
> to
> 
>   set data in Next Form in Advance and display it automatically in
> coressponding <html:text property="">
>   without using value="<%=name%>" .
> 
>   Is it possible because struts is creating bean to pass it to Action of
> that page only
> 

As Andrew has pointed out, the key idea in struts is that all user 
requests should go through the controller, get dispatched to an Action 
and then to the view.  The form beans get created and put into the 
session or request when Actions are dispatched, not when pages "load". 
Struts takes a controller-centric, rather than a page-centric view.  The 
taglibs enable you to effectively bind HTML elements to properties of 
form beans.  As a concrete example, here is the sequenece of events in 
the EditRegistration example described in 
http://jakarta.apache.org/struts/faqs/newbie.html#prepopulate

1. The user submits a request with path /editRegistration.  Since the 
EditRegistration action has name="registrationForm" in its declaration, 
struts creates a new RegistrationForm and puts it into the request 
before calling the EditRegistration Action's execute method.
2. The EditRegistration action pre-populates the form and then forwards 
to registration.jsp.  HTML elements on this page are "linked" to the 
form bean properties via the html tags.
3. The user edits the page and submits the form.  The page posts to
/saveRegistration.  This action also expects a RegistrationForm.  Struts 
again creates a new form, but this time there are Request parameters 
that match some of the form bean properties and struts uses these to 
populate the RegistrationForm passed in to SaveRegistration's execute 
method. Here again the "linkage" between request parameters and form 
bean properties is automatic. SaveRegistration uses these data to update 
the user's information.

It may help to review the "walking tour" included in the struts-example 
application.

Phil



> -----Original Message-----
> From: Phil Steitz [mailto:phil@steitz.com]
> Sent: Saturday, April 26, 2003 10:02 PM
> To: Struts Users Mailing List
> Subject: Re: newbie:Displaying data in HTML page by prepopulating
> ActionForm(Help Me Plz)
> 
> 
> Sachin Chowdhary wrote:
> 
>> I am trying to enter data into actionForm so that input textbox will
>>populate by the data in ActionForm
>>
>>So i have entered data into ActionForm by
>>request.setAttribute("secondSimpleForm",secondSimpleForm); and it si also
>>showing data on form but not in input field.this is beacuse ActionForm
> 
> will
> 
>>be created by struts when form is submitted.but is there any diff way of
>>doing this can anyone help me i have gone through the article but it is
> 
> too
> 
>>confusing for me http://jakarta.apache.org/struts/faqs/newbie.html
>>
>>So can anyone help Me.
>>
>>I have searched many sites but i have not got answer..Net Proviodes
> 
> similar
> 
>>feature there form is loaded on pageload but in struts how can we do that
>>for display of data in HTML page .
>>
>>Please if any help or suggesstion i will be very greatful to you
>>
>>Thanks
>>
>>Sachin
>>
>>
> 
> Sachin,
> 
> The method described in the newbie FAQ really is the simplest way to
> handle form pre-population.  It's worth the effort to study the sample
> app carefully and start experimenting by modifying it to get a feel for
> how struts works.  The problem with your code below is that you are
> trying to do "manually" what the framework does for you.  See intermixed.
> 
> -Phil
> 
> 
>><?xml version="1.0" encoding="UTF-8"?>
>><!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts
>>Configuration 1.1//EN"
>>"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
>><struts-config>
>>
>>  <!-- ========== Form Bean Definitions
>>=================================== -->
>>  <form-beans>
>>    <form-bean name="firstSimpleForm"
> 
> type="struts.forms.FirstSimpleForm"/>
> 
>>    <form-bean name="secondSimpleForm"
>>type="struts.forms.SecondSimpleForm"/>
>>  </form-beans>
>>  <!-- ========== Action Mapping Definitions
>>============================== -->
>>  <action-mappings>
>>    <action path="/forwardToNext" type="struts.actions.ForwardToNext"
>>
>>input="/jsp/Name.jsp">
>>        <forward name="next" path="/jsp/FullName.jsp"/>
>>    </action>
>>  </action-mappings>
>></struts-config>
>>
>>ForwardToNext.java
>>
>>public class ForwardToNext extends Action {
>>    public ActionForward execute(ActionMapping mapping,ActionForm form,
>>                          HttpServletRequest request,HttpServletResponse
>>response) throws IOException,ServletException {
>>
>>        SecondSimpleForm secondSimpleForm=new SecondSimpleForm();
> 
> Here you should use the form that struts has already created for you and
> passed in via the form parameter, casting it to your form bean type:
> 
> (but problem is that form created by Struts is form from the previous page
> and i want to
>   set data in Next Form in Advance by using this
> )
>            SecondSimpleForm secondSimpleForm = (SecondSimpleForm) form;
> 
> 
>>        secondSimpleForm.setFullName("Sachin Chowdhary");
>>
>>        request.setAttribute("secondSimpleForm",secondSimpleForm);
> 
> The line above should be removed. Struts does this for you.
> 
> 
>>        return(mapping.findForward("next"));
>>    }
>>
>>}
>>
>>SecondSimpleForm.java
>>
>>public class SecondSimpleForm extends ActionForm {
>>    public String getFullName() {
>>        return fullName;
>>    }
>>
>>    public void setFullName(String fullName) {
>>        this.fullName = fullName;
>>    }
>>
>>    String fullName;
>>
>>}
>>
>>Name.jsp
>>
>><html:html>
>>    <html:form action="/forwardToNext.do" name="firstSimpleForm"
>>type="struts.forms.FirstSimpleForm">
>>        Enter your Name
>>        <html:text property="userName" />
>>        <html:submit />
>>    </html:form>
>></html:html>
>>
>>
>>
>>FullName.jsp
>>
>><html:html>
>>    <html:form action="/ScforwardToNext.do" name="secondSimpleForm"
>>type="struts.forms.SecondSimpleForm">
>>        Enter your Name
>>        <jsp:useBean id="secondSimpleForm" scope="request"
>>type="struts.forms.SecondSimpleForm"/>
> 
> The useBean above should be removed. Struts puts the form bean in scope
> for you.
> 
> 
>>        <bean:write name="secondSimpleForm" property="fullName"/>
>>        <html:text property="fullName" />
>>    </html:form>
>></html:html>
>>
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: struts-user-help@jakarta.apache.org
>>
> 
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 




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


RE: newbie:Displaying data in HTML page by prepopulating ActionForm(Help Me Plz)

Posted by Sachin Chowdhary <cs...@pisoftek.com>.
Hi Phil,

    So it is possible that if will set data in ActionForm the it will be
displayed in <html:text Automatically or i have to use value="" tag for
displaying it.

   but problem is that struts is creating form of previous page and i want
to

  set data in Next Form in Advance and display it automatically in
coressponding <html:text property="">
  without using value="<%=name%>" .

  Is it possible because struts is creating bean to pass it to Action of
that page only

-----Original Message-----
From: Phil Steitz [mailto:phil@steitz.com]
Sent: Saturday, April 26, 2003 10:02 PM
To: Struts Users Mailing List
Subject: Re: newbie:Displaying data in HTML page by prepopulating
ActionForm(Help Me Plz)


Sachin Chowdhary wrote:
>  I am trying to enter data into actionForm so that input textbox will
> populate by the data in ActionForm
>
> So i have entered data into ActionForm by
> request.setAttribute("secondSimpleForm",secondSimpleForm); and it si also
> showing data on form but not in input field.this is beacuse ActionForm
will
> be created by struts when form is submitted.but is there any diff way of
> doing this can anyone help me i have gone through the article but it is
too
> confusing for me http://jakarta.apache.org/struts/faqs/newbie.html
>
> So can anyone help Me.
>
> I have searched many sites but i have not got answer..Net Proviodes
similar
> feature there form is loaded on pageload but in struts how can we do that
> for display of data in HTML page .
>
> Please if any help or suggesstion i will be very greatful to you
>
> Thanks
>
> Sachin
>
>
Sachin,

The method described in the newbie FAQ really is the simplest way to
handle form pre-population.  It's worth the effort to study the sample
app carefully and start experimenting by modifying it to get a feel for
how struts works.  The problem with your code below is that you are
trying to do "manually" what the framework does for you.  See intermixed.

-Phil

> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts
> Configuration 1.1//EN"
> "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
> <struts-config>
>
>   <!-- ========== Form Bean Definitions
> =================================== -->
>   <form-beans>
>     <form-bean name="firstSimpleForm"
type="struts.forms.FirstSimpleForm"/>
>     <form-bean name="secondSimpleForm"
> type="struts.forms.SecondSimpleForm"/>
>   </form-beans>
>   <!-- ========== Action Mapping Definitions
> ============================== -->
>   <action-mappings>
>     <action path="/forwardToNext" type="struts.actions.ForwardToNext"
>
> input="/jsp/Name.jsp">
>         <forward name="next" path="/jsp/FullName.jsp"/>
>     </action>
>   </action-mappings>
> </struts-config>
>
> ForwardToNext.java
>
> public class ForwardToNext extends Action {
>     public ActionForward execute(ActionMapping mapping,ActionForm form,
>                           HttpServletRequest request,HttpServletResponse
> response) throws IOException,ServletException {
>
>         SecondSimpleForm secondSimpleForm=new SecondSimpleForm();
Here you should use the form that struts has already created for you and
passed in via the form parameter, casting it to your form bean type:

(but problem is that form created by Struts is form from the previous page
and i want to
  set data in Next Form in Advance by using this
)
           SecondSimpleForm secondSimpleForm = (SecondSimpleForm) form;

>         secondSimpleForm.setFullName("Sachin Chowdhary");
>
>         request.setAttribute("secondSimpleForm",secondSimpleForm);
The line above should be removed. Struts does this for you.

>
>         return(mapping.findForward("next"));
>     }
>
> }
>
> SecondSimpleForm.java
>
> public class SecondSimpleForm extends ActionForm {
>     public String getFullName() {
>         return fullName;
>     }
>
>     public void setFullName(String fullName) {
>         this.fullName = fullName;
>     }
>
>     String fullName;
>
> }
>
> Name.jsp
>
> <html:html>
>     <html:form action="/forwardToNext.do" name="firstSimpleForm"
> type="struts.forms.FirstSimpleForm">
>         Enter your Name
>         <html:text property="userName" />
>         <html:submit />
>     </html:form>
> </html:html>
>
>
>
> FullName.jsp
>
> <html:html>
>     <html:form action="/ScforwardToNext.do" name="secondSimpleForm"
> type="struts.forms.SecondSimpleForm">
>         Enter your Name
>         <jsp:useBean id="secondSimpleForm" scope="request"
> type="struts.forms.SecondSimpleForm"/>
The useBean above should be removed. Struts puts the form bean in scope
for you.

>         <bean:write name="secondSimpleForm" property="fullName"/>
>         <html:text property="fullName" />
>     </html:form>
> </html:html>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>




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



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


Re: newbie:Displaying data in HTML page by prepopulating ActionForm(Help Me Plz)

Posted by Phil Steitz <ph...@steitz.com>.
Sachin Chowdhary wrote:
>  I am trying to enter data into actionForm so that input textbox will
> populate by the data in ActionForm
> 
> So i have entered data into ActionForm by
> request.setAttribute("secondSimpleForm",secondSimpleForm); and it si also
> showing data on form but not in input field.this is beacuse ActionForm will
> be created by struts when form is submitted.but is there any diff way of
> doing this can anyone help me i have gone through the article but it is too
> confusing for me http://jakarta.apache.org/struts/faqs/newbie.html
> 
> So can anyone help Me.
> 
> I have searched many sites but i have not got answer..Net Proviodes similar
> feature there form is loaded on pageload but in struts how can we do that
> for display of data in HTML page .
> 
> Please if any help or suggesstion i will be very greatful to you
> 
> Thanks
> 
> Sachin
> 
> 
Sachin,

The method described in the newbie FAQ really is the simplest way to 
handle form pre-population.  It's worth the effort to study the sample 
app carefully and start experimenting by modifying it to get a feel for 
how struts works.  The problem with your code below is that you are 
trying to do "manually" what the framework does for you.  See intermixed.

-Phil

> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts
> Configuration 1.1//EN"
> "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
> <struts-config>
> 
>   <!-- ========== Form Bean Definitions
> =================================== -->
>   <form-beans>
>     <form-bean name="firstSimpleForm" type="struts.forms.FirstSimpleForm"/>
>     <form-bean name="secondSimpleForm"
> type="struts.forms.SecondSimpleForm"/>
>   </form-beans>
>   <!-- ========== Action Mapping Definitions
> ============================== -->
>   <action-mappings>
>     <action path="/forwardToNext" type="struts.actions.ForwardToNext"
> 
> input="/jsp/Name.jsp">
>         <forward name="next" path="/jsp/FullName.jsp"/>
>     </action>
>   </action-mappings>
> </struts-config>
> 
> ForwardToNext.java
> 
> public class ForwardToNext extends Action {
>     public ActionForward execute(ActionMapping mapping,ActionForm form,
>                           HttpServletRequest request,HttpServletResponse
> response) throws IOException,ServletException {
> 
>         SecondSimpleForm secondSimpleForm=new SecondSimpleForm();
Here you should use the form that struts has already created for you and 
passed in via the form parameter, casting it to your form bean type:
           SecondSimpleForm secondSimpleForm = (SecondSimpleForm) form;

>         secondSimpleForm.setFullName("Sachin Chowdhary");
> 
>         request.setAttribute("secondSimpleForm",secondSimpleForm);
The line above should be removed. Struts does this for you.

> 
>         return(mapping.findForward("next"));
>     }
> 
> }
> 
> SecondSimpleForm.java
> 
> public class SecondSimpleForm extends ActionForm {
>     public String getFullName() {
>         return fullName;
>     }
> 
>     public void setFullName(String fullName) {
>         this.fullName = fullName;
>     }
> 
>     String fullName;
> 
> }
> 
> Name.jsp
> 
> <html:html>
>     <html:form action="/forwardToNext.do" name="firstSimpleForm"
> type="struts.forms.FirstSimpleForm">
>         Enter your Name
>         <html:text property="userName" />
>         <html:submit />
>     </html:form>
> </html:html>
> 
> 
> 
> FullName.jsp
> 
> <html:html>
>     <html:form action="/ScforwardToNext.do" name="secondSimpleForm"
> type="struts.forms.SecondSimpleForm">
>         Enter your Name
>         <jsp:useBean id="secondSimpleForm" scope="request"
> type="struts.forms.SecondSimpleForm"/>
The useBean above should be removed. Struts puts the form bean in scope 
for you.

>         <bean:write name="secondSimpleForm" property="fullName"/>
>         <html:text property="fullName" />
>     </html:form>
> </html:html>
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 




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