[Opensource] Calling two states within the same request
Peter A. J. Pilgrim
peterp at xenonsoft.demon.co.uk
Mon Sep 9 15:40:05 PDT 2002
Malcolm Wise wrote:
> Hi,
>
> I don't know if this is a bug or I'm doing something stupid, but here
> goes...
>
> I have a jsp defined as follows:
>
> <tiles:insert page="/layouts/excalibur-basic-layout.jsp" flush="true">
> <tiles:put name="title" value="projects.title" />
> <tiles:put name="header" value="/includes/header.jsp" />
> <tiles:put name="search" value="/projects/Search/Display.do" />
> <tiles:put name="body" value="/projects/jsp/homebody.jsp" />
> <tiles:put name="footer" value="/includes/footer.jsp" />
> </tiles:insert>
>
> The /projects/Search/Display.do action maps to a controller
> 'ProjectSearch.java', state 'promptSearch' (the initial state) which
> generates a search form with a submit button. The submit button submits the
> form to /projects/Search.do which is also mapped to 'ProjectSearch.java',
> state 'search' which essentially finds database rows matching the entered
> search criteria and displays them. So far, so good.
> the /projects/Search.do mapping has a forward 'search' defined which
> forwards to the following jsp (projectlist.jsp) to show the search results:
>
> <tiles:insert page="/layouts/excalibur-basic-layout.jsp" flush="true">
> <tiles:put name="title" value="projects.title" />
> <tiles:put name="header" value="/includes/header.jsp" />
> <tiles:put name="search" value="/projects/Search/Display.do" />
> <tiles:put name="body" value="/projects/jsp/projectlistbody.jsp" />
> <tiles:put name="footer" value="/includes/footer.jsp" />
> </tiles:insert>
>
> As you can see, this page needs to show the search form too (as do all my
> other pages). Here is where the problem occurs. When the controller
> 'ProjectSearch.java' gets invoked for the second time (from projectlist.jsp)
> to set up the form, it calls the 'search' state instead of the
> 'promptSearch' state. I've tried changing the <tiles:put name=search value
> to /projects/Search/Display.do?state=promptSearch but to no avail. I guess
> it's still using the state value from the submitted form. If this isn't a
> bug, then any suggestions appreciated as to how to get around this!
I am not sure what you're trying doing? Including the search form as Tile
component. I think it will fail because there is no way to override
`request.getParameter("x")' in JSP 1.2. In other words there is no
`request.setParameter("x", 'whatever' )' call. Hence the failure.
I think. Since the form input parameter from the web page to web server
are the same. Struts ActionServlet constructs the ActionForm parameter
from the Servlet Request parameters each and every time ( web client
request ).
Try an alternative design. Just use one page, and use conditional logic
tags. I would make ActionForm with a java collection type ( java.util.List)
inside it.
class SearchPageForm {
java.util.List results;
...
public List getResults() { ... }
public void setResults( List list ) { .. }
}
Retrieve the results of the search as List collection and store them in the
action form and then forward to the results page.
Your JSP thus contains both the search form and conditionally the results
as well. Use the Struts logic conditional to check for the presence,
and as also the size of the results. If you get zero results then
tell the user.
<%-- Your search form --%>
<logic:present name="searchForm" value="results" >
<logic:greaterThen name="searchForm" value="results.size" value="0">
<%-- Display results --%>
</logic:/greaterThen>
<logic:equals name="searchForm" value="results.size" value="0">
<font color="red"> Sorry, I could n't match
your search criteria. No results </font>
</logic:/equals>
</logic:present>
HTH
--
Peter Pilgrim +-----\ +-++----++----+
Java Technologist | | | || || | 'n' Shine
| O | | || --+| ---+
/\ | ._ / | | \ \ | |
/ \ | | \ \ | |+-- || ---+ A new day
/_ _\ "Up" | | | | | || || | is coming
|| +-+ +-+ +-++----++----+
<home page="http://www.xenonsoft.demon.co.uk/" />
More information about the Opensource
mailing list