如何将动态表单字段绑定到Play中的HashSet!用Java?

时间:2015-10-11 20:50:23

标签: java forms spring-mvc data-binding playframework

我是Play的新手!我正在为一个简单的动态表单编写代码。我有两个模型类:Extension和ExtensionPair。扩展可以包含许多ExtensionPairs,因此我希望能够添加尽可能多的ExtensionPairs并提交表单并在提交后将其绑定到Extension Java对象。

当我使用ArrayList作为ExtensionPairs时,我的代码工作正常(参见下面的代码)。但是,如果我尝试使用HashSet,我会得到:

[InvalidPropertyException: Invalid property 'objectExtensionPairs[0]' of bean class
[models.Extension]: Illegal attempt to get property 'objectExtensionPairs' threw
exception; nested exception is org.springframework.beans.InvalidPropertyException:
Invalid property 'objectExtensionPairs[0]' of bean class [models.Extension]: Cannot
get element with index 0 from Set of size 0, accessed using
property path 'objectExtensionPairs[0]']

我在StackOverflow中寻找一些问题,发现this one几乎说我不能用Spring Framework绑定HashSet。

是否有任何替代方法可以使用HashSet和表单绑定Play!和Java?

请参阅下面的代码:

Extension.java:

package models;

import java.util.ArrayList;

public class Extension {
    public Extension() {
        objectExtensionPairs = new ArrayList<ExtensionPair>();
    }

    public Extension(String objectExtensionOrganization, String objectExtensionDescription) {
        this();
        this.objectExtensionOrganization = objectExtensionOrganization;
        this.objectExtensionDescription = objectExtensionDescription;
    }

    // Getters and Setters

    private ArrayList<ExtensionPair> objectExtensionPairs;
    private String objectExtensionOrganization;
    private String objectExtensionDescription;
}

ExtensionPair.java:

package models;

public class ExtensionPair {
    public ExtensionPair() {
    }

    public ExtensionPair(String objectExtensionKey, String objectExtensionValue) {
        this.objectExtensionKey = objectExtensionKey;
        this.objectExtensionValue = objectExtensionValue;
    }

    // Getters and Setters

    private String objectExtensionKey;
    private String objectExtensionValue;
}

我已编码Play!两者的Java模板,以便我可以使用带有&#34;添加&#34;的动态表单创建或编辑扩展。和&#34;删除&#34;按钮可以拥有尽可能多的ExtensionPairs。

extensionPairTemplate.scala.html:

@(extP: ExtensionPair, index: Int = 0)

@extensionPair = @{ if(extP == null) new ExtensionPair("","") else extP } 

<script type="text/javascript">
// Some JavaScript code to make the form dynamic (add/remove pairs)
</script>

<div id="epDiv@index.toString()">

objectExtensionKey:
<input type="text" name="objectExtensionPairs[@index.toString()].objectExtensionKey" value="@extensionPair.getObjectExtensionKey()"/>

objectExtensionValue:
<input type="text" name="objectExtensionPairs[@index.toString()].objectExtensionValue" value="@extensionPair.getObjectExtensionValue()" />

<button type="button" id="removeExtensionPairButton@index.toString()" onclick="removeExtensionPairFormElements(@index.toString())">Remove</button>

</div>

extensionTemplate.scala.html:

@(extensionForm: Form[models.Extension], ext: Extension)
@extension = @{ if(ext == null) new Extension("","") else ext } 

<script src="@routes.Assets.versioned("javascripts/jquery-1.11.3.min.js")" type="text/javascript"></script>
<script type="text/javascript">
// Some JavaScript code to make the form dynamic (add/remove pairs)
</script>

@helper.form(action = routes.Application.addExtension) {
    <div id="extensionDiv">
    <fieldSet>
        @helper.inputText(extensionForm("objectExtensionOrganization").copy(value=Some(extension.getObjectExtensionOrganization())))
        @helper.inputText(extensionForm("objectExtensionDescription").copy(value=Some(extension.getObjectExtensionDescription())))
        <div id="extensionPairsDiv">
            @if(extension.getObjectExtensionPairs().size() > 0) {
                @for((ep,epIndex) <- extension.getObjectExtensionPairs().zipWithIndex) {
                    @extensionPairTemplate(ep, epIndex)
                }
            }
        </div>
    </fieldSet> 
    <br/>
    <button type="button" id="addPair">Add extension pair</button>
    <input type="submit" value="Submit"/>
    </div>
}

我的控制器:

// For when the form is submitted (i.e. editing an Extension)
public Result addExtension() {
  Form<Extension> formExtension = Form.form(Extension.class).bindFromRequest();
  Extension e = formExtension.get();
  return ok(extensionTemplate.render(Form.form(Extension.class), e));
}

// For when a new Extension is being created
public Result extensionForm() {
    return ok(extensionTemplate.render(Form.form(Extension.class), null));
}

1 个答案:

答案 0 :(得分:1)

您始终可以手动绑定表单:

Map<String, String[]> formMap = request().body().asFormUrlEncoded();
String myContent = formMap.get("myFormElementName")[0]);