aspose file tools*
The moose likes Struts and the fly likes data lost from JSP back to same Action (struts 2) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "data lost from JSP back to same Action (struts 2)" Watch "data lost from JSP back to same Action (struts 2)" New topic
Author

data lost from JSP back to same Action (struts 2)

steve Pentas
Greenhorn

Joined: Mar 05, 2009
Posts: 14
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.

struts.xml
-------
<action name="show" class="MyAction" method='showResult">
<result type="tiles" name="success">display</result>
..
</action>

<action name="save" class="MyAction" method='saveResult">
<result type="chain" name="success">display</result>
..
</action>
---------

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:

<definition name="display" template="..">
<put name="body" value="/../display.jsp"/>
..
</definition>
-----------------------------
display.jsp:

<s:form action="save" method="post"..>
..
<s:iterator value="%{customers}" >
..
<tr>
<td> <s:label name=".." value="$%{name}"..>
</td>
</tr>
</s:iterator>
<s:submit....
</s:submit>
</s:form>
*****************************

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.

Why isn't the "customers" var passed from JSP ?

Thanks.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You left out the part of the JSP that might help answer that question, namely, any of the form input fields.
steve Pentas
Greenhorn

Joined: Mar 05, 2009
Posts: 14
David Newton wrote:You left out the part of the JSP that might help answer that question, namely, any of the form input fields.


Hi, Let me put more details here --

-----------
public class CustomerInfo {
private String name;
private Address address;
// bunch of getter and setter for 'name" and "address"
}

public class Address {
private String county;
private String state;
..
// bunch of getter/setter
}
--------
display.jsp:

<s:form action="save" method="post"..>
..
<s:iterator value="%{customers}" >
..
<tr>
<td><s:label name="%{name}" value="$%{name}"/></td>
<td><s:label name="%{address.state}" value="$="%{address.state}" /></td>
<td><s:label name="%{address.state}" value="$="%{address.county}" /></td>
..
</tr>

</s:iterator>
<s:submit....
></s:submit>
</s:form>

----

Basically that is all I have. It seems I missed something important and caused problem ? please tell me !!
steve Pentas
Greenhorn

Joined: Mar 05, 2009
Posts: 14
Oh, By the way, I used struts 2 so I didn't define forms for this.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Well, you have no form elements that deal with customers, so no customers-related data would go to the server.
steve Pentas
Greenhorn

Joined: Mar 05, 2009
Posts: 14
David Newton wrote:Well, you have no form elements that deal with customers, so no customers-related data would go to the server.


First, the <td> fields looked like

<td> <s:textfield...>

Sorry I gave the wrong information by putting <s:label> instead of <s:textfield>.

David, I didn't exactly understand what you wrote. Could you give more details ? Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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
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.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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
David, thanks. Unfortunately I tried what you suggested but it did not work.

In my actual JSP, it looks like

<s:iterator value="%{customerInfo}">
<tr> table header row...
</tr>

<s:iterator value="%{address}">
<tr>
<td><s:textfield name="%{customerInfo.address.county}" value="%{county}">
</td>
...
</s:iterator>

</s:iterator>

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.... ??

Thanks.




David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

*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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: data lost from JSP back to same Action (struts 2)