struts 2:捕获对番石榴表(TreeBasedTable)的修改 [英] struts 2 : Capturing modifications to a guava table (TreeBasedTable) in action

查看:96
本文介绍了struts 2:捕获对番石榴表(TreeBasedTable)的修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个对象(ObjectA),其中有另一个对象(ObjectB).在对象B内有一个Guava TreeBasedTable.此表具有一个字符串,作为行键,列键,而另一个对象"ObjectC"作为值.该表已使用<s:iterator/><s:textfield/>标记显示在jsp上,并且已正确显示(<s:textfield/>内的值"正确,但名称"不正确).

There is an object (ObjectA) which has another object inside (ObjectB). There is a Guava TreeBasedTable inside the Object B. This Table has a string as row-key,column-key and another object "ObjectC" as value. This table has been displayed on the jsp using the <s:iterator/> and <s:textfield/> tags and it is being displayed correctly (the "values" inside the <s:textfield/> are correct but the "names" are not).

现在,修改<s:textfield/>时出现问题.如何在动作类的ObjectC中捕获修改后的值?

Now, the problem arises when the <s: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 Table<String,String,ObjectC> allPFields;
// ...getters and setters....
public class ObjectC implements Serializable {
private Integer attr111;
public String attr112;
// ...getters and setters....

jsp代码:

<!-- language: lang-html -->

<s:iterator value="#objB.allPlainFields.row(#rowKey)" 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>

Guava中的TreeBasedTable类似于地图中的地图,我尝试执行allPFields[#outerkey][#innerkey].attr112,但是没有用.

A TreeBasedTable in Guava is similar to a map inside a map, I tried doing allPFields[#outerkey][#innerkey].attr112 but, it didn't work.

显示具有数据库中现有值的屏幕时的对象结构

The object structure when the screen is displayed with existing values in the database

<!-- language: lang-java -->
objA        
objBList    ArrayList<E>  (id=668)  
elementData Object[10]  (id=7438)   
    [0] objB  (id=7439) 
        allPFields  TreeBasedTable<R,C,V>  (id=7443)    
            backingMap  TreeMap<K,V>  (id=8116) 
            cellSet null    
            columnComparator    NaturalOrdering  (id=503)   
            columnKeySet    null    
            columnMap   null    
            factory TreeBasedTable$Factory<C,V>  (id=8117)  
            rowKeySet   null    
            rowKeySet   StandardRowSortedTable$RowKeySortedSet  (id=8118)   
            rowMap  StandardRowSortedTable$RowSortedMap  (id=8119)  
            rowMap  null    
            values  null

"allPFields"看起来像下面这样:

And the "allPFields" looks like the following in action:

{OuterKey1=
    {InnerKey1=ObjectC[attr111=31, attr112=Hi there], 
     InnerKey2=ObjectC[attr111=40, attr112=How are you]
    }
 }

上面的"allPFields"值已从IDE控制台中获取.

The "allPFields" value above has been picked up from the IDE console.

推荐答案

正如我在另一个问题中告诉您的那样,我从未使用过Guava TreeBasedTable;

As I told you in your other question, I've never used Guava TreeBasedTable;

但是,根据官方Guava文档

TreeBasedTable,其中is essentially backed by a TreeMap<R, TreeMap<C, V>>

V get(Object rowKey, Object columnKey)

返回与给定的行键和列键对应的值;如果不存在此类映射,则返回null.

Returns the value corresponding to the given row and column keys, or null if no such mapping exists.

Java中应该是:

Object value = objA.getObjB().get(listIndex).getAllPlainFields.get(rowKey, columnKey).getAttr112;
value = "new value";

然后在OGNL中,您可以尝试以下操作(完全未经测试):

Then in OGNL you could try something (totally untested) like:

<s:textfield value="%{#fieldMap.value.attr12}"
 name="objA.objB[#fieldStatus.index].allPlainFields.get(#rowKey, #fieldMap.Key).attr112"  />

PS:当心打字错误 ......您同时使用allPlainFieldsallPFields ...其中之一是错误的,请确保所有符号都指向正确的变量名

P.S: BEWARE OF TYPO... you are using allPlainFields and allPFields together... one of them is wrong, make sure that all the notations point to the right variable name.

P.P.S:我不知道您的要求,但是这种结构对我来说似乎有点过度设计"了……它绝对不尊重KISS paradigm:)

P.P.S: I don't know your requirements, but this structure seems a little "over-designed" to me... it definitely does not respect the KISS paradigm :)

这篇关于struts 2:捕获对番石榴表(TreeBasedTable)的修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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