I have an arrayList of values called "selectedMods" that I've added as a ModelAttribute in my controller. I want to then use this arrayList in my jsp page. I was hoping to somehow traverse through the arrayList in my jsp page and then compare each value in the arrayList to the checkbox item values, and if one the checkbox values equals a value in the arraylist, I'd like to check that checkbox. Here's my jsp code so far for displaying the checkboxes:
<div class="control-group">
<label class="control-label" for="moduleIds"><spring:message code="label.modules"/></label>
<div class="controls">
<c:forEach items="${modules}" var="module">
<label class="checkbox"><input type="checkbox" name="moduleIds" value="${module.id}" <c:if test="${selectedMods.contains(module.id)">checked="checked"</c:if> /> ${module.title}</label>
</c:forEach>
<form:errors path="moduleIds" cssClass="form-errors" />
</div>
</div>
Here's my controller method that sets the ModelAttribute:
@RequestMapping(method = RequestMethod.POST, value = "/save")
public final String save(final ModelMap model, @Valid AssignTraining assignTraining, BindingResult result) {
String formFile;
if (result.hasErrors()) {
formFile = "userTraining/select";
model.addAttribute("selectedMods", assignTraining.getModuleIds());
model.addAttribute("modules", moduleDao.findByDeletedFalse());
//String[] selectedCheckBoxes = request.getParameterValues("moduleIds");
//add something here maybe?
} else {
List<Module> modules = moduleDao.findAll(new ArrayList<Long>(assignTraining.getModuleIds()));
List<TrainingEntry> trainingEntries = TrainingEntryUtil.scheduleTraining(assignTraining.getTmNumber(),
modules);
for (TrainingEntry entry : trainingEntries) {
trainingEntryDao.save(entry);
}
formFile = "redirect:/userTraining/view?tmNumber=" + assignTraining.getTmNumber();
}
return formFile;
}
Any help is much appreciate.
Aucun commentaire:
Enregistrer un commentaire