表示形式=> CustomEditorsが定義されていない場合、スプリング@InitBinderが起動しません

StackOverflow https://stackoverflow.com/questions/2638848

質問

私は(骨のように簡略化)以下のいるコントローラーます:

@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が定義されています。私の実際のコントローラでは、このエディタは正常に動作しています。フォームフィールドにインスタンス2010年11月1日のために入力したとき、これはうまく編集者によって日付に変換されます。フォームに戻って行くときにも日付が文字列に再びうまく変換されたバックでした。

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

私は、私が使用していないことで、これを回避する可能性が確信しています

このあなたの助けのおかげで、
Stijn

役に立ちましたか?

解決

のページに転送する前に、セットアップドメインに使用@ModelAttributeます。

あなたは春に対処するとき、

は慎重に使用newに、それだけで、オブジェクトの外側バネコンテキストの新しいインスタンスを作成し、あなたが春の機能(例えばウェブが結合として、データ妥当性検証など)のいずれかを使用することはできません。

例:

@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());

他のヒント

私はわからないが、registerCustomEditorメソッドの第2引数はnullに設定されています。この引数は、私は、nullに設定されています場合に発生することが起こっている内容を正確に把握していないので、あなたがエディタを関連付けるフィールド名を設定することです。あなたは、特定のタイプのすべてのフィールドで、このエディタを使用したい場合は、このパラメータなしで同じ方法が存在する:

public void registerCustomEditor(Class requiredType, PropertyEditor propertyEditor)

私、私は必ずこれで問題が解決するわけではないんですけれども、これにしようとするだろう。

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

希望、それは助けています。

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



        @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が呼び出されていなかったので、私の問題は、私の「カテゴリー」クラスではありません認識されていました。 それのプロトタイプに - - 「秘密は」ここに含まれるように私の「@RequestMapping」方法を変更することでした2つのパラメータ これは、私は必要はありません。
            @ModelAttribute( "カテゴリ")カテゴリ分類、
            BindingResult結果
しかし、私は希望 - これは、すべて(私はそれが魔法、ちょうど煙、鏡およびJavaリフレクションではないことを知っているを解決しました )印刷とオンライン文献が適切にこのような単純なユースケースに対処します。

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



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

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top