Subject: Using the same Action for GET and POST?
From: "George Su" <[EMAIL PROTECTED]>
 ===

Hi, I'm still trying to get up to speed on Struts, and I'm trying to set up
an edit form.

I'm wondering, is it possible to use the same action mapping (same jsp page,
action, actionform) to handle both the GET of the page and the POST?  I do
not want to access the jsp directly on the GET.

In other words, I want /action/edit?name=foo to call my action, which I call
EditAction.  In EditAction.perform(...), if request.getMethod() is GET, I
want to populate my ActionForm bean with some data correlating to parameter
'name', so that edit.jsp can access that data.  Then I want edit.jsp called.
The problem here is, it goes into an infinite loop because the page keeps
forwarding to itself.  How can I tell it to just include edit.jsp instead of
forwarding to it?

When the edit form posts (to itself) I call EditAction.perform(...) and
since request.getMethod() returns POST, I call my business logic bean to
save the data, and forward to another page.

Is it possible to do it this way?  Is this strategy poor design?  Any advice
greatly appreciated.

Thanks,
George
[EMAIL PROTECTED]

(I'm using Struts 1.0)

In web.xml, /action/* maps to ActionServlet.

In struts-config.xml:

    <action
         path="/edit"
         type="cm.addressbook.web.EditAction"
         name="editForm"
         scope="request"
         input="/edit.jsp">
         <forward name="edit" path="/action/edit"/>
         <forward name="list" path="/list.jsp" redirect="true"/>
    </action>

In EditAction.java:

    public ActionForward perform(ActionMapping       mapping,
                                 ActionForm          form,
                                 HttpServletRequest  request,
                                 HttpServletResponse response)
        throws IOException, ServletException
    {
        if (request.getMethod().equals("GET")) {
            return doGet(mapping, form, request, response);
        }
        if (request.getMethod().equals("POST")) {
            // call business logic and then forward to home page
            return doPost(mapping, form, request, response);
        }
        return super.perform(mapping, form, request, response);
    }

    public ActionForward doGet(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
    {
        String name = request.getParameter("name");
        ((EditForm)form).setName(name);
        return mapping.findForward("edit");
    }





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

Reply via email to