Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Struts where are values stored between jsp pages?

801424Sep 1 2010 — edited Sep 2 2010
Hi.I realize that this is forum is not specifically for struts but I thought someone might know the answer to my question.
I have run the following code. The code contains two JSP’s that both get there values from the same ArrayList generated by the same actionform so contain the same data. Example.jsp is diplayed when the application is first run when submit is clicked newjsp.jsp is displayed.
If a value in the fields of example.jsp is changed and submit is clicked the altered value is also displayed in newjsp.jsp. I am confused by this as after submit is clicked ExampleForm.java method reset is called which returns the variable values to their original value but the newly entered value is still displayed. Please tell me how this works
Example.jsp

<%@ page language="java"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-nested" prefix="nested"%>

<html>
    <head>
        <title>JSP for exampleForm form</title>
    </head>
    <body>
        <html:form action="/example">

            <h3>Use of nested:iteration tag</h3>

            <nested:nest property="department">
	DEP. ID: <nested:text property="id"/> <br />
	NAME: <nested:text property="name"/> <br /><br />
                <nested:iterate property="customers">
                    <b>Customer info</b><br />
		CUST. ID: <nested:text property="id"/> <br />
		NAME: <nested:text property="name"/> <br />
                </nested:iterate>
            </nested:nest>

            <br />
            <br />
            <html:submit/><html:cancel/>
        </html:form>
    </body>
</html>

Newjsp.jsp

<%@ page language="java"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-nested" prefix="nested"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>

<html>
    <head>
        <title>JSP for exampleForm form</title>
    </head>
    <body>
        <html:form action="/example">

            <h3>Use of nested:iteration tag</h3>

            <nested:nest property="department">

                <table>
                    <nested:iterate property="customers">
                        <tr><td>Customer info</td>
                            <td>CUST. ID: <nested:text property="id"/> </td>
                            <td>NAME: <nested:text property="name"/> </td>
                        </nested:iterate>
                    </tr>
                </table>
            </nested:nest>


            <br />
            <br />
            <html:submit/><html:cancel/>
        </html:form>
    </body>
</html>

ExampleAction.jsp

package de.laliluna.tutorial.nested.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import de.laliluna.tutorial.nested.form.ExampleForm;
import de.laliluna.tutorial.nested.object.*;
import java.util.*;

public class ExampleAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

  
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ExampleForm formBean = (ExampleForm) form;
        Department depart = formBean.getDepartment();
        ArrayList dep = (ArrayList) depart.getCustomers();
        Customer cust0 = (Customer) dep.get(0);
        System.out.println(">>>>>>>>>>>>>> customer.getName() = " + cust0.getName());
        System.out.println(">>>>>>>>>>>>>> customer.getId() = " + cust0.getId());
        Customer cust1 = (Customer) dep.get(1);
        System.out.println(">>>>>>>>>>>>>> customer.getName() = " + cust1.getName());
        System.out.println(">>>>>>>>>>>>>> customer.getId() = " + cust1.getId());
        Customer cust2 = (Customer) dep.get(2);
        System.out.println(">>>>>>>>>>>>>> customer.getName() = " + cust2.getName());
        System.out.println(">>>>>>>>>>>>>> customer.getId() = " + cust2.getId());


        return mapping.findForward("example");
    }
}

ExampleForm.jsp

package de.laliluna.tutorial.nested.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import de.laliluna.tutorial.nested.object.*;
import java.util.ArrayList;
import java.util.Collection;


public class ExampleForm extends org.apache.struts.action.ActionForm {

    Department department;

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

   
    public void reset(ActionMapping mapping,
            HttpServletRequest request) {
        System.out.println("Entered action bean reset");
     
        Collection customers = new ArrayList();
        customers.add(new Customer(1, "Maria"));
        customers.add(new Customer(2, "Klaus"));
        customers.add(new Customer(3, "Peter"));

        department = new Department(1, "Department A", customers);

    }
}

Customer.java

package de.laliluna.tutorial.nested.object;


public class Customer {

    private int id;
    private String name;

    
    public Customer() {
    }

    public Customer(int id, String name) {
        this.id = id;
        this.name = name;

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


Department.java

package de.laliluna.tutorial.nested.object;

import java.util.Collection;

public class Department {

    private int id;
    private String name;
 
    private Collection customers;

  
    public Department() {
    }

    public Department(int id, String name, Collection customers) {
        this.id = id;
        this.name = name;
        this.customers = customers;
    }

    public Collection getCustomers() {
        return customers;
    }

    public void setCustomers(Collection customers) {
        this.customers = customers;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 30 2010
Added on Sep 1 2010
2 comments
433 views