File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes html:image - running into an infinite loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "html:image - running into an infinite loop" Watch "html:image - running into an infinite loop" New topic
Author

html:image - running into an infinite loop

Anuj Upadhyay
Greenhorn

Joined: Mar 17, 2004
Posts: 9
Hi,
I have read old mails regarding the html:image implementation and the
recommendations related to identifying which image is clicked in the form.
For
reference is used Ted's - 'Struts Tip #1 - Use an ImageButtonBean to
represent
an image button' @ http://husted.com/struts/tips/001.html
My problem is a step further in the same. I will try my best to explain it,
I
hope I get it right.
I have a mainpage which has many image buttons, they all map to different
actions that they must perform. Once the image is clicked, I identify the
image
and perform the relevant action by forwarding the call to the relevant
action
mapping. Most actions have a .jsp (UI) and action class and are defined in
the
struts-config.xml part shown below:
<struts-config>
<form-beans>
<form-bean name="dataForm" type="com.data.struts.form.DataForm">
</form-bean>
</form-beans>
<global-forwards>
<forward name="getData" path="/getData.do"/>
<forward name="resetData" path="/resetData.do"/>
</global-forwards>
<action-mappings>
<action path="/getData" type="com.data.struts.action.DataAction"
name="dataForm" scope="request" input="/jsp/dataForm.jsp">
<forward name="success" path="/jsp/dataForm.jsp"/>
</action>
<action path="/resetData" type="com.data.struts.action.ResetDataAction"
name="dataForm" scope="request" >
<forward name="success" path="/getData.do"/>
</action>
</action-mappings>
<message-resources parameter="ApplicationResources"/>
</struts-config>
In my case DataAction class receives the click event and forward the call to
/resetData.
//as below
if (dataForm.getRestButton().pressed() && errors == null) {
Log.debug("RESET BUTTON :"+ dataForm. getRestButton ().getName()
+", X : "+ dataForm. getRestButton ().getX()
+", Y : "+ dataForm. getRestButton ().getY());
return (mapping.findForward("resetData"));
}
The ResetDataAction process the request (does some thing) and must return
back
to the caller - forward to /getData finally.
//as below
mapping.findForward("getData")
All button handling working fine and I can do the processing in
ResetDataAction
but the problem start when I forward the call back to /getData. The call
forward
to DataAction class but the button click is identified and the call gets
forwarded back to /resetData and then back.... here I get into a loop.
I tried to reset on the form (which is common to both action classes) but no
success, I tried explicitly setting the button beans X & Y values to null,
still
no success.
What I could do was do the process I was doing in ResetDataAction in
DataAction
but that is not what I would like to do.
I want to keep the two implementations separate as one only delegates and
the
other does some processing. Can someone throw more light onto this? I have
been
reading a lot but have not come across a person who faced a similar problem.
It
may be something really basic but I can't solve it the way I want and it's
bothering me.
Any help on this will be much appreciated.
Thanks in advance,
Anuj
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4707
    
    7

Struts Cycle (partial):
- Request submitted in with parameters, action="action1.do"
- ActionServlet determines the mapping for "action1.do"
- ActionServlet determines the Action class, say Action1
- ActionServlet determines the ActionForm class, say Form1
- Form1.reset() method is invoked
- ActionServlet populates Form1 fields by calling set methods that have matching request parameters.
- ... and so on ...
It's a common misconception to think that any changes made to form fields will be reflected in the request parameters. This is not so. The values of the request parameters can not be changed. When you forward from one Action to another Action, the whole Struts Cycle starts over again. That means that the parameters that get pushed into the form associated with the second action (and any action you subsequently forward to while processing the same request) will always be the same values that were in the original request submission. This includes the image button values that you clicked. Calling the form's reset method will not help.
So what should you do? Refactor.
One approach would be to extract the code in the second action to a "helper" class. Then, instead of forwarding from the first Action to the second Action, invoke the extracted method in the helper class from the first Action. The second action will also invoke the helper class in the course of it's normal processing flow.

HTH


Junilu - [How to Ask Questions] [How to Answer Questions]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4707
    
    7

Just a clarification. The BEFORE example above doesn't really reflect your program structure because it won't get into an infinite loop like yours. However, the approach would be the same to break out of your loop. The main idea is to refactor the code that's in the subsequent Actions to a helper class. As much as possible, avoid forwarding from one Action to another as this is highly susceptible to the problem you are facing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: html:image - running into an infinite loop