You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Karl <ka...@webartjapan.com> on 2003/03/14 04:35:51 UTC

Search on one page, results on another

I'm trying to build a search page using struts, but I'm confused as to how to 
set it up properly...

I start with a search page search.jsp where the user fills out search 
criteria.
The criteria is read as SearchForm and passed to SearchAction.
SearchAction performs the search, and then gets a collection of objects.

At this point, I want it to go to the search results page and display the 
resulting collection of objects, but I'm not sure how I do that...
I've got a SearchResultsForm which has a getter/setter for a collection of my 
result objects, which I want do display in my searchresults.jsp page.

The only piece I'm missing is the method to make a SearchResultsForm in the 
SearchAction, put in the collection of result objects, and then pass that 
SearchResultsForm to a display action /view_results which will display the 
results on searchresults.jsp.
Since I only have the action form that was passed in, how do I pass on the new 
SearchResultsForm?  Or am I even going about this the right way?


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Search on one page, results on another

Posted by Mark <st...@webpit.com>.
Here's an example how to solve this problem.  I used Karapan Sapi to generate my first application from reverse engineering my database and it generated all the code and jsp pages.  I found the way that it does this to be unreliable however.  But the method they used to generate a paging list to be effective.


This will display and page through a list of log entries in my database

  private ActionForward performList(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
    try {
      DataSource ds = (DataSource) servlet.getServletContext().getAttribute("DATASOURCE");

      LogDAO logDAO = new LogDAO(ds);

      List logs = logDAO.list();

      int offset;
      int length = PAGE_LENGTH; /// how many entries per page?
      String pageOffset = request.getParameter("pager.offset");  // we keep track of which page we're on
      if (pageOffset == null || pageOffset.equals("")) {
        offset = 0;
      } else {
        offset = Integer.parseInt(pageOffset);
      }
      String url = request.getContextPath()+"/do"+mapping.getPath();
      String pagerHeader = Pager.generate(offset, logs.size(), length, url);

     // setup some request attributes so our jsp page can see them
      request.setAttribute("offset", new Integer(offset));
      request.setAttribute("pagerHeader", pagerHeader);
      request.setAttribute("length", new Integer(length));

      request.setAttribute("LOGS", logs);
    } catch (Exception e) {
      generalError(request, e);
      return mapping.findForward("failure");
    }

    return mapping.findForward("success");
  }



now in our jsp page we do this:

We are basically just fetching our list of Log objects from the request.  remember above we called our setAttribute field "LOGS"

Struts uses the offset value to skp to a specific page.  See the attached Pager.java for more information.  At the right of our list we display edit, view, delete links to the item.  The links are hyperlinked by the primary key of the log record in the db, not the index property of the iterate tag.  Although you could do that too (i actually use the second method in my "Show Cart" page to remove/update items in the session shopping cart rather than a db)

<table class="bodytable" width="100%" cellspacing="1" border="0">
<bean:write name="pagerHeader" scope="request" filter="false"/>
<logic:iterate id="log" offset="offset" length="length" name="LOGS" type="com.myapp.model.Log">

<tr>
<td align="left" valign="top" width="125" class="tableattributecolor"><span class="tablecelllabelbold">
<bean:write name="log" property="id" scope="page"/>
</span></td>

<td align="left" valign="top" width="125" class="tableattributecolor"><span class="tablecelllabelbold">
<bean:write name="log" property="logtext" scope="page"/>
</span></td>

<td align="left" valign="top" width="125" class="tableattributecolor"><span class="tablecelllabelbold">
<bean:write name="log" property="datestamp" scope="page"/>

</span></td>
<td align="left" valign="top" width="125" class="tableattributecolor"><span class="tablecelllabelbold">
<html:link href="edit" paramId="id" paramName="log" paramProperty="id"><bean:message key="label.edit"/></html:link>&nbsp;
<html:link href="view" paramId="id" paramName="log" paramProperty="id"><bean:message key="label.view"/></html:link>&nbsp;
<html:link href="remove" paramId="id" paramName="log" paramProperty="id"><bean:message key="label.remove"/></html:link>
</span></td>
</tr>
</logic:iterate>

</table>


Regards,
Mark Williamson

*********** REPLY SEPARATOR  ***********

On 03/14/2003 at 4:22 PM Karl wrote:

>I had a look in the archives for the past month but didn't see anything to do 
>with this =(
>
>If I use setAttribute, how do I get a handle on it from the jsp page?
>
>I also want the results to be displayed as a paginated list of links, which 
>means that the search results page needs an ActionForm of its own.  As well, 
>the individual links will go to an edit page, which will also need an 
>ActionForm of its own.  This problem looks like it is going to propagate 
>every time I need to use data submitted from one page as a lookup for 
>presenting data on another (which also may have an input form present).
>
>The FAQ suggests putting every piece of information I'd ever need into one 
>giant form object but this seems like an incredibly naive solution to a 
>paradigm issue...
>
>
>2003 3$B7n(B 14 $B6bMKF|(B 12:43$B!"(BMark $B$5$s$O=q$-$^$7$?(B:
>> I believe this question was asked and answered just about a week or two
>> ago....suggest you look in the archives, there are several answers
>>
>> all you really need to do right before your forward is
>>
>> request.setAttribute("searchresults",myCollection);
>>
>> and in your jsp just use iterate tag to walk through the results.
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: struts-user-help@jakarta.apache.org




Re: Search on one page, results on another

Posted by Kevin Williams <kw...@tarity.com>.
Karl,

I might be way off base here, but my opinion is that you can't use forms
when displaying an unknown quantity of data.  (Or if you can, it is
easier not to).  Forms represent one set of data.  I have yet to read of
struts having a form collection (and I'm new so if there is one, let me
know!) to handle this scenario.  What I've done to solve this issue is
to create some custom iteration tags that handle a vector.  Within this
tag, I specify the vector's name.  In my action class I retrieve the
vector, and set it in the page context (either request, or if necessary,
session scope).

This has worked quite nicely for me.  Not everything HAS to be in a form
object.

Kevin

On Thu, 2003-03-13 at 23:22, Karl wrote:
> I had a look in the archives for the past month but didn't see anything to do 
> with this =(
> 
> If I use setAttribute, how do I get a handle on it from the jsp page?
> 
> I also want the results to be displayed as a paginated list of links, which 
> means that the search results page needs an ActionForm of its own.  As well, 
> the individual links will go to an edit page, which will also need an 
> ActionForm of its own.  This problem looks like it is going to propagate 
> every time I need to use data submitted from one page as a lookup for 
> presenting data on another (which also may have an input form present).
> 
> The FAQ suggests putting every piece of information I'd ever need into one 
> giant form object but this seems like an incredibly naive solution to a 
> paradigm issue...
> 
> 
> 2003 3月 14 金曜日 12:43、Mark さんは書きました:
> > I believe this question was asked and answered just about a week or two
> > ago....suggest you look in the archives, there are several answers
> >
> > all you really need to do right before your forward is
> >
> > request.setAttribute("searchresults",myCollection);
> >
> > and in your jsp just use iterate tag to walk through the results.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Search on one page, results on another

Posted by Karl <ka...@webartjapan.com>.
I had a look in the archives for the past month but didn't see anything to do 
with this =(

If I use setAttribute, how do I get a handle on it from the jsp page?

I also want the results to be displayed as a paginated list of links, which 
means that the search results page needs an ActionForm of its own.  As well, 
the individual links will go to an edit page, which will also need an 
ActionForm of its own.  This problem looks like it is going to propagate 
every time I need to use data submitted from one page as a lookup for 
presenting data on another (which also may have an input form present).

The FAQ suggests putting every piece of information I'd ever need into one 
giant form object but this seems like an incredibly naive solution to a 
paradigm issue...


2003 3月 14 金曜日 12:43、Mark さんは書きました:
> I believe this question was asked and answered just about a week or two
> ago....suggest you look in the archives, there are several answers
>
> all you really need to do right before your forward is
>
> request.setAttribute("searchresults",myCollection);
>
> and in your jsp just use iterate tag to walk through the results.


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Search on one page, results on another

Posted by Mark <st...@webpit.com>.
I believe this question was asked and answered just about a week or two ago....suggest you look in the archives, there are several answers

all you really need to do right before your forward is

request.setAttribute("searchresults",myCollection);

and in your jsp just use iterate tag to walk through the results.

*********** REPLY SEPARATOR  ***********

On 03/14/2003 at 12:35 PM Karl wrote:

>I'm trying to build a search page using struts, but I'm confused as to how to 
>set it up properly...
>
>I start with a search page search.jsp where the user fills out search 
>criteria.
>The criteria is read as SearchForm and passed to SearchAction.
>SearchAction performs the search, and then gets a collection of objects.
>
>At this point, I want it to go to the search results page and display the 
>resulting collection of objects, but I'm not sure how I do that...
>I've got a SearchResultsForm which has a getter/setter for a collection of my 
>result objects, which I want do display in my searchresults.jsp page.
>
>The only piece I'm missing is the method to make a SearchResultsForm in the 
>SearchAction, put in the collection of result objects, and then pass that 
>SearchResultsForm to a display action /view_results which will display the 
>results on searchresults.jsp.
>Since I only have the action form that was passed in, how do I pass on the new 
>SearchResultsForm?  Or am I even going about this the right way?
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: struts-user-help@jakarta.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org