Link to home
Start Free TrialLog in
Avatar of komlaaa
komlaaa

asked on

<action forward struts

using struts 1.1-- The request http://localhost:8085/wptStrutsHib/createCustomer.do
is taking to a blank page instead of taking to "/views/orderCreated.jsp"  as specific in struts action below:

            
                  <form-bean
                        name="createCustomerForm"
                        type="org.apache.struts.action.DynaActionForm">
                        <form-property name="firstName" type="java.lang.String"></form-property>
                        <form-property name="lastName" type="java.lang.String"></form-property>
                        <form-property name="email" type="java.lang.String"></form-property>
                  </form-bean>

 <action
      path="/orderCreated"
      name="orderCreatedForm"
      input="/form/createOrder.jsp"
      scope="request"    
      type="actions.OrderCreatedAction">
      <forward name="orderCreated" path="/views/orderCreated.jsp" />
    </action>


============ The JSP page ==================
      <body>
            <html:form action="/createCustomer">
                  Email : <html:text property="email"/><html:errors property="email"/><br/>
                  FirstName : <html:text property="firstName"/><html:errors property="firstName"/><br/>
                  LastName : <html:text property="lastName"/><html:errors property="lastName"/><br/>
                  <html:submit/><html:cancel/>
            </html:form>
      </body>
Avatar of brunoguimaraes
brunoguimaraes
Flag of Brazil image

The code you posted is referrint to an action called orderCreated.

The request http://localhost:8085/wptStrutsHib/orderCreated.do would take you to the desired page.

Either this was the mistake you made, or you'll have to post the correct action configuration (for the action createCustomer).
I agree with brunoguimaraes, you posted wrong action definition (orderCreated instead of createCustomer).  Post the relevant one.
Avatar of komlaaa
komlaaa

ASKER

Sorry:

    <action
      path="/createCustomer"      
      name="createCustomerForm"
      input="/form/createCustomer.jsp"
      scope="request"      
      type="actions.CreateCustomerAction">
      <forward name="customerCreated" path="/form/createOrder.jsp" />
    </action>
Avatar of komlaaa

ASKER

Also ActionClass:

public abstract class AbstractAction extends Action {

            public ActionForward execute(
                  ActionMapping mapping,
                  ActionForm form,
                  HttpServletRequest request,
                  HttpServletResponse response)
                  throws Exception {

                  // Define action errors and forward
                  ActionErrors errors = new ActionErrors();
                  ActionForward forward = new ActionForward();
                  
                  try {
                            forward = performAction(mapping, form, request, response);
                  } catch (Exception e) {
                        // Report the error using the appropriate name and ID.
                        errors.add("name", new ActionError("id"));
                  }

                  // If a message is required, save the specified key(s)
                  // into the request for use by the <struts:errors> tag.
                  
                  //Saves the error
//Listing 4: Contents of new performAction method.
                  if (!errors.empty()) {
                        saveErrors(request, errors);
                        // Forward control to the appropriate 'failure' URI
                        forward = mapping.findForward("failure");
                  } else {
                        // Forward control to the appropriate 'success' URI
                        if (forward == null) {
                              forward = mapping.findForward("success");
                        }
                  }

                  // Finish with
                  return (forward);

            }




================ subclass ==================
public class CreateCustomerAction extends AbstractAction {

      public ActionForward performAction(ActionMapping mapping, ActionForm form,
                  HttpServletRequest request, HttpServletResponse response)
                  throws Exception {
            
          DynaActionForm createCustomerForm = (DynaActionForm) form;// TODO Auto-generated method stub

            // TODO Auto-generated method stub
            String fn = (String) createCustomerForm.get("firstName");
            String ln = (String) createCustomerForm.get("lastName");
        String ad = (String) createCustomerForm.get("address");
        int custId = Math.abs( (int)System.currentTimeMillis());
       
        domain.Customer currentCustomer  = new Customer();
        currentCustomer.setFirstName(fn);
        currentCustomer.setLastName(ln);
        currentCustomer.setAddress(ad);
        currentCustomer.setId(custId);
       
    try {
                  // perform list customer operations
                  CommandExecutor.getInstance().executeHibernateCommand(
                                          new CreateCustomer(currentCustomer));
                  request.setAttribute("customer",currentCustomer );
                        
            }
          catch (Exception e)
          {
                  throw new ServletException(e);
            }

            return mapping.findForward("customerCreated");

      }

}
Forward "customerCreated" is redirecting to the same page the action was called (/form/createOrder.jsp). Is this the desired behaviour?
Avatar of komlaaa

ASKER

brunoguimaraes;

I don't think that is true. May wrong naming but notice  "customerCreated" versus "createCustomer"
ASKER CERTIFIED SOLUTION
Avatar of summerian
summerian
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is your action definition:

<action
      path="/createCustomer"      
      name="createCustomerForm"
      input="/form/createCustomer.jsp"
      scope="request"      
      type="actions.CreateCustomerAction">
      <forward name="customerCreated" path="/form/createOrder.jsp" />
</action>

The forward name is called "customerCreated", and it forwards do the page /form/createOrder.jsp.

Now your action class:

public class CreateCustomerAction extends AbstractAction {

public ActionForward performAction(ActionMapping mapping, ActionForm form,
                  HttpServletRequest request, HttpServletResponse response)
                  throws Exception {
           
        DynaActionForm createCustomerForm = (DynaActionForm) form;

        String fn = (String) createCustomerForm.get("firstName");
        String ln = (String) createCustomerForm.get("lastName");

        String ad = (String) createCustomerForm.get("address");
        int custId = Math.abs( (int)System.currentTimeMillis());
       
        domain.Customer currentCustomer  = new Customer();
        currentCustomer.setFirstName(fn);
        currentCustomer.setLastName(ln);
        currentCustomer.setAddress(ad);
        currentCustomer.setId(custId);
       
    try {
                  CommandExecutor.getInstance().executeHibernateCommand(new CreateCustomer(currentCustomer));
                  request.setAttribute("customer",currentCustomer );
                       
    }  catch (Exception e) {
                  throw new ServletException(e);
    }

     return mapping.findForward("customerCreated");

}
      
As you see on the last line, it returns the same forward, "customerCreated".
Avatar of komlaaa

ASKER

Summerian, you are right,... i added the failure page but now i don't really know what happened?
Avatar of komlaaa

ASKER

i mean i am been forward to the failure.jsp page,... i don't know why it is failing to the success page
Add the following lines - code below.

This is not a good pattern to follow, but it will show you why exception is thrown.

But. I must point out that this kind of pattern you were applying swallows exceptions - you don't have stacktrace in app logs, or any error message on pages. You must add something like e.printStacktrace() to your catch clause, to have some info in app logs afterwards, or you will never have a clue what's happening if something's gone wrong.
try {
    CommandExecutor.getInstance().executeHibernateCommand(new CreateCustomer(currentCustomer));
    request.setAttribute("customer",currentCustomer );
                       
}  catch (Exception e) {
    request.setAttribute("errorMessage", e.getMessage());    
    throw new ServletException(e);
}
 
(failure.jsp)
error reason: <%=request.getAttribute("errorMessage")%>

Open in new window

Avatar of komlaaa

ASKER

Excellent Help,... I appreciate it