A
Anonymous
When I click submit on orderForm.jsp and submit.do redirects me to
confirm.jsp,
I get the output "Thank you null" no matter what I type in the name as.
However, The name and the name attribute is however successfully
printed out in the System output (I can see them in the console) in
OrderAction.java.
Perhaps we get sent to confirm.jsp before OrderAction has finished
setting the attribute "name"? But you can clearly see in the code that
it does this before
returning ActionForward.
Maybe their running in different threads or something? how do I fix
this? code is below...
Here are my files (including sruts-config.xml down the bottom)
orderForm.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<html:form action="/submit.do">
Name:<html:text property="name" />
<html:submit/>
<html:reset/>
</html:form>
confirm.jsp
<BODY>
<P>Thank you <%=request.getAttribute("name")%></P>
</BODY>
OrderForm.java
package net.mshome.orderweb.resources;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
* Form bean for a Struts application.
* Users may access 1 field on this form:
* <ul>
* <li>name - [your comment here]
* </ul>
* @version 1.0
* @author
*/
public class OrderForm extends ActionForm {
private String name = null;
/**
* Get name
* @return String
*/
public String getName() {
return name;
}
/**
* Set name
* @param <code>String</code>
*/
public void setName(String n) {
this.name = n;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
// Reset values are provided as samples only. Change as appropriate.
name = null;
}
public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
// Validate the fields in your form, adding
// adding each error to this.errors as found, e.g.
// if ((field == null) || (field.length() == 0)) {
// errors.add("field", new
org.apache.struts.action.ActionError("error.field.required"));
// }
return errors;
}
}
OrderAction.java
package net.mshome.orderweb.resources;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
* @version 1.0
* @author
*/
public class OrderAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
ActionErrors errors = new ActionErrors();
ActionForward forward = new ActionForward(); // return value
OrderForm orderForm = (OrderForm) form;
try {
// Getting the value from the form
String name = orderForm.getName();
System.out.println("The name is: " + name);
request.setAttribute("name", name);
System.out.println("Name attribute is now:"
+ request.getAttribute("name").toString());
} catch (Exception e) {
// Report the error using the appropriate name and ID.
e.printStackTrace();
System.out.println(e.getMessage());
}
// If a message is required, save the specified key(s)
// into the request for use by the <struts:errors> tag.
if (!errors.isEmpty()) {
saveErrors(request, errors);
}
// Write logic determining how the user should be forwarded.
forward = mapping.findForward("success");
// Finish with
System.out.println("Forwarding");
return (forward);
}
}
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD
Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<!-- Data Sources -->
<data-sources>
</data-sources>
<!-- Form Beans -->
<!-- Global Exceptions -->
<form-beans>
<form-bean name="orderForm"
type="net.mshome.orderweb.resources.OrderForm">
</form-bean>
</form-beans>
<global-exceptions>
</global-exceptions>
<!-- Global Forwards -->
<global-forwards>
</global-forwards>
<!-- Action Mappings -->
<action-mappings>
<action path="/submit"
type="net.mshome.orderweb.resources.OrderAction" name="orderForm"
scope="session" validate="false">
<forward name="success" path="confirm.jsp" redirect="true"
contextRelative="false">
</forward>
</action>
</action-mappings>
<!-- Message Resources -->
<message-resources
parameter="net.mshome.orderweb.resources.ApplicationResources"/>
</struts-config>
confirm.jsp,
I get the output "Thank you null" no matter what I type in the name as.
However, The name and the name attribute is however successfully
printed out in the System output (I can see them in the console) in
OrderAction.java.
Perhaps we get sent to confirm.jsp before OrderAction has finished
setting the attribute "name"? But you can clearly see in the code that
it does this before
returning ActionForward.
Maybe their running in different threads or something? how do I fix
this? code is below...
Here are my files (including sruts-config.xml down the bottom)
orderForm.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<html:form action="/submit.do">
Name:<html:text property="name" />
<html:submit/>
<html:reset/>
</html:form>
confirm.jsp
<BODY>
<P>Thank you <%=request.getAttribute("name")%></P>
</BODY>
OrderForm.java
package net.mshome.orderweb.resources;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
* Form bean for a Struts application.
* Users may access 1 field on this form:
* <ul>
* <li>name - [your comment here]
* </ul>
* @version 1.0
* @author
*/
public class OrderForm extends ActionForm {
private String name = null;
/**
* Get name
* @return String
*/
public String getName() {
return name;
}
/**
* Set name
* @param <code>String</code>
*/
public void setName(String n) {
this.name = n;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
// Reset values are provided as samples only. Change as appropriate.
name = null;
}
public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
// Validate the fields in your form, adding
// adding each error to this.errors as found, e.g.
// if ((field == null) || (field.length() == 0)) {
// errors.add("field", new
org.apache.struts.action.ActionError("error.field.required"));
// }
return errors;
}
}
OrderAction.java
package net.mshome.orderweb.resources;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
* @version 1.0
* @author
*/
public class OrderAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
ActionErrors errors = new ActionErrors();
ActionForward forward = new ActionForward(); // return value
OrderForm orderForm = (OrderForm) form;
try {
// Getting the value from the form
String name = orderForm.getName();
System.out.println("The name is: " + name);
request.setAttribute("name", name);
System.out.println("Name attribute is now:"
+ request.getAttribute("name").toString());
} catch (Exception e) {
// Report the error using the appropriate name and ID.
e.printStackTrace();
System.out.println(e.getMessage());
}
// If a message is required, save the specified key(s)
// into the request for use by the <struts:errors> tag.
if (!errors.isEmpty()) {
saveErrors(request, errors);
}
// Write logic determining how the user should be forwarded.
forward = mapping.findForward("success");
// Finish with
System.out.println("Forwarding");
return (forward);
}
}
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD
Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<!-- Data Sources -->
<data-sources>
</data-sources>
<!-- Form Beans -->
<!-- Global Exceptions -->
<form-beans>
<form-bean name="orderForm"
type="net.mshome.orderweb.resources.OrderForm">
</form-bean>
</form-beans>
<global-exceptions>
</global-exceptions>
<!-- Global Forwards -->
<global-forwards>
</global-forwards>
<!-- Action Mappings -->
<action-mappings>
<action path="/submit"
type="net.mshome.orderweb.resources.OrderAction" name="orderForm"
scope="session" validate="false">
<forward name="success" path="confirm.jsp" redirect="true"
contextRelative="false">
</forward>
</action>
</action-mappings>
<!-- Message Resources -->
<message-resources
parameter="net.mshome.orderweb.resources.ApplicationResources"/>
</struts-config>