I wish to use Spring's powerful binding tools (errors/validation/tag library/etc.) on a form that really only has one bindable value. Rather than having an object containing a single String property ("name"), I'm trying to use a basic String as the command object, but I'm not sure how (or if) to get this to work with everything at the same top-level. Here's an example using the object (MyObject) with a nested string - this works fine. I'd like to just use a string, but when I change MyObject to String, whatever is entered for this String is not reflected in the next page. What am I doing wrong?
@Controller
@SessionAttributes("command")
public class TestController {
private static final String[] WIZARD_PAGES = new String[] {
"enterValue",
"confirmValue"
};
@RequestMapping(value = "doStuff.action", method = RequestMethod.GET)
public String formBackingObject(HttpServletRequest request, HttpServletResponse response, final ModelMap modelMap) {
MyObject entry = new MyObject();
modelMap.put("command", entry);
return WIZARD_PAGES[0];
}
@RequestMapping(value = "doStuff.action", method = RequestMethod.POST)
public String onSubmit(HttpServletRequest request, HttpServletResponse response, final ModelMap modelMap,
final @ModelAttribute("command") MyObject entry,
final Errors errors, SessionStatus status,
@RequestParam(value = "_page") Integer currentPage,
@RequestParam(value = "_finish", required=false) Object finish,
@RequestParam(value = "_cancel", required=false) Object cancel) {
// submitting
if (finish != null) {
// do submit stuff here
status.setComplete();
return "redirect:/home.action";
}
// cancelling
if (cancel != null) {
status.setComplete();
return "redirect:/home.action";
}
// moving to next page
int targetPage = WebUtils.getTargetPage(request, "_target", currentPage);
if (targetPage >= currentPage) {
// validate current page here
switch(currentPage) {
case 0: // TODO: call validation
if (!errors.hasErrors()) {
// do some stuff to prepare the next page
}
break;
}
}
if (errors.hasErrors())
return WIZARD_PAGES[currentPage];
return WIZARD_PAGES[targetPage];
}
And enterValue.jsp:
<form:form commandName="command">
<form:input path="name"/>
<input type="submit" name="_target1" value="<spring:message code="COMMON.NEXT"/>" />
<input type="hidden" name="_page" value="0" />
</form:form>
And confirmValue.jsp:
<form:form commandName="command">
${name}
<input type="submit" name="_finish" value="<spring:message code="COMMON.SUBMIT"/>" />
<input type="hidden" name="_page" value="1" />
</form:form>