• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

html:image - running into an infinite loop

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic