バインド可能な値が実際には1つしかないフォームで、Springの強力なバインドツール(エラー/検証/タグライブラリなど)を使用したいと思います。単一のStringプロパティ( "name")を含むオブジェクトを使用するのではなく、基本的なStringをコマンドオブジェクトとして使用しようとしていますが、これをすべてのオブジェクトで機能させる方法(またはその場合)がわかりません。同じトップレベル。ネストされた文字列でオブジェクト(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];
}
そして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>
そして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>