Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Help with Struts ActionForward populating and Redisplaying Input Forms

Reply
Thread Tools

Help with Struts ActionForward populating and Redisplaying Input Forms

 
 
Miles Davenport
Guest
Posts: n/a
 
      10-08-2004
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>
----
 
Reply With Quote
 
 
 
 
Bryce
Guest
Posts: n/a
 
      10-08-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hiding and redisplaying an ASP.NET Panel Matt ASP .Net 2 10-28-2009 08:01 PM
struts actionForward default page Alessandro Rossi Java 2 05-26-2005 03:06 PM
redisplaying content of a 'textarea' on a form after an error Sharon Lee HTML 2 01-04-2004 07:05 AM
Cannot find global ActionForward for name /pages/welcome Hari Om Java 0 10-06-2003 05:12 PM
Using an ActionForward from within a custom tag in Struts 1.1 Brian J. Sayatovic Java 1 10-06-2003 01:39 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57