> When the action forwards to a JSP the collection that
> was obtained and set on the form seems to have been lost.
If you are forwarding the set as part of the ActionForm, Struts may have
recycled it and called reset().
You might need to do something like,
request.setAttribute("searchResult",searchForm);
before returning, and then check for searchResult in your JSP.
Jim Downing wrote:
>
> Hi,
> I'm trying to write a little struts app that simply lists all the records in
> a database table by calling a findAll method on an Entity EJB. I'm pretty
> sure I'm getting something wrong in my Action mapping - the Action performs
> the findAll successfully, and sets a collection of remote interfaces on the
> relevant action form. When the action forwards to a JSP the collection that
> was obtained and set on the form seems to have been lost.
>
> If I map the success value of PersonSearchAction to /personSearch.do, it
> goes into a loop and I end up with a stack overflow.
>
> Any help would be gratefully received - I've been going round in circles.
> Why isn't the PersonSearchForm carrying the findAll results passed to the
> JSP?
>
> TIA,
> jim
>
> Relevant bit of struts-config.xml: -
> =======================
> <form-beans>
> <form-bean name="personSearchForm"
> type="com.paribus.hemlatta.clients.people.PersonSearchForm"/>
> </form-beans>
>
> <action-mappings>
> <action path="/personSearch"
> type="com.paribus.hemlatta.clients.people.PersonSearchAction"
> name="personSearchForm"
> scope="session"
> input="/findPeople.jsp"
> unknown="false"
> validate="false" >
> <forward name="success" path="/findPeople.jsp"/>
> </action>
> </action-mappings>
>
> findPeople.jsp : -
> ============
> <%@ page language="java" %>
> <%@ taglib uri="/WEB-INF/app.tld" prefix="app" %>
> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
>
> <html:html>
> <head>
> <jsp:useBean id="bean" scope="page"
> class="com.paribus.hemlatta.clients.people.PersonSearchForm"/>
> </head>
> <body>
> <h1><bean:message key="personSearch.title"/></h1>
> <html:errors/>
>
> <html:form action="/personSearch.do?action=exec">
> <html:hidden property="action" value="exec"/>
> <bean:message key="personSearch.label.searchStr"/> <html:text
> property="searchStr"/>
> <html:submit><bean:message
> key="personSearch.label.submit"/></html:submit><br>
>
> Got <%= bean.getResults().size() %> people from this search
> <ul>
> <logic:iterate id="element" name="bean" property="results">
> <li><bean:write name="element"/></li>
> </logic:iterate>
> </ul>
>
> </html:form>
> </body>
> </html:html>
>
> PersonSearchAction : -
> ================
> /* All the imports required... */
> public final class PersonSearchAction extends Action
> {
> public ActionForward perform( ActionMapping mapping,
> ActionForm form,
> HttpServletRequest req,
> HttpServletResponse res)
> throws IOException, ServletException
> {
> Locale locale = getLocale(req);
> MessageResources messages = getResources();
> HttpSession session = req.getSession();
> String action = req.getParameter("action");
>
> if(action == null)
> action = "init";
> if(servlet.getDebug() >= 1)
> servlet.log("PersonSearchAction: "+ action);
>
> if("init".equals(action))
> {
> ...
> }
> else if ("exec".equals(action))
> {
> if(servlet.getDebug() >= 1)
> servlet.log("Got exec successfully");
> if(form==null)
> {
> servlet.log("PersonSearchAction: ERROR! action \"exec\" cannot
> operate on "
> +" a null form");
> return mapping.findForward("error");
> }
> else
> {
> PersonSearchForm searchForm = (PersonSearchForm) form;
> // Blank search string implies "find all"
> servlet.log("searchForm.getSearchStr().length(): "+
> String.valueOf(searchForm.getSearchStr().length()) );
> if(searchForm.getSearchStr() == null ||
> searchForm.getSearchStr().length() < 1)
> {
> try
> {
> Context ic = new InitialContext();
> Object objref = ic.lookup("PersonEJB");
> PersonHome home =
> (PersonHome) PortableRemoteObject.narrow(objref,
> PersonHome.class);
> Collection all = home.findAll();
> if(servlet.getDebug() >= 1)
> servlet.log("Found "+ all.size() +" people");
> searchForm.setResults(all);
> }
> catch(NamingException e)
> {
> e.printStackTrace();
> }
> catch(FinderException e)
> {
> e.printStackTrace();
> }
> }
> return mapping.findForward("success");
> }
> }
>
> return mapping.findForward("success");
> }
> }
-- Ted Husted, Husted dot Com, Fairport NY USA.
-- Custom Software ~ Technical Services.
-- Tel 716 737-3463.
-- http://www.husted.com/about/struts/