[2.3.8 java] Binding multiple child-Objects from a Form

500 views
Skip to first unread message

Quirin Stockinger

unread,
Apr 26, 2015, 4:08:55 AM4/26/15
to play-fr...@googlegroups.com
This is a tag-on question to a question in this forum from 2012.
In https://groups.google.com/forum/#!topic/play-framework/hhmPMZwPDYU user Leonard Punt wrote about binding multiple objects:
So, what I did:
- Created a class Foo.java, which is an entity.
- Created a 'helper' class FooList.java, which has only one variable: List<Foo> foos. This class is not an entity.
- In my Controller I pass a Form<FooList> to my view.
- In my view I've created my 'own field':
@fooFields(form: Form[FooList], index: Integer) = {
 
<div class="foo">

@inputText(form("foos[" + index + "].name"), '_label -> "Foo's name")

@inputText(form("
foos[" + index + "].age"), '_label -> "Foo's age")
</div>

}


- And in the main part of my view:
@form(routes.Foos.createFoos()) {  
   
<div class="foos">

@fooFields(fooListForm, 0)

@fooFields(fooListForm, 1)

@fooFields(fooListForm, 2)
</div>
<input type="submit" value="Submit">
}

I have tried to replicate this approach in my play 2.3.8 Application where I have a @MappedSuperclass Subtask and two child classes SubtaskGroupA and SubtaskGroupB. The basic premise is to display a List of either GroupA or GroupB Subtasks, let the user make choices and retrieve his choices from the form. To achieve this, I also wrote a helper class (not an Entity) called SubtaskList which contains only one field: public List<Subtask> subtaskList. Instead of the @inputText-helper I use the @select to create dropdown menus for the user's choices.
Before rendering, I query all appropriate Subtasks from my DB into a List<Subtask> list, and fill my form with the data
Form<SubtaskList> form = form(SubtaskList.class).fill(list);.
 
Upon pressing the Submit-button I receive the following Exception in my Controller-method:

play - Cannot invoke the action, eventually got an error: org.springframework.beans.InvalidPropertyException: Invalid property 'subtaskList[0]' of bean class [models.qosdatamodel.SubtaskList]: Illegal attempt to get property 'subtaskList' threw exception; nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property 'subtaskList' of bean class [models.qosdatamodel.SubtaskList]: Could not instantiate property type [models.qosdatamodel.Subtask] to auto-grow nested property path: java.lang.InstantiationException

All fields in the Subtask hierarchy are private with appropriate Getters/Setters and have default constructors.
I am grateful for every insight.

Quirin Stockinger

unread,
Apr 26, 2015, 4:58:57 AM4/26/15
to play-fr...@googlegroups.com
I think I have found my problem. To debug I retrieved the key,value-Map from the request body and logged them to the console. The problem seems to be the mapping of the fields in my form to the appropriate variables in my Entities.

<!-- Parameter Name-->
                <td>@subtaskForm.get.subtaskList.get(index).getGivenParameter.name</td>
                <!-- Parameter Value-->
                <td>@subtaskForm.get.subtaskList.get(index).getGivenParameter.value</td>
                <!-- Parameter Type-->
                <td>@select(subtaskForm("models.qosdatamodel.SubtaskList.subtaskList["+index+"].typeChosen"),
                    options= options(models.qosdatamodel.Subtask.paramTypeList),
                    '_default -> "--- Choose the parameter type ---",
                    '_label -> None
                )</td>
                <!-- Candidate Parameter Chosen-->
                <td>@select(subtaskForm("subtaskList["+index+"].chosenParameter"),
                    options = options(models.qosdatamodel.Subtask.candidateParams(subtaskForm.get.subtaskList.get(index).getAssociatedTask.getId)),
                    '_default -> "--- Choose the candidate parameter ---",
                    '_label -> None
                )</td>
                <!-- Chosen Upper Bound Formula-->
                <td>@select(subtaskForm("subtaskList["+index+"].chosenUpperBound"),
                    options = options(models.qosdatamodel.Subtask.boundList),
                    '_default -> "--- Choose the upper bound ---",
                    '_label -> None
                )</td>
                <!-- Chosen Lower Bound Formula-->
                <td>@select(subtaskForm("subtaskList["+index+"].chosenLowerBound"),
                    options = options(models.qosdatamodel.Subtask.boundList),
                    '_default -> "--- Choose the lower bound ---",
                    '_label -> None
                )</td>

retrieves the following keys and values upon submission:

[debug] application - put: <models.qosdatamodel.SubtaskList.subtaskList[0].typeChosen,cost of service execution>

[debug] application - put: <subtaskList[0].chosenParameter,Execution Time>

[debug] application - put: <subtaskList[0].chosenUpperBound,Sum>

[debug] application - put: <subtaskList[0].chosenLowerBound,Product>

[debug] application - put: <models.qosdatamodel.SubtaskList.subtaskList[1].typeChosen,throughput>

[debug] application - put: <subtaskList[1].chosenParameter,Total Output>

[debug] application - put: <subtaskList[1].chosenUpperBound,Product>

[debug] application - put: <subtaskList[1].chosenLowerBound,Min>

[debug] application - put: <models.qosdatamodel.SubtaskList.subtaskList[2].typeChosen,uptime probability>

[debug] application - put: <subtaskList[2].chosenParameter,Total Availability>

[debug] application - put: <subtaskList[2].chosenUpperBound,Sum>

[debug] application - put: <subtaskList[2].chosenLowerBound,Product>


For me it seems like only typeChosen was successfully mapped to a variable in an Entity.

Quirin Stockinger

unread,
May 4, 2015, 7:43:12 AM5/4/15
to play-fr...@googlegroups.com
Does anyone have an idea? I am still struggling with this.
Thank you.
Reply all
Reply to author
Forward
0 new messages