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>
JSP

Avatar of undefined
Last Comment
komlaaa

8/22/2022 - Mon
brunoguimaraes

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).
summerian

I agree with brunoguimaraes, you posted wrong action definition (orderCreated instead of createCustomer).  Post the relevant one.
ASKER
komlaaa

Sorry:

    <action
      path="/createCustomer"      
      name="createCustomerForm"
      input="/form/createCustomer.jsp"
      scope="request"      
      type="actions.CreateCustomerAction">
      <forward name="customerCreated" path="/form/createOrder.jsp" />
    </action>
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER
komlaaa

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");

      }

}
brunoguimaraes

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

brunoguimaraes;

I don't think that is true. May wrong naming but notice  "customerCreated" versus "createCustomer"
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
summerian

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
brunoguimaraes

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".
ASKER
komlaaa

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

i mean i am been forward to the failure.jsp page,... i don't know why it is failing to the success page
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
summerian

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

ASKER
komlaaa

Excellent Help,... I appreciate it