Java Tapestry使用AJAX从可编辑网格更新DAO

Java Tapestry使用AJAX从可编辑网格更新DAO,java,ajax,dynamic,datagrid,tapestry,Java,Ajax,Dynamic,Datagrid,Tapestry,我试图在Tapestry中完成以下工作 我有一本包含我的数据的字典 我试图实现我有一个下拉菜单选择组件,其中包含来自外部字典的键 当选择更改时,应使用所选子字典中的键和值更新网格 例如: Dictionary<String, Dictionary<String, Object>> dictionaries = new Hashtable<String, Dictionary<String, Object>>(); Dictionary<St

我试图在Tapestry中完成以下工作

我有一本包含我的数据的字典

我试图实现我有一个下拉菜单选择组件,其中包含来自外部字典的键

当选择更改时,应使用所选子字典中的键和值更新网格

例如:

Dictionary<String, Dictionary<String, Object>> dictionaries = new Hashtable<String, Dictionary<String, Object>>();

Dictionary<String, Object> dict1 = new Hashtable<String, Object>();
Dictionary<String, Object> dict2 = new Hashtable<String, Object>();

dict1.put("k1", "d1v1");
dict1.put("k2", "d1v2");

dict2.put("k1", "d2v1");
dict2.put("k2", "d2v2");

dictionaries.put("d1", dict1);
dictionaries.put("d2", dict2);
configValue是当前存在的条目的值,而不是我试图将其更改为的条目的值

有没有办法得到这个值?我的网格具有任意数量的行

编辑2:提供了更多信息

这是我的TML:

<t:form t:id="configSelectForm">
    <t:select t:id="storageKeySelecter"
          t:model="storageKeyModel"
          t:value="storageKey"
          zone="configZone" />
</t:form>

<br/>

<t:zone t:id="configZone" id="configZone" elementName="div">
    <form t:type="form" t:id="configReviewForm" id="configReviewForm" t:zone="configZone" zone="configZone">
        <table width="100%">
            <t:grid t:source="configurationEntries"
                    t:add="update, delete"
                    t:row="configurationEntry"
                    t:mixins="DisableGridSorting"
                    t:include="configKey, configValue"
                    t:encoder="configurationEntryEncoder" >

                <p:configValueCell>
                    <input t:id="configValueTextField" t:type="TextField" t:value="configurationEntry.configValue"
                           t:validate="required" />
                </p:configValueCell>

                <p:updateCell>
                    <t:actionlink id="update" t:id="update" zone="configZone" t:context="[configurationEntry.configKey, configurationEntry.configValue]">Update</t:actionlink>
                </p:updateCell>

                <p:deleteCell>
                    <t:actionlink id="delete" t:id="delete" zone="configZone" t:context="configurationEntry.configKey">Delete</t:actionlink>
                </p:deleteCell>

            </t:grid>
        </table>
        <br/>
        <input type="submit" value="Update" class="button" zone="configZone"/>
    </form>
</t:zone>

<br/>
<br/>

<t:form t:id="addNewEntryForm">
    <table width="100%">
        <tr>
            <td>
                <input t:id="newEntryKey" t:type="textfield" t:value="newEntry.configKey" 
                       t:validate="required" t:context="newEntry.configKey" />
            </td>

            <td>
                <input t:id="newEntryValue" t:type="textfield" t:value="newEntry.configValue" 
                       t:validate="required" t:context="newEntry.configValue" />
            </td>

            <td>
                <input type="submit" value="Add New Entry" zone="configZone"/>
            </td>
        </tr>
    </table>
</t:form>
我想把key2的值改为newValue2传递给方法的参数是key2和value2,而不是key2和newValue2

同样的问题也是批量更新不起作用的原因。configurationEntries中的所有值都是数据库中的shapshot值,而不是当前写入我的网格中的值

所以,我的问题是如何使用AJAX从可编辑网格更新数据库,这是我的具体示例。我已经尝试了在SO和internet的其他部分上建议的许多方法,但它们似乎不起作用,我也不知道为什么。

您需要使用zone参数

查看SelectZoneDemo.tml并从中选择ZoneDemo.java。它给出了一个在选择更改时更新分区的示例

对于更复杂的交互,您可能对

感兴趣,您需要使用zone参数

查看SelectZoneDemo.tml并从中选择ZoneDemo.java。它给出了一个在选择更改时更新分区的示例


