In my spring mvc application, I have levels that users can create. With these levels, there are various requirements that must be met for the level (you need a car, phone, etc.).
When creating a new level, the user can see a list of all these requirements and move these requirements to the required requirements area (by clicking on them to move them back and forth from one div to another). It would look something like 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>
RequirementList
is just a model attribute that returns a list of requirements
The model for level and requirement is as follows:
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 function in the controller is as follows:
@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 to my problem:
I can get name, periods, id, etc. level is just fine, but the requirements do not come at all. I tried pasting <input type='hidden' value='' + id +'' name="requirements"/>
in divs that are in the required requirements when submitting the form and doing something like this
String[] requiredRequirements = ((String) result.getFieldValue("requirements")).split(","); level.setRequirements(getRequirementsFromIDs(requiredRequirements));
This works fine until I call the validate method, because as a result of binding the requirements, this is just a list of strings (from a hidden field called requirements), so it gives out a type mismatch. I was thinking of writing a property editor, but this seems like a hack fix.
I was wondering if anyone has any tips for fixing this problem.
Thank you in advance