0

私は次の(骨に簡略化された)コントローラーを持っています:

@Controller  
public class TestController  {

@RequestMapping(value = "/test.htm", method = RequestMethod.GET)
public String showForm(final ModelMap map) {
    final TestFilter filter = new TestFilter();
    filter.setStartDate(new Date(System.currentTimeMillis()));
    map.addAttribute("reportPerResourceForm", filter);
    return "test";
}

@InitBinder
public void initBinder(final WebDataBinder binder) {
    binder.registerCustomEditor(Date.class, null, new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), true));
}

}

jsp:

<form:form commandName="reportPerResourceForm" id="reportForm">
    <form:input path="startDate" />
</form:form>

これは、別のビュー コントローラーで発生した問題をテストするために、すぐに作成したコントローラーです。コントローラでわかるように、CustomeDateEditor が定義されています。私の実際のコントローラーでは、このエディターは正常に動作しています。たとえば、フォーム フィールドに 11/01/2010 と入力すると、これはエディターによって適切に日付に変換されます。また、フォームに戻ると、日付は再び文字列にうまく変換されました。

ただし、(TestController のように) フォームにデフォルトの日付を設定したい場合は、CustomDateEditor.getAsText() からの戻り値を使用する代わりに、フォーム フィールドに Date.toString() を表示するだけです! いくつかのデバッグの後、RequestMethod == GET の場合に InitBinder メソッドが呼び出されないことがわかりました。これは正常ですか?

使用しないことでこれを回避できると確信しています

助けてくれてありがとう、
スタイン

4

3 に答える 3

3

@ModelAttributeページに転送する前にドメインを設定するために使用します。

スプリングを扱うときに慎重に使用newすると、スプリング コンテキスト外でオブジェクトの新しいインスタンスが作成されるだけであり、スプリング機能 (Web バインディング、検証など) を使用することはできません。

例 :

@RequestMapping(value = "/test.htm", method = RequestMethod.GET)
public String showForm(@ModelAttribute yourDomain, final ModelMap map)

そしてあなたのドメインであなたは使うことができます:

@DateTimeFormat(pattern="dd/MM/yyyy")
private Date balance = new Date(System.currentTimeMillis());
于 2010-12-28T01:22:51.690 に答える
1

よくわかりませんが、registerCustomEditor メソッドの 2 番目の引数が null に設定されています。この引数は、エディターを関連付けるフィールド名を設定するためのものなので、null に設定するとどうなるか正確にはわかりません。このエディターを特定のタイプのすべてのフィールドで使用する場合は、このパラメーターなしで同じメソッドが存在します。

public void registerCustomEditor(Class requiredType, PropertyEditor propertyEditor)

これで問題が解決するかどうかはわかりませんが、これを試してみます。

binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), true));

それが役に立てば幸い。

于 2010-04-15T07:55:43.877 に答える
0

これを解決するために、私自身がコントローラーに次のコードを持っています:



        @InitBinder
        public void initBinder(WebDataBinder binder) {
            binder.registerCustomEditor(Category.class, new CategoryEditor(categoryService));
        }

        @ModelAttribute("categoryList") // Populate reference-data (EG select-lists) in the view. (p. 390-

    391).
        public List<Category> populateCategoryList() {
            return categoryService.list();
        }

        // Note: without adding "BindingResult result" to the following prototype
        // (and preceding it with a @ModelAttribute("categoryList") -
        // my initBibder() method does not get called!
        // I discovered and added this hokum in response to the following links:
        // http://forum.springsource.org/showthread.php?46837-InitBinder-not-called
        // http://forum.springsource.org/showthread.php?46876-Custom-date-format-on-GET-requests&p=154820
        @RequestMapping("/site/list.htm")
        @ModelAttribute("sites")  // 20110819
        public ModelAndView listSite(
                @ModelAttribute("category") Category category,
                BindingResult result
                )
        {
    //        List<Site> sites = siteService.list();
            List<Site> sites = new ArrayList<Site>(); // = siteService.list();
            return new ModelAndView("siteList", "sites", sites);
        }
    }


@InitBinder が呼び出されていないため、「Category」クラスが認識されないという問題がありました。ここでの「秘密」は、「@RequestMapping」メソッドを変更して、プロトタイプに必要のない 2 つのパラメーターを含めることでした:
@ModelAttribute("category") カテゴリ カテゴリ、
BindingResult 結果
これですべてが解決しました (魔法ではないことがわかっています) 、煙、ミラー、Java リフレクションだけです。しかし、印刷物やオンラインの資料が、このような単純なユースケースを適切に扱っていることを願っています)。

対応する JSP ファイルの関連コードは次のとおりです。



        <div>
        Select a category: 
        <form:select path="category">
                    <form:options items="${categoryList}" itemValue="id" itemLabel="name" 

    />
        </form:select>
        </div>

于 2011-09-15T07:39:31.633 に答える