我希望在实际上只有一个可绑定值的表单上使用 Spring 强大的绑定工具(错误/验证/标记库/等)。我没有使用包含单个字符串属性(“名称”)的对象,而是尝试使用基本字符串作为命令对象,但我不确定如何(或是否)让它与相同的顶级。这是一个使用带有嵌套字符串的对象 (MyObject) 的示例 - 这很好用。我想只使用一个字符串,但是当我将 MyObject 更改为 String 时,为该字符串输入的任何内容都不会反映在下一页中。我究竟做错了什么?
@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];
}
并输入Value.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>
并确认Value.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>