对于更复杂的交互,您可能有兴趣从需要提交表单的输入中获取实际值。当您使用ActionLink时,将使用其上下文中的值。这些值在链接呈现期间保存在上下文中,如果不进行黑客攻击,则无法在客户端进行更改

要使示例正常工作,可以对每行使用单独的表单。您还需要一些可以在表单之外工作的提交组件:

<t:zone t:id="configZone" id="configZone">
  <table width="100%">
    <t:grid t:source="configurationEntrySource"
            t:add="update, delete"
            t:row="configurationEntry"
            t:mixins="DisableGridSorting"
            t:include="configKey, configValue"
            t:encoder="configurationEntryEncoder">
      <p:configValueCell>
        <t:form t:id="configReviewForm" zone="configZone">
          <input t:id="value" t:type="TextField" t:value="configurationEntry.configValue"
                 t:validate="required" t:context="configurationEntry.configValue" />
        </t:form>
      </p:configValueCell>

      <p:updateCell>
        <t:customlinksubmit form="configReviewForm">Update</t:customlinksubmit>
      </p:updateCell>

      <p:deleteCell>
        <t:actionlink t:id="delete" zone="configZone" context="configurationEntry.configKey">Delete</t:actionlink>
      </p:deleteCell>

    </t:grid>
  </table>
  <br/>
</t:zone>

要获得输入的实际值,您需要提交表单。当您使用ActionLink时,将使用其上下文中的值。这些值在链接呈现期间保存在上下文中,如果不进行黑客攻击,则无法在客户端进行更改

要使示例正常工作,可以对每行使用单独的表单。您还需要一些可以在表单之外工作的提交组件:

<t:zone t:id="configZone" id="configZone">
  <table width="100%">
    <t:grid t:source="configurationEntrySource"
            t:add="update, delete"
            t:row="configurationEntry"
            t:mixins="DisableGridSorting"
            t:include="configKey, configValue"
            t:encoder="configurationEntryEncoder">
      <p:configValueCell>
        <t:form t:id="configReviewForm" zone="configZone">
          <input t:id="value" t:type="TextField" t:value="configurationEntry.configValue"
                 t:validate="required" t:context="configurationEntry.configValue" />
        </t:form>
      </p:configValueCell>

      <p:updateCell>
        <t:customlinksubmit form="configReviewForm">Update</t:customlinksubmit>
      </p:updateCell>

      <p:deleteCell>
        <t:actionlink t:id="delete" zone="configZone" context="configurationEntry.configKey">Delete</t:actionlink>
      </p:deleteCell>

    </t:grid>
  </table>
  <br/>
</t:zone>

我知道我需要使用zone参数。让我困惑的是如何连接两个不同的组件,Select和Grid,这样当Select更改时,Grid也会更改。尽管如此,第二个链接仍需向上投票:如果将网格放置在区域中,则网格将在区域刷新时更新。网格的数据源可能会受到select值的影响。您是否有代码片段或引用我可以搜索这些内容?有一些例子可以帮助我弄清楚东西吗?例子:我知道我需要使用zone参数。让我困惑的是如何连接两个不同的组件,Select和Grid,这样当Select更改时,Grid也会更改。尽管如此,第二个链接仍需向上投票:如果将网格放置在区域中,则网格将在区域刷新时更新。网格的数据源可能会受到select值的影响。您是否有代码片段或引用我可以搜索这些内容?有一些例子可以帮助我弄明白事情的来龙去脉吗?例如:不确定为什么你要在这里添加一个赏金,因为@nathanq提供了一个到jumpstart中一个运行演示的链接,其中包含代码?不确定为什么你要在这里添加赏金,因为@nathanq提供了到jumpstart中一个运行演示的链接,其中包含代码?
<t:form t:id="configSelectForm">
    <t:select t:id="storageKeySelecter"
          t:model="storageKeyModel"
          t:value="storageKey"
          zone="configZone" />
</t:form>

<br/>

<t:zone t:id="configZone" id="configZone" elementName="div">
    <form t:type="form" t:id="configReviewForm" id="configReviewForm" t:zone="configZone" zone="configZone">
        <table width="100%">
            <t:grid t:source="configurationEntries"
                    t:add="update, delete"
                    t:row="configurationEntry"
                    t:mixins="DisableGridSorting"
                    t:include="configKey, configValue"
                    t:encoder="configurationEntryEncoder" >

                <p:configValueCell>
                    <input t:id="configValueTextField" t:type="TextField" t:value="configurationEntry.configValue"
                           t:validate="required" />
                </p:configValueCell>

                <p:updateCell>
                    <t:actionlink id="update" t:id="update" zone="configZone" t:context="[configurationEntry.configKey, configurationEntry.configValue]">Update</t:actionlink>
                </p:updateCell>

                <p:deleteCell>
                    <t:actionlink id="delete" t:id="delete" zone="configZone" t:context="configurationEntry.configKey">Delete</t:actionlink>
                </p:deleteCell>

            </t:grid>
        </table>
        <br/>
        <input type="submit" value="Update" class="button" zone="configZone"/>
    </form>
