I created a simple struts 2 application that first displays customer information retrieved from database and displayed on a JSP with all fields editable. Then user can edit the fields and click "save" to save it to database. I The code snippett are below and I will explain my problem at the end.
public class MyAction implements ParameterAware,
Preparable {
private List<CustomerInfo> customers;
public getCustomers() {...}
public setCustomer(..) {...}
public String show() {
// get data from database and save it to "cusotmers"
return SUCCESS;
}
public String save() {
int size = customers.size();
// save new "customers" (modified by user) to database
return SUCCESS;
}
}
class CustomerInfo {
private String name;
....
public String getName() {..}
public setName(..) {..}
}
---------------------------------
tiles.xml:
My problem is --- I can see results when I hit http://../myapp/show. The results are good. I cna also edit the fields on the page. However, when I clikc "save" button, using debugger I found that the "customer" is NULL in the "save" method. That means "customers" result is not passed from JSP back to the Action. Note: I am using the same action class for "show" and "save", they just call two different methods.
In order for S2 to think there's anything relating to "customers" there'd need to be form fields with a leading "customers" in the name, like: <s:textfield name="customers.name" or whatever. Otherwise there's zero relationship between your form fields and the collection.>
steve Pentas
Greenhorn
Joined: Mar 05, 2009
Posts: 14
posted
0
David Newton wrote:In order for S2 to think there's anything relating to "customers" there'd need to be form fields with a leading "customers" in the name, like: <s:textfield name="customers.name" or whatever. Otherwise there's zero relationship between your form fields and the collection.>
I am sorry ! I rechecked my jsp code. actually it has the 'name=' field there
I put name="%{address.county}" in front of the value="$%{address.county}"
But I really didn't get "customers" result in the save() method. it seems "show" method does pass things to the JSP. But JSP does not pass back. Could it be because when I click "save" button, it creates a new instance of "MyAction" class and the new instance does not contain anything (var) that has been set ? I am confused.
My original statement still stands: without any reference to "customers" there's no connection between your form data and the "customers" collection.
If you have a text input named "address.county" that's the form parameter that will be sent to the server. So S2 will look for an "address" setter. See? Nothing to do with "customers". If you have a text input named "customers.address.county" *then* there's a relationship to the customers collection. S2 can't magically guess that you're referring to a collection of customers.
steve Pentas
Greenhorn
Joined: Mar 05, 2009
Posts: 14
posted
0
David, thanks. Unfortunately I tried what you suggested but it did not work.
I found that if I replace the second <s:iterator value="%{address}">
by <s:iterator value="%{customerInfo.address}"> it does not even display any value. Changing it to <s:iterator value="%{address}"> worked.
However, using ><s:textfield name="%{customerInfo.address.county}" value="%{county}"> turns out that I did not get anything from that name.
Note: I pass a <List> of "CustomerInfo". Each "CustomerInfo" contains a name and a <List> of address. Can't figure out where I did wrong.... ??
*Please* use code tags: see the difference?You are mixing up OGNL v. strings. If you have a "name" attribute as "%{customerInfo.address.county}" (an OGNL expression) it's going to attempt to call getCustomerInfo().getAddress().getCounty() and use the results as the name of the field. The "name" attribute is almost always a simple string: "customerInfo.address.county". Stuff like this is trivial to debug by viewing the source of the generated page and on the submission by using a Firebug-like tool to examine the request being sent to the browser.
On a stylistic note, collections are generally named in a plural form to avoid confusion. Because of your earlier post where "address" was an Address, and "address" isn't plural, it wasn't immediately clear why you were trying to iterate over it, even though you added a note to that effect. If you're going to change implementations over the course of a thread then you may want to consider posting the most current code to avoid confusion.