Help with Struts ActionForward populating and Redisplaying Input Forms

  • Thread starter Miles Davenport
  • Start date
M

Miles Davenport

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>
----
 
B

Bryce

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 said:
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"/>
</struts-config>

assuming you had a jsp testCollectionform.jsp:
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top