How should multiple button support be handled in Struts? Each button gets submitted with different text, but in order to separate the view from the controller that text should not appear in the Action. I'm curious how others have dealt with this situation.

Ted Husted

Without Javascript, I don't think there is another solution, since that is all the information that HTTP provides.

With Javascript, you can have the button set a hidden property on the form, and then key on that.

This example uses the property "forward", which could also be a property on the ActionForm.

<html:hidden property="forward" value="error"/>
<html:submit onclick="set('update');">UPDATE</html:submit>
<html:submit onclick="set('copy');">COPY</html:submit>
<html:cancel onclick="set('cancel');">CANCEL</html:cancel>

function set(target) {
     document.forms[0].forward.value=target;
};

A good trick here is to use a generic "routing" or "relay" action, so you don't have to hardcode some type of switch into the Action. It's just one line in the perform method:

return mapping.findForward(request.getParameter("forward"));

This can be used any number of times in a Struts application, just by changing the form bean and the local forwards in its mapping.

<action 
path="/prospect/Submit"
type="org.apache.scaffold.http.RelayAction"
name="prospectForm"
scope="request"
validate="false">
<forward 
name="update"  
path="/do/prospect/Store"/> 
<forward 
name="cancel"  
path="/do/prospect/Current"/> 

HTH - Ted

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact