Struts - Cant pass attribute to page from Action class

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

Sudsy

Anonymous said:
When I click submit on orderForm.jsp and submit.do redirects me to
confirm.jsp,
<snip>

You've already answered your own query with the use of the word "redirect".
If you don't forward then you lose all of the form attributes as you're
requiring the browser to make a second request to the server. Since the
attributes only have request scope...
 
S

Sudsy

Anonymous wrote:
<BODY>
<P>Thank you <%=request.getAttribute("name")%></P>
</BODY>
<snip>

Since orderForm is defined as having session scope, the following might
work:
<%= orderForm.name %>
Don't have time to check it out right now, it's late, etc...
 
M

Murray

Anonymous said:
<forward name="success" path="confirm.jsp" redirect="true"
contextRelative="false">
</forward>

Here is your problem. When you use redirect="true" this issues a client side
redirect (status 302 - Moved Temporarily). Hence this will be a new request
and any request attributes you set in the Action will no longer be available
in the jsp.

Either don't use redirect (preferable) or stick the value in the session to
make it available across multiple requests.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top