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.ac tion.DynaA ctionForm" >
<form-property name="firstName" type="java.lang.String"></ form-prope rty>
<form-property name="lastName" type="java.lang.String"></ form-prope rty>
<form-property name="email" type="java.lang.String"></ form-prope rty>
</form-bean>
<action
path="/orderCreated"
name="orderCreatedForm"
input="/form/createOrder.j sp"
scope="request"
type="actions.OrderCreated Action">
<forward name="orderCreated" path="/views/orderCreated. jsp" />
</action>
============ The JSP page ==================
<body>
<html:form action="/createCustomer">
Email : <html:text property="email"/><html:er rors property="email"/><br/>
FirstName : <html:text property="firstName"/><htm l:errors property="firstName"/><br/ >
LastName : <html:text property="lastName"/><html :errors property="lastName"/><br/>
<html:submit/><html:cancel />
</html:form>
</body>
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.ac
<form-property name="firstName" type="java.lang.String"></
<form-property name="lastName" type="java.lang.String"></
<form-property name="email" type="java.lang.String"></
</form-bean>
<action
path="/orderCreated"
name="orderCreatedForm"
input="/form/createOrder.j
scope="request"
type="actions.OrderCreated
<forward name="orderCreated" path="/views/orderCreated.
</action>
============ The JSP page ==================
<body>
<html:form action="/createCustomer">
Email : <html:text property="email"/><html:er
FirstName : <html:text property="firstName"/><htm
LastName : <html:text property="lastName"/><html
<html:submit/><html:cancel
</html:form>
</body>
I agree with brunoguimaraes, you posted wrong action definition (orderCreated instead of createCustomer). Post the relevant one.
ASKER
Sorry:
<action
path="/createCustomer"
name="createCustomerForm"
input="/form/createCustome r.jsp"
scope="request"
type="actions.CreateCustom erAction">
<forward name="customerCreated" path="/form/createOrder.js p" />
</action>
<action
path="/createCustomer"
name="createCustomerForm"
input="/form/createCustome
scope="request"
type="actions.CreateCustom
<forward name="customerCreated" path="/form/createOrder.js
</action>
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("failu re");
} else {
// Forward control to the appropriate 'success' URI
if (forward == null) {
forward = mapping.findForward("succe ss");
}
}
// Finish with
return (forward);
}
================ subclass ==================
public class CreateCustomerAction extends AbstractAction {
public ActionForward performAction(ActionMappin g 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("fi rstName");
String ln = (String) createCustomerForm.get("la stName");
String ad = (String) createCustomerForm.get("ad dress");
int custId = Math.abs( (int)System.currentTimeMil lis());
domain.Customer currentCustomer = new Customer();
currentCustomer.setFirstNa me(fn);
currentCustomer.setLastNam e(ln);
currentCustomer.setAddress (ad);
currentCustomer.setId(cust Id);
try {
// perform list customer operations
CommandExecutor.getInstanc e().execut eHibernate Command(
new CreateCustomer(currentCust omer));
request.setAttribute("cust omer",curr entCustome r );
}
catch (Exception e)
{
throw new ServletException(e);
}
return mapping.findForward("custo merCreated ");
}
}
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("failu
} else {
// Forward control to the appropriate 'success' URI
if (forward == null) {
forward = mapping.findForward("succe
}
}
// Finish with
return (forward);
}
================ subclass ==================
public class CreateCustomerAction extends AbstractAction {
public ActionForward performAction(ActionMappin
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("fi
String ln = (String) createCustomerForm.get("la
String ad = (String) createCustomerForm.get("ad
int custId = Math.abs( (int)System.currentTimeMil
domain.Customer currentCustomer = new Customer();
currentCustomer.setFirstNa
currentCustomer.setLastNam
currentCustomer.setAddress
currentCustomer.setId(cust
try {
// perform list customer operations
CommandExecutor.getInstanc
new CreateCustomer(currentCust
request.setAttribute("cust
}
catch (Exception e)
{
throw new ServletException(e);
}
return mapping.findForward("custo
}
}
Forward "customerCreated" is redirecting to the same page the action was called (/form/createOrder.jsp). Is this the desired behaviour?
ASKER
brunoguimaraes;
I don't think that is true. May wrong naming but notice "customerCreated" versus "createCustomer"
I don't think that is true. May wrong naming but notice "customerCreated" versus "createCustomer"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here is your action definition:
<action
path="/createCustomer"
name="createCustomerForm"
input="/form/createCustome r.jsp"
scope="request"
type="actions.CreateCustom erAction">
<forward name="customerCreated" path="/form/createOrder.js p" />
</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(ActionMappin g mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
DynaActionForm createCustomerForm = (DynaActionForm) form;
String fn = (String) createCustomerForm.get("fi rstName");
String ln = (String) createCustomerForm.get("la stName");
String ad = (String) createCustomerForm.get("ad dress");
int custId = Math.abs( (int)System.currentTimeMil lis());
domain.Customer currentCustomer = new Customer();
currentCustomer.setFirstNa me(fn);
currentCustomer.setLastNam e(ln);
currentCustomer.setAddress (ad);
currentCustomer.setId(cust Id);
try {
CommandExecutor.getInstanc e().execut eHibernate Command(ne w CreateCustomer(currentCust omer));
request.setAttribute("cust omer",curr entCustome r );
} catch (Exception e) {
throw new ServletException(e);
}
return mapping.findForward("custo merCreated ");
}
As you see on the last line, it returns the same forward, "customerCreated".
<action
path="/createCustomer"
name="createCustomerForm"
input="/form/createCustome
scope="request"
type="actions.CreateCustom
<forward name="customerCreated" path="/form/createOrder.js
</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(ActionMappin
HttpServletRequest request, HttpServletResponse response)
throws Exception {
DynaActionForm createCustomerForm = (DynaActionForm) form;
String fn = (String) createCustomerForm.get("fi
String ln = (String) createCustomerForm.get("la
String ad = (String) createCustomerForm.get("ad
int custId = Math.abs( (int)System.currentTimeMil
domain.Customer currentCustomer = new Customer();
currentCustomer.setFirstNa
currentCustomer.setLastNam
currentCustomer.setAddress
currentCustomer.setId(cust
try {
CommandExecutor.getInstanc
request.setAttribute("cust
} catch (Exception e) {
throw new ServletException(e);
}
return mapping.findForward("custo
}
As you see on the last line, it returns the same forward, "customerCreated".
ASKER
Summerian, you are right,... i added the failure page but now i don't really know what happened?
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.
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")%>
ASKER
Excellent Help,... I appreciate it
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).