We are working with struts 2.0.14 and facing problem setting back indexed properties from JSP to action class.
Code looks similar to this.
public class MyExample extends ActionSupport
{
private List<Person> persons;
private List<String> names;
public String execute()
{
//persons = myDAO.readPersons(names);
return SUCCESS;
}
public String update()
{
for (Person p : persons)
{
System.out.println(p.getName() + "---" + p.getAddress().getStreet());
}
return SUCCESS;
}
// Getters and setters
}
class Person
{
private Address address;
private String name;
// Getters and setters
}
class Address
{
private String street;
private String number;
// Getters and setters
}
And JSP:
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<!---- Other code ---->
<s:ieterate value="persons" status="status">
<tr>
<s:textfield name="person['%{#status.index}'].name"/>
<s:textfield name="person['%{#status.index}'].address.number"/>
<s:textfield name="person['%{#status.index}'].street"/>
</tr>
I could successfully display values onto the page, but when i submit the form the values are not getting populated. I checked firebug to see开发者_运维技巧 how the parameters are posted and they are like person['0'].name, person['0'].address.number etc., Please let me know where the mistake lies.
Did you try without the ' around the %{#status.index}?
Like this:
<s:textfield name="person[%{#status.index}].name"/>
In addition to removing the quotes around the index as Nate suggests, I noticed that your field is named persons
but your parameters use person
instead. Assuming that's a typo and it should be, for instance, persons[%{#status.index}].name
, make sure:
- Your action has a
setPersons(List<Person> person)
so that it can create a new list and set it. Person
andAddress
have no-arg constructors so that they can be automatically created on the fly.
If it's still not working, try adding the @Element
(com.opensymphony.xwork2.util.Element) to persons
to give it a hint about what type of object to fill it with:
@Element(value=Person)
List<Person> persons;
One other thing that might be happening is one of the superclasses of your action may implement ParameterNameAware which filters out parameters by name. A quick way to check is to override acceptableParameterName
in your action and see if it makes it work:
@Override
public boolean acceptableParameterName(String name) {
return true;
}
See the API docs for ParametersInterceptor for more details: http://struts.apache.org/2.0.14/struts2-core/apidocs/com/opensymphony/xwork2/interceptor/ParametersInterceptor.html
精彩评论