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