« Finally.. | Main | Picture organizer script »
December 03, 2004
Struts LookupDispatchAction
I started investingating struts yesterday and found it somewhat frustrating. one of the things I wanted to do was have submit buttons that have the same name, but have the request handler detect which button was pressed.. clearly you can do this using javascript.. but some browsers may have javascript disabled.
The Jakarta Struts project promises this capability using their framework. One of the helper action classes was the LookupDispatchAction, but using it wasn't as straightforward as I hoped. Essentially I wanted to create a simple forward/back wizard that would submit a post inbetween pages. The LookupDispatchAction class behaves similarly to how HTML radio buttons work, where you have a similar group name, but each property has a different value (e.g. young, old, dead). In my case, my group was called "direction" with submit values of "next", "back", and "cancel"
Without going through all the gory details, I've provided the source on how to do this. After googling around I couldn't find any concrete samples, other than code snippets that left me wondering like, "should I still provide the execute(...) method in my LookupDispatchAction subclass because none of my direction methods were being invoked." I kept getting errors like: "Request[/step1] does not contain handler parameter named 'direction'. This may be caused by whitespace in the label text"
I also placed an entry in tss, without much luck.
Anyway, here's the code. It's the complete web application including the humongous 2Mb struts libs. (I'm using struts 1.24) I have build scripts for both tomcat 4.1x and tomcat 5.5x
Posted by patrick at December 3, 2004 05:24 PM
Comments
Thanks for creating this small and useful solution.
Note however that the default distribution of ant 1.6.1 doesn't include catalina-ant.jar, which is necessary for building your build file.
It isn't a huge problem, but still it is nice to know that the jar is necessary on top of the default distribution.
Posted by: arnold at March 18, 2005 03:06 AM
Thanks for the catch.. I've actually stopped using struts and gone with JSF instead. New projects, in my opinion, would be better suited going with JSF.
Posted by: Patrick
at March 19, 2005 04:06 PM
That's a very good example! Thanks! Do you know if I can also use this for image buttons?
For instance
works with Firefox but not with Internet Explorer because IE doens'transfer direction=Next.
Regards, Uwe
Posted by: Uwe at May 29, 2005 02:36 AM
That's a very good example! Thanks! Do you know if I can also use this for image buttons?
For instance
works with Firefox but not with Internet Explorer because IE doens'transfer direction=Next.
Regards, Uwe
Posted by: Uwe at May 29, 2005 02:45 AM
don't know if you solved it (haven't looked at the code), but as a record for the reader: If you get that "Request[/step1] does not contain handler parameter named 'direction'. This may be caused by whitespace in the label text" error, you forgot to add the "parameter" value to the struts-config.xml.
Here's a working example:
http://husted.com/struts/tips/002.html
For the reader: you must not define your execute() function, otherwise you'd overwrite LookupDispatchAction's execute() function, that already does all the magic for you.
Posted by: Alex at June 19, 2005 07:58 AM
I just found a fix for the error that occurs when no button property is found.
Error
"Request[/step1] does not contain handler parameter named 'direction'. This may be caused by whitespace in the label text"
This occurs when a user attempts to access the page using a bookmark where the LookupDispatchAction is looking for a button property. When it is NULL the above exception is thrown.
To fix the issue add a default action by adding a method named "unspecified"
Example
public ActionForward unspecified(ActionMapping
mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
return mapping.findForward("previous");
}
This default action will be ran when no button was clicked to get to the page.
I just figured this out and wanted to share. Hope this helps someone.
Source:
http://struts.apache.org/api/org/apache/struts/actions/DispatchAction.html
Posted by: Matt at July 7, 2005 09:30 AM
I also had this error in my web application. My page always worked with Firefox and failed in IE about 50% of the time with the error
"Request[/user] does not contain handler parameter named 'method'. This may be caused by whitespace in the label text"
The correction was in the HTML around the submit button. I had:
' />
I just needed to remove the extraneous tag and that fixed it!
Posted by: MarkCL at September 9, 2005 11:38 AM