Hi Erik,

you solution is very interesting, but I am not absolutely happy with the
fact,
that control is specified not only in struts-config.xml but in source code
as well (map definition). Even application properties play here an important
role.

I have tried to use your ideas and produced very simple solution which would
satisfy
both buttons and images needs. This is just an idea, so any feedback is
welcome.

struts-config.xml
=================

    <action    path="/test"
               type="org.apache.struts.actions.MultiSubmitAction"
               name="testForm"
              scope="request"
              input="/test.jsp">
      <forward name="add.x"         path="/add.do"/>
      <forward name="delete.x"      path="/delete.do"/>
    </action>

    <action    path="/add"
               type="org.apache.struts.webapp.example.AddAction"
               name="testForm"
              scope="request"
              input="/test.jsp">
      <forward name="success"         path="/add_results.jsp"/>
    </action>

    <action    path="/delete"
               type="org.apache.struts.webapp.example.DeleteAction"
               name="testForm"
              scope="request"
              input="/test.jsp">
      <forward name="success"         path="/delete_results.jsp"/>
    </action>

test.jsp
========

    <html:form action="/test">
          <!-- BUTTON -->
        <html:submit property="add.x">
            <bean:message key="button.add"/>
        </html:submit>

          <!-- IMAGE -->
          <html:image page="/images/delete.gif" property="delete" />
    </html:form>


MultiSubmitAction.java
======================

public class MultiSubmitAction extends Action {

    public ActionForward perform(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
        throws IOException, ServletException {

        String forwards[] = mapping.findForwards();

        for (int i = 0; i < forwards.length; i++) {
            if (request.getParameter(forwards[i]) != null) {
                // Return the required ActionForward instance
                return mapping.findForward(forwards[i]);
            }
        }

        // Go back to input (any other ideas ?)
        return new ActionForward(mapping.getInput());

    }
}

Some considerations:

If you never intend to use images, then you can omit ".x" suffix.

If somebody wants to have all methods stored in one file he can subclass
DispatchAction
and play with parameter property:

    <action    path="/add"
               type="org.apache.struts.webapp.example.AddDeleteAction"
               name="testForm"
              parameter="add"
              scope="request"
              input="/test.jsp">
    </action>

    <action    path="/delete"
               type="org.apache.struts.webapp.example.AddDeleteAction"
               name="testForm"
              parameter="delete"
              scope="request"
              input="/test.jsp">
    </action>


Instead of calling return mapping.findForward(forwards[i]) it might be
interesting to instantiate the action, similar to as it is done in
ActionServlet
(protected Action processActionCreate) and just call it's perform() method.
It would be faster for sure.

Instead of 'misusing' mappings it might be possible to introduce some
additional tag
to action config:

      <dispatch property="delete.x" path="/delete.do"/>
or
      <dispatch property="delete.x" method="delete"/>


Dmitri Valdin


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to