Help with Struts ActionForward populating and Redisplaying Input Forms

Discussion in 'Java' started by Miles Davenport, Oct 8, 2004.

  1. Hi, I have been experimenting with populating and Redisplaying input
    forms.

    test.jsp contains a "searchTerm", and is passed to a form-bean, and
    passed successfully to testForward.jsp.

    I have an SearchAction class (ActionForward), SearchForm class, and
    struts-config.

    I have managed to get the "forward" form to display the ":<html:text
    property="searchTerm" />" from the JSP, and forward to
    "testForward.jsp".

    I would like to know the following:
    --------
    * I have another value (String collection) in my ActionForm, with
    get/set methods (this is not taken from test.jsp).

    I would like to set this value by using
    "myForm.setCollection("myTestCollection");" - but this is not working.
    I believe I need to capture the HTTPSession, and use this to pass the
    value into setCollection.

    How do I pass a new value using "setCollection" ?


    * Can I store an Iterator using the same mechanism as the (String
    collection) ?

    * What would be the best way to display each Iterator Element. An
    Iterate tag sounds like a good idea. Is there a better alternative.
    --------
    Some sample code would be useful, as my Struts are abit rusty. :)

    snippets of current code are below.

    cheers


    Miles.


    ----
    public class SearchAction extends Action
    {
    /**
    * This is the main action called from the Struts framework.
    * @param mapping The ActionMapping used to select this instance.
    * @param form The optional ActionForm bean for this request.
    * @param request The HTTP Request we are processing.
    * @param response The HTTP Response we are processing.
    */


    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) throws
    IOException, ServletException
    {
    SearchForm myForm = (SearchForm) form;
    String mySearchTerm = myForm.getSearchTerm();

    myForm.setCollection("myTestCollection");

    return mapping.findForward("success");
    }

    }

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

    package ons.fds;

    import javax.servlet.http.HttpServletRequest;
    import org.apache.struts.action.*;

    import java.io.*;
    import java.net.*;
    import java.util.*;

    public final class SearchForm extends ActionForm {

    private String searchTerm = null;

    private String collection = null;

    private Iterator resultList = null;

    public String getCollection() {
    return (this.collection);
    }

    public void setCollection(String newCollection) {
    collection = newCollection;
    }

    public String getSearchTerm() {
    return (this.searchTerm);
    }

    public void setSearchTerm(String latestSearchTerm) {
    searchTerm = latestSearchTerm;
    }

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

    <?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-beans>
    <form-bean name="searchForm" type="ons.fds.SearchForm"/>
    </form-beans>
    <action-mappings>
    <action path="/test" forward="/testForward.jsp"
    type="ons.fds.SearchAction" name="searchForm" scope="session"
    input="/test.jsp"/>
    </action-mappings>
    </struts-config>
    -------------------------------------------------------------------------------
    JSP Page

    <html:form action="test.do">
    Search:<html:text property="searchTerm" />
    </html:form>
    ----
     
    Miles Davenport, Oct 8, 2004
    #1
    1. Advertising

  2. Miles Davenport

    Bryce Guest

    On 8 Oct 2004 06:25:40 -0700, (Miles Davenport)
    wrote:

    >Hi, I have been experimenting with populating and Redisplaying input
    >forms.
    >
    >test.jsp contains a "searchTerm", and is passed to a form-bean, and
    >passed successfully to testForward.jsp.
    >
    >I have an SearchAction class (ActionForward), SearchForm class, and
    >struts-config.
    >
    >I have managed to get the "forward" form to display the ":<html:text
    >property="searchTerm" />" from the JSP, and forward to
    >"testForward.jsp".
    >
    >I would like to know the following:
    >--------
    >* I have another value (String collection) in my ActionForm, with
    >get/set methods (this is not taken from test.jsp).
    >
    >I would like to set this value by using
    >"myForm.setCollection("myTestCollection");" - but this is not working.


    Why is it not working? Are you getting an error message?

    > I believe I need to capture the HTTPSession, and use this to pass the
    >value into setCollection.


    No you don't.

    >How do I pass a new value using "setCollection" ?


    MyForm myform = (MyForm)form;
    myform.setMyField(myField);

    Thats it.

    >* Can I store an Iterator using the same mechanism as the (String
    >collection) ?


    Why? You can set a collection class (setMyCollection(Collection
    myCollection).

    >* What would be the best way to display each Iterator Element. An
    >Iterate tag sounds like a good idea. Is there a better alternative.


    see <logic:iterate> or if you are using JSTL <c:foreach>

    >public class SearchAction extends Action
    >{
    >
    > public ActionForward execute(ActionMapping mapping, ActionForm form,
    >HttpServletRequest request, HttpServletResponse response) throws
    >IOException, ServletException
    > {
    > SearchForm myForm = (SearchForm) form;
    > String mySearchTerm = myForm.getSearchTerm();
    >
    > myForm.setCollection("myTestCollection");
    >
    > return mapping.findForward("success");
    > }
    >
    > }


    This looks fine.

    [snip form which looks fine]

    ><?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-beans>
    > <form-bean name="searchForm" type="ons.fds.SearchForm"/>
    > </form-beans>
    > <action-mappings>
    > <action path="/test" forward="/testForward.jsp"
    >type="ons.fds.SearchAction" name="searchForm" scope="session"
    >input="/test.jsp"/>
    > </action-mappings>


    Here is a problem the "forward" attribute to the <action> tag:
    forward - The request URI path to which control is passed when this
    mapping is invoked. This is an alternative to declaring a type
    property.

    Therefor, your Action class is being bypassed. You will want to define
    your action so:

    <action
    path="/test"
    type="..."
    name="searchForm"
    scope="session"
    input="/test.jsp">

    <forward name="edit" path="/testCollectionform.jsp"/>
    </action>

    ></struts-config>


    assuming you had a jsp testCollectionform.jsp:

    <html:text property="collection"/>


    >JSP Page
    >
    ><html:form action="test.do">
    >Search:<html:text property="searchTerm" />
    ></html:form>
    >----



    --
    now with more cowbell
     
    Bryce, Oct 8, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian J. Sayatovic
    Replies:
    1
    Views:
    712
    Brian J. Sayatovic
    Oct 6, 2003
  2. Hari Om
    Replies:
    0
    Views:
    2,951
    Hari Om
    Oct 6, 2003
  3. Alessandro Rossi

    struts actionForward default page

    Alessandro Rossi, May 26, 2005, in forum: Java
    Replies:
    2
    Views:
    3,180
    Wendy Smoak
    May 26, 2005
  4. Sharon Lee
    Replies:
    2
    Views:
    535
    Fredo Vincentis
    Jan 4, 2004
  5. Matt
    Replies:
    2
    Views:
    402
Loading...

Share This Page