Change
public String getSelected() { if (getCreateButton().isSelected()) { return "create"; } if (getRetrieveButton().isSelected()) { return "retrieve"; } if (getUpdateButton().isSelected()) { return "update"; } if (getDeleteButton().isSelected()) { return "delete"; } return null; } to
public String getSelected() {
if (getCreateButton().isSelected()) { createButton = null; return "create"; }
if (getRetrieveButton().isSelected()) { retrieveButton = null; return "retrieve"; }
if (getUpdateButton().isSelected()) { updateButton = null; return "update"; }
if (getDeleteButton().isSelected()) { deleteButton = null; return "delete"; }
return null;
}
However, in this case you must create the buttons with the getWhateverMethod() as follows:
public ImageButtonBean getWhateverButton() { return new ImageButtonBean(); }
Which means, of course, that you can now get rid of the initial instantiation of the Button Beans. In fact, you can dispense with that altogeher, since you are coming close to the suggested improvement, which is still not the best solution, in my opinion.
Michael McGrady
Michael McGrady wrote:
First, the problem is that you are bloating your system with the ImageButtonBeans in the first place. You don't have to do that. But, once you do it, you have the problems you state. You can use various other simpler and less resource intensive solutions linked to at http://wiki.apache.org/struts/StrutsCatalogVariousButtonSolutions . You will find there the following solution, for those of you who like the button approach which should automatically solve all your problems. Below are two approaches that use Buttons. The first does not use the x and y values to determine that button was used but, rather, uses them to destroy the button, automatically solving your issue. The second is the sort of solution you are using. I would, however, recommend that you use something different from either of these. You can see the options at the link noted.
Michael McGrady
package com.crackwillow.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionMessages;
import com.crackwillow.log.StdOut;
public class LogonButtonForm extends ActionForm { protected InnerButton button; protected String methodCalled; protected String username; protected String password;
public LogonButtonForm() { }
public void reset(ActionMapping mapping, HttpServletRequest request) { methodCalled = null; username = null; password = null; }
public String getMethodCalled() { return methodCalled; } public String getUsername() { return username; } public String getPassword() { return password; }
public void setMethodCalled(String methodCalled) { this.methodCalled = methodCalled; }
public void setUsername(String username) { this.username = username; }
public void setPassword(String password) { this.password = password; }
public InnerButton getButton() { this.button = new InnerButton(); return button; }
public void reset() { button = null; }
public class InnerButton {
private Integer x, y;
public InnerButton() { }
public InnerButton getCreate() { methodCalled = "create"; return button; }
public InnerButton getRetrieve() { methodCalled = "retrieve"; return button; }
public InnerButton getUpdate() { methodCalled = "update"; return button; }
public InnerButton getDelete() { methodCalled = "delete"; return button; }
public void setX(Integer x) { if(y == null) { reset(); } else { this.x = x; } }
public void setY(Integer y) { if(x == null) { reset(); } else { this.y = y; } } }
Compare this to what you are using, which is something like:
package com.crackwillow.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionMessages; import org.apache.struts.util.ImageButtonBean;
import com.crackwillow.log.StdOut;
public class LogonImageButtonBeanForm extends ActionForm { protected String methodCalled; protected String username; protected String password; private ImageButtonBean createButton = new ImageButtonBean(); private ImageButtonBean retrieveButton = new ImageButtonBean(); private ImageButtonBean updateButton = new ImageButtonBean(); private ImageButtonBean deleteButton = new ImageButtonBean();
public LogonImageButtonBeanForm() { }
public ImageButtonBean getRetrieveButton() { return retrieveButton; } public ImageButtonBean getDeleteButton() { return deleteButton; } public ImageButtonBean getUpdateButton() { return updateButton; } public ImageButtonBean getCreateButton() { return createButton; } public String getMethodCalled() { return methodCalled; } public String getUsername() { return username; } public String getPassword() { return password; }
public void setCreateButton(ImageButtonBean button) { this.createButton = button; }
public void setRetrieveButton(ImageButtonBean button) { this.retrieveButton = button; }
public void setUpdateButton(ImageButtonBean button) { this.updateButton = button; }
public void setDeleteButton(ImageButtonBean button) { this.deleteButton = button; }
public void setMethodCalled(String methodCalled) { this.methodCalled = methodCalled; }
public void setUsername(String username) { this.username = username; }
public void setPassword(String password) { this.password = password; }
public String getSelected() { if (getCreateButton().isSelected()) { return "create"; } if (getRetrieveButton().isSelected()) { return "retrieve"; } if (getUpdateButton().isSelected()) { return "update"; } if (getDeleteButton().isSelected()) { return "delete"; } return null; }
public void reset(ActionMapping mapping, HttpServletRequest request) { methodCalled = null; username = null; password = null; }
} ///;-)
} ///;-)
[EMAIL PROTECTED] wrote:
Greetings,
I have searched the lists for some time without any success.
I have a Session Form Bean (this is necessay to do a unit of work that spans many pages...)
On a page I have 3 ImageButtonBeans. Update, Cancel and Beneficiaries.
My problem arises in the Validate method of the Form.
In order to Update the user must have a beneficiary.
So the user tries to update and a message stating that a beneficiary is required.
The user then clicks on the Beneficiaries button... the validate method is executed...
and the Update button still has its X and Y values and the request fails the validation and the message is redisplayed.
This loop goes on...
I have tried to set the Update button to a new ImageButtonBean.. same problem
I have also tried to update the session Form bean after setting the Update button to a new ImageButtonBean with no luck.
How can I get around this situation?
TIA,
Glenn
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]