</t:zone>

<br/>
<br/>

<t:form t:id="addNewEntryForm">
    <table width="100%">
        <tr>
            <td>
                <input t:id="newEntryKey" t:type="textfield" t:value="newEntry.configKey" 
                       t:validate="required" t:context="newEntry.configKey" />
            </td>

            <td>
                <input t:id="newEntryValue" t:type="textfield" t:value="newEntry.configValue" 
                       t:validate="required" t:context="newEntry.configValue" />
            </td>

            <td>
                <input type="submit" value="Add New Entry" zone="configZone"/>
            </td>
        </tr>
    </table>
</t:form>
public class PropertyConfiguration {

    @Inject
    private BeanModelSource beanModelSource;

    @Component
    private Form configReviewForm;

    @Property
    private List<ConfigurationEntry> configurationEntries;

    private List<ConfigurationEntry> persistentEntries;

    @Property
    private ConfigurationEntry configurationEntry = new ConfigurationEntry("", "");

    @Property
    private ConfigurationEntryEncoder configurationEntryEncoder;

    @InjectComponent
    private Zone configZone;

    @InjectComponent
    private TextField configValueTextField;

    @Inject
    private ConfigurationPersitanceDAO dao;

    private GridDataSource dataSource;

    @Inject
    private Messages messages;

    @Property
    private ConfigurationEntry newEntry;

    @Inject
    private Request request;

    @Property
    @Validate("required")
    @Persist(PersistenceConstants.SESSION)
    private String storageKey;

    private StringSelectModel storageKeysSelectModel;

    public ValueEncoder<ConfigurationEntry> getConfigurationEntryEncoder() {
        initConfigurationEntityEncoder();

        return this.configurationEntryEncoder;
    }

    public BeanModel<ConfigurationEntry> getModel() {
        return beanModelSource.createDisplayModel(ConfigurationEntry.class, messages);
    }

    public SelectModel getStorageKeyModel() {
        if (storageKeysSelectModel == null) {
            storageKeysSelectModel = new StringSelectModel(this.dao.getStorageKeys());
        }

        return storageKeysSelectModel;
    }

    private void initConfigurationEntityEncoder() {
        if (this.configurationEntryEncoder == null) {
            this.configurationEntryEncoder = new ConfigurationEntryEncoder(dao, storageKey);
        }
    }

    private void initZoneData() {
        if (this.storageKey == null) {
            this.storageKey = this.dao.getStorageKeys().get(0);
        }
        initConfigurationEntityEncoder();   
    }

    public Object onActionFromDelete(String configKey) {
        System.out.println("Deleting from: " + storageKey + " entry: " + configKey);
        this.dao.deleteConfigurationEntry(storageKey, configKey);

        return request.isXHR() ? configZone.getBody() : null;
    }

    public Object onActionFromUpdate(String configKey, String configValue) {
        this.dao.updateConfigurationEntry(storageKey, configKey, configValue);

        return request.isXHR() ? configZone.getBody() : null;
    }

    void onActivate(String storageKey) {
        initZoneData();
        this.newEntry = new ConfigurationEntry("", "");
    }

    String onPassivate() {
        this.newEntry = new ConfigurationEntry("", "");
        return this.storageKey;
    }

    Object onRefresh() {
        return request.isXHR() ? configZone.getBody() : null;
    }

    Object onSuccessFromAddNewEntryForm() {
        String configKey = this.newEntry.getConfigKey();
        String configValue = this.newEntry.getConfigValue();

        this.dao.addConfigurationEntry(storageKey, configKey, configValue);

        return request.isXHR() ? configZone.getBody() : null;
    }

    void onValidateFromAddNewEntryForm() {
        return;
    }

    Object onValueChangedFromStorageKeySelecter(String storageKey) {
        this.storageKey = storageKey;
        initConfigurationEntityEncoder();
        this.configurationEntries = wrap(this.dao.getConfiguration(storageKey));
        return configZone.getBody();
    }

    private void updateChangedConfigurations(List<ConfigurationEntry> changedEntries) {
        for (ConfigurationEntry changedEntry : changedEntries) {
            String configKey = changedEntry.getConfigKey();
            String configValue = changedEntry.getConfigValue();

            System.out.println("Updated: [" + storageKey + ":" + configKey + ":" + configValue + "]");

            this.dao.updateConfigurationEntry(storageKey, configKey, configValue);
        }
    }

    void onValidateFromConfigReviewForm() {
        this.persistentEntries = wrap(this.dao.getConfiguration(storageKey));
        List<ConfigurationEntry> tmpList = new ArrayList<ConfigurationEntry>();

        for (ConfigurationEntry newEntry : this.configurationEntries) {
            for (ConfigurationEntry oldEntry : this.persistentEntries) {
                System.out.println("NewEntry: " + newEntry.toString() + " vs. OldEntry: " + oldEntry.toString());
                if (oldEntry.getConfigKey().equals(newEntry.getConfigKey())) {
                    if (!oldEntry.getConfigValue().equals(newEntry.getConfigValue())) {
                        newEntry.setConfigValue(newEntry.getConfigValue().trim());
                        tmpList.add(newEntry);
                    }
                }
            }
        }

        this.persistentEntries = tmpList;
    }

    Object onSuccessFromConfigReviewForm() {
        updateChangedConfigurations(this.persistentEntries);

        return request.isXHR() ? configZone.getBody() : null;
    }

    /**
    *   Wraps dictionary entries in instances of ConfigurationEntry
    */
    private List<ConfigurationEntry> wrap(
        Dictionary<String, Object> rawConfiguration) {
        Set<String> keySet = new TreeSet<String>();
        List<ConfigurationEntry> wrappedEntries = new ArrayList<ConfigurationEntry>();

        Enumeration<String> keys = rawConfiguration.keys();

        while (keys.hasMoreElements()) {
            String key = keys.nextElement();
            keySet.add(key);
        }

        for (String key : keySet) {
            String value = (String) rawConfiguration.get(key);

            ConfigurationEntry entry = new ConfigurationEntry(key, value);

            wrappedEntries.add(entry);
        }

        return wrappedEntries;
    }
}
key1 => value1
key2 => value2
key3 => value3
<t:zone t:id="configZone" id="configZone">
  <table width="100%">
    <t:grid t:source="configurationEntrySource"
            t:add="update, delete"
            t:row="configurationEntry"
            t:mixins="DisableGridSorting"
            t:include="configKey, configValue"
            t:encoder="configurationEntryEncoder">
      <p:configValueCell>
        <t:form t:id="configReviewForm" zone="configZone">
          <input t:id="value" t:type="TextField" t:value="configurationEntry.configValue"
                 t:validate="required" t:context="configurationEntry.configValue" />
        </t:form>
      </p:configValueCell>

      <p:updateCell>
        <t:customlinksubmit form="configReviewForm">Update</t:customlinksubmit>
      </p:updateCell>

      <p:deleteCell>
        <t:actionlink t:id="delete" zone="configZone" context="configurationEntry.configKey">Delete</t:actionlink>
      </p:deleteCell>

    </t:grid>
  </table>
  <br/>
</t:zone>
@SupportsInformalParameters
public class CustomLinkSubmit implements ClientElement {

    @Parameter(required = true, allowNull = false, defaultPrefix = BindingConstants.COMPONENT)
    private Form form;

    @Parameter(allowNull = false, defaultPrefix = BindingConstants.LITERAL)
    private SubmitMode mode = SubmitMode.NORMAL;

    @Inject
    private ComponentResources resources;

    @Inject
    private JavaScriptSupport javascriptSupport;

    private String clientId;

    @BeginRender
    void beginRender(MarkupWriter writer) {
        clientId = javascriptSupport.allocateClientId(resources);
        writer.element("span", "id", clientId);
        resources.renderInformalParameters(writer);
    }

    @AfterRender
    void afterRender(MarkupWriter writer) {
        writer.end();

        JSONObject spec = new JSONObject()
                .put("form", form.getClientId())
                .put("clientId", clientId)
                .put("validate", mode == SubmitMode.NORMAL);

        javascriptSupport.addInitializerCall(InitializationPriority.EARLY, "linkSubmit", spec);
    }

    public String getClientId() {
        return clientId;
    }
}