This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Struts and the fly likes Form to display search results on same page Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Frameworks » Struts
Reply Bookmark "Form to display search results on same page" Watch "Form to display search results on same page" New topic
Author

Form to display search results on same page

hauss timmons
Greenhorn

Joined: Jan 31, 2005
Posts: 2
Newbie to struts here.

I'm trying to create a search form that, when submitted, displays the search results on the same page. Right now I am encountering an infinite loop due to my action mapping (included below). What's happening (I stepped through using remote debugging) is that my actionform validate AND my actual action which does the search is being executed even before the page is rendered. I can understand the actionform but why is the action being executed? The action returns to the success mapping which is the same page.. resulting in the infinite loop. Incase it gets asked.. I do have to have them both on the same page.

What is the "best practices" way to tell an action not to execute unless submit is clicked.. query string parameters for example?


-- exceprt from struts-config.xml
<form-bean name="search"
type="forms.searchFormBean"/>

...

<action path="/Search"
type="actions.MySearchAction"
input="/Search.do"
name="search"
scope="request">
<forward name="success" path="/Search.do"/>
</action>

--- search.jsp
<html:form action="Search.do" >
<html:text property="keywords"/><br/>
<html:submit>Search</html:submit><br/>
</html:form>
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Both your input and your ActionForward are mapped back to MySearchAction. Try pointing them to a jsp instead of an Action. What's happening is you are chaining the Action to itself, the result of which is as you've noticed, an infinite loop.


Jason's Blog
hauss timmons
Greenhorn

Joined: Jan 31, 2005
Posts: 2
Hmm, I am also using Tiles (which I am a newbie at as well). My tiles def for the search page is

<definition name="SearchPage" extends="MainLayout" >
<put name="title" value="Search"/>
<put name="body" value="/search.jsp" />
</definition>

And.. the loop also happened when the input attribute was not there on the action mapping.

So, should I make the link *to* this search page (e.g. the link from other pages) go to the SearchPage tile (not sure how to do that).. right now I link right to Search.do which I guess is why the action is executing?

Thanks for such a quick response by the way!
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
The input parameter of the <action> element refers to the page that called the Action. So if your search form is calling the action, then your input parameter should point back to that page. If that's the case, your action mapping in the struts-config should look something like this:



Your search page view, as you've named it in your tiles-def, is SearchPage. Therefore that is the view that you need to forward to on success, as illustrated in the above code fragment.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Form to display search results on same page
 
Similar Threads
Frustration with struts. Please help!!!
LookupDispatchAction - Missing message
Action method is not getting invoked
Struts1.2_ Exception
Cannot retrieve mapping for action /content/AddLinks