In my spring mvc application, I have levels that users can create. With these levels, there are various requirements that a level needs in order to be taken (need a car, a phone, etc).
When creating a new level, the user can see a list of all these requirements and move those requirements into the required requirements area (by clicking on them to move them back and forth from one div to another). It would look a little something like this jsp
<div id="allRequirements">
<c:forEach var="requirement" items="${RequirementList}">
<div class="requirements">
<input type="hidden" value="${requirement.id}" name="id"/>
<h2><c:out value="${requirement.name}"/></h2>
</div>
</c:foreach>
</div>
<div id="requiredRequirements"></div>
The RequirementList
is just a model attribute that returns a list of requirements
The model for the level and requirement look like this:
public class Level {
private String name;
private int id;
private int points
private List<Requirement> requirements;
....
}
public class Requirement{
private String name;
private String id;
....
}
and the method for this add functionality in the controller looks like this
@RequestMapping(value = "/level/addNewLevel", method = RequestMethod.POST)
public String addNewLevel(@ModelAttribute("level") Level level, BindingResult result, Model model)
{
validator.validate(level, result);
if(result.hasErrors()) {
//show errors
}
else {
//add level
}
}
So now onto my problem:
I can get the name, points, id, etc of the level just fine but the Requirements are not coming over at all. I tried to insert <input type='hidden' value='' + id +'' name="requirements"/>
in the divs that are in the requiredRequirements when the form is submitted and the do something like this
String[] requiredRequirements = ((String) result.getFieldValue("requirements")).split(",");
level.setRequirements(getRequirementsFromIDs(requiredRequirements));
This works fine until i call the validate method because in the binding result, the requirements is just a list of strings (from the hidden field called requirements) so it throws a type mismatch. I thought about writing a property editor but that seems like a hack to fix a hack.
I was wondering if anyone had any advice on how to fix this problem.
Thanks in advance