Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Struts - Cant pass attribute to page from Action class

Reply
Thread Tools

Struts - Cant pass attribute to page from Action class

 
 
Anonymous
Guest
Posts: n/a
 
      12-08-2004
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.Applicati onResources"/>
</struts-config>

 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      12-08-2004
Anonymous wrote:
> 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...

--
Java/J2EE/JSP/Struts/Tiles/C/UNIX consulting and remote development.

 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      12-08-2004
Anonymous wrote:
<snip>
> <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...

--
Java/J2EE/JSP/Struts/Tiles/C/UNIX consulting and remote development.

 
Reply With Quote
 
Murray
Guest
Posts: n/a
 
      12-08-2004

"Anonymous" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> <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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can we call an Action from another Action in struts??? vyshu Java 1 04-27-2007 09:19 AM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM
Struts mapping action to action??? runescience Java 3 02-07-2006 04:07 PM
Struts Forward to an Action from an to Action and URLs rjweytens Java 6 06-25-2004 01:49 PM
Struts Static Action Form vs Dyanamic Action Form Joe Bloggs Java 1 08-03-2003 02:30 AM



Advertisments