0

バインド可能な値が実際には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>
4

2 に答える 2

0

文字列は不変です。MyObject を使用する場合、Spring が変更するのは、String 自体ではなく、String へのオブジェクトの参照です。

于 2013-01-25T21:39:04.623 に答える
0

あなたのケースはおそらく2つのことが原因だと思います:

1) 文字列は不変オブジェクトです。新しい文字列を割り当てるたびに、新しい参照値が返されることを意味します

2) メソッドのパラメーターが値で渡されます。String は参照型であるため、オブジェクト参照の参照値は変更できません

例えば

public static void change(String text) { // (1) pass by value
   text = "Hello Word"; // (2) immutable
}

public static void main(String[] args) {
  String str = "Hello";
  change(str);
  System.out.println(str); // Output: Hello
}

String オブジェクト参照を別のオブジェクト内にラップすると、change メソッドに渡されるオブジェクト参照は、 String 参照ではなくmyObjectになります。そのため、myObject を使用すると String オブジェクト参照の参照値を変更できます。

于 2013-01-28T12:57:35.677 に答える