我有一个事件对象,里面有一个Map<ObjectA, List<ObjectB>>
, ObjectA
是标签,list<ObjectB>
是表行。使用以下代码,我可以正确显示表格,但是当我将表格提交给 Action
时类, map 是null
在 Activity 中。
JSP 代码:
<s:iterator value="event.planMap" var="map" >
<h4>Plan Type: <s:property value='key' /></h4>
<table id="plan">
<s:iterator value="value" status="stat" var="detail" >
<tr>
<td><input type="text" id="name" name="event.planMap['%{#map.key}'][%{#stat.index}].name" value="<s:property value='name'/>"/></td>
<td><input type="text" id="text" name="event.planMap['%{#map.key}'][%{#stat.index}].text" value="<s:property value='text'/>"/></td>
<td><input type="text" id="contact" name="event.planMap['%{#map.key}'][%{#stat.index}].contact" value="<s:property value='contact'/>"/></td>
</tr>
</s:iterator>
</table>
</s:iterator>
@Andrea & Roman,所以我修改了代码。显示表格是正确的,但我得到了错误,它进入了结果输入。如果我删除 planMap
, 行动成功。所以至少我知道错误是planMap
.修改后的代码为:
Event
定义:
public Event {
private Map<Object_A, Object_B> planMap;
public Map<Object_A, Object_B> getPlanMap {
return this.planMap;
}
public void setPlanMap(Map<Object_A, Object_B> planMap) {
this.planMap = planMap;
}
}
Object_B
定义:
public Object_B {
private List<Object_C> details;
public List<Object_C> getDetials() {
return this.details;
}
public void setDetails(List<Object_C> details) {
this.details = details;
}
}
JSP代码是:
<s:iterator value="event.planMap" status="mStat" >
<h4>Plan Type: <s:property value='key' /></h4>
<table id="plan">
<s:iterator value="value.details" status="stat">
<tr>
<td><input type="text" id="name" name="event.planMap['% {#mStat.index}'].details[%{#stat.index}].name" value="<s:property value='name'/>"/></td>
<td><input type="text" id="text" name="event.planMap['%{#mStat.index}'].details[%{#stat.index}].text" value="<s:property value='text'/>"/></td>
<td><input type="text" id="contact" name="event.planMap['%{#mStat.index}'].details[%{#stat.index}].contact" value="<s:property value='contact'/>"/></td>
</tr>
</s:iterator>
</table>
</s:iterator>
最佳答案
Struts 可以访问 Action bean 的索引属性,例如 List
, Map
等,但要求元素类应该是一个 Java Bean,如 Map<ObjectA, ObjectC>
, 其中ObjectC
包裹 List<ObjectB>
.
然后使用Struts type conversion像这个答案中那样填充索引属性 How to get updated list values from JSP in my action .
Indexing a collection by a property of that collection
It is also possible to obtain a unique element of a collection by passing the value of a given property of that element. By default, the property of the element of the collection is determined in
Class-conversion.properties
usingKeyProperty_xxx=yyy
, wherexxx
is the property of the bean Class that returns the collection andyyy
is the property of the collection element that we want to index on.For an example, see the following two classes:
MyAction.java
/** * @return a Collection of Foo objects */ public Collection getFooCollection() { return foo; }
Foo.java
/** * @return a unique identifier */ public Long getId() { return id; }
关于java - 如何将 Map<Object, List<Object>> 传递给 Struts 2 中的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23250299/