Struts - Cant pass attribute to page from Action class

Discussion in 'Java' started by Anonymous, Dec 8, 2004.

  1. Anonymous

    Anonymous Guest

    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>
     
    Anonymous, Dec 8, 2004
    #1
    1. Advertising

  2. Anonymous

    Sudsy Guest

    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.
     
    Sudsy, Dec 8, 2004
    #2
    1. Advertising

  3. Anonymous

    Sudsy Guest

    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.
     
    Sudsy, Dec 8, 2004
    #3
  4. Anonymous

    Murray Guest

    "Anonymous" <> wrote in message
    news:...
    > <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.
     
    Murray, Dec 8, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Bloggs
    Replies:
    1
    Views:
    794
    Sudsy
    Aug 3, 2003
  2. rjweytens
    Replies:
    6
    Views:
    16,104
    rjweytens
    Jun 25, 2004
  3. runescience

    Struts mapping action to action???

    runescience, Feb 6, 2006, in forum: Java
    Replies:
    3
    Views:
    1,831
    runescience
    Feb 7, 2006
  4. Nagaraj
    Replies:
    1
    Views:
    869
    Lionel B
    Mar 1, 2007
  5. vyshu
    Replies:
    1
    Views:
    2,556
    Richard Senior
    Apr 27, 2007
Loading...

Share This Page