Struts2将对象内部的地图绑定到动作属性 [英] Struts2 binding a map inside an object to an action attribute

查看:121
本文介绍了Struts2将对象内部的地图绑定到动作属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第1部分: 有一个对象(ObjectA),它具有另一个对象(ObjectB).对象B内有一个哈希表.此哈希表有一个字符串作为键,另一个对象"ObjectC"作为值.该哈希图已使用s:iterator和s:textfield显示在jsp上,并且已正确显示.即s:textfield中的值"正确,但名称"却不正确. 现在,当修改文本字段时出现问题.我们如何在动作类的ObjectC中捕获修改后的值?

Part 1: There is an object (ObjectA) which has another object (ObjectB). There is a Hashmap inside the Object B. This hashmap has a string as key and another object "ObjectC" as value. This hashmap has been displayed on the jsp using the s:iterator and s:textfield and it is being displayed correctly. i.e. the "values" inside the s:textfield are correct but the "name" are not. Now, the problem arises when the textfield is modified. How do we capture the modified values inside ObjectC in the action class?

public class ObjectA implements Serializable {
    private Integer attr1;
    private List<ObjectB> objB;
    //...getters and setters....

public class ObjectB implements Serializable {
private Integer attr11;
private Map<String,ObjectC> myMap;
    // ...getters and setters....

public class ObjectC implements Serializable {
private Integer attr111;
public String attr112;
    // ...getters and setters....


jsp代码:

<s:iterator value="#objB.myMap" var="fieldMap" status="fieldStatus">

<li><label><s:property value="#fieldMap.key"/></label><span>
<s:textfield name="<NOT SURE>" value="%{#fieldMap.value.attr12}"  /></span></li>



</s:iterator> 

推荐答案

在您的情况下, objB 是一个List,它包含一个HashMap,然后对于其中的每个元素一个HashMap List.

In your case, objB is a List contanining an HashMap, then one HashMap for every element of the List.

objA
  |--- objB[0]
    |-- objC[A]
    |-- objC[B]
    |-- objC[C]
  |--- objB[1]
    |-- objC[X]
    |-- objC[Y]
    |-- objC[Z]
  |--- objB[n]
    |-- objC[N1]
    |-- objC[N2]
    |-- objC[N3]

然后,您需要两个迭代器和以下OGNL表示法来引用具有name属性的单个元素:

Then you need two iterators and the following OGNL notation, to refer to the single elements with the name attribute:

<s:iterator value="objA.objB" var="listRow" status="listStatus">
<!-- Iterating the List -->
    <s:iterator value="#listRow.myMap" var="mapRow" >
    <!-- Iterating the Map -->
        <li>
            <label>
                <s:property value="#mapRow.key"/>
            </label>
            <span>
                <s:textfield value="%{#mapRow.value.attr12}"  
                  name="objA.objB[#listStatus.index].myMap[#mapRow.key].attr112"
                />
            </span>
        </li>
    </s:iterator> 
</s:iterator> 

这篇关于Struts2将对象内部的地图绑定到动作属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