Shamwow
Shamwow

Reputation: 51

Adding objects to List with Struts2

I am new to using struts2 framework and have some confusion with it. I have a form that submits to an action class. I want the fields submitted to be saved into a List of Customer list. In the action class I declared a List, defined the getModel method to return a List of Customer, and in the prepare method (I'm not sure if this is right) I added objects to list

JSP

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Add Account </title>
</head>
<body>
   <h2>
            Customer details
   </h2>
<s:form action="AddCustomerAction" name="/example" method="post">
    <h1>
        <s:textfield label="Enter First Name" name="firstName" key="firstName" required="true" size="25"/>
    </h1>
    <h1>
        <s:textfield label="Enter Last Name" name="lastName" required="true" size="25" />
    </h1>

    <h1>
        <s:textfield label="Enter Address" name="address" required="true" size="25" />
    </h1>
    <h1>
       <s:select value ="state" name="state" list="stateList" label="Select State" listKey="code" listValue="desc" required="true"/>
    <h1>
    <h1>
        <s:textfield label="Enter City" name="city" required="true" size="25" />
    </h1>
    <h1>
        <s:textfield label="Enter Zipcode" name="zipcode" required="true" size="25" />
    </h1>

     <h1>
        <s:submit name="OK"/>
     </h1>
     </s:form>
</body>

Action class

package example;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import java.util.ArrayList;
import java.util.List;

public class AddCustomerAction extends ActionSupport implements ModelDriven{

private Customer customer = new Customer();
private List<Customer> customerList;

public List<Customer> getModel()
{
    return customerList;
}

public String execute()
{
    this.printCustomer();
    return SUCCESS;
}

public void prepare(){
    customerList = new ArrayList<Customer>();
    customerList.add(new Customer("first", "last"));
}


private void printCustomer(){
    System.out.println("First Name = "+customer.getFirstName());
    System.out.println("Last Name = "+customer.getLastName());
    System.out.println("Add = "+customer.getAddress());
    System.out.println("State = "+customer.getState());
    System.out.println("City = "+customer.getCity());
    System.out.println("Zipcode = "+customer.getZipcode());    
}

In the action class I want to save the information submitted from the form in the Customer List so I can iterate over it and display the data.

When I run this, firstname and lastName are null (even though i entered values).

is my approach (setting the getModel() and prepare() as I have) correct? what am i missing?

Upvotes: 0

Views: 1866

Answers (2)

Alireza Fattahi
Alireza Fattahi

Reputation: 45485

The problem is about List<Customer>. Your form submits a Customer not a list of customer. So

public Object getModel()
{
    return customerList;
}

If you want to get a list of customers, you must change your jsp and too.

Also to use ModelDriven actions, make sure that the Model Driven Interceptor is applied to your action.

Upvotes: 1

coding_idiot
coding_idiot

Reputation: 13734

<s:textfield label="Enter First Name" name="firstName" key="firstName" required="true" size="25"/>

The name of this field is firstName and hence, it requires a getter/setter for a property/field named firstName

In order to get the customer bean filled up in your action with the parameters from the request, change the name to customer.firstName

and then you'll need a setter for customer field, which you already have.

The only assumption here is that you need to have a field named firstName in your customer class, but looking at customer.getFirstName() I guess you do have a field named firstName in your customer bean.

I tried to explain pretty clearly, on how the parameters get mapped from request to action, please let me know if you didn't understand anything.

Upvotes: 0

Related Questions