質問

私はこのようなフィールドを検証するための単純なフォームに取り組んでいます。

public class Contact {

    @NotNull
    @Max(64)
    @Size(max=64)
    private String name;

    @NotNull
    @Email
    @Size(min=4)
    private String mail;

    @NotNull
    @Size(max=300)
    private String text;


}

ゲッターとセッターの休止状態の依存関係もクラスパスに提供しています。しかし、単純なフォームを検証する方法がまだわかりません。実際には、春の休止状態の組み合わせに関するドキュメントはあまりありません。

@RequestMapping(value = "/contact", method = RequestMethod.POST)
public String add(@Valid Contact contact, BindingResult result) {
    ....
}

オリジナルの Spring 3.x ドキュメントを除いて、それを説明するか、チュートリアルを教えていただけますか

役に立ちましたか?

解決

@Valid アノテーションを使用してバッキング Bean の検証をトリガーする場合。これは Hibernate アノテーションではなく、検証 API の javax.validation.Valid です。

実行するには必要があります 両方:

私の場合、検証を行うためにバッキング Bean のフォーム フィールドにアノテーションを付ける代わりに、カスタム バリデータ (RegistrationValidator) を使用しました。エラー メッセージに I18N キーを設定する必要があるため、Spring 3 MessageCodeResolver を独自のものに置き換える必要がありました。Spring 3 のオリジナルのものは、最初の方法で見つからない場合、常に型またはフィールド名を追加して正しいキーを見つけようとします。

ちょっとした例:

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
...
import javax.validation.Valid;

@Controller
public class RegistrationController
{
    @InitBinder
    protected void initBinder(WebDataBinder binder) 
    {
        binder.setMessageCodesResolver(new MessageCodesResolver());
        binder.setValidator(new RegistrationValidator());
    }

    @RequestMapping(value="/userRegistration.html", method = RequestMethod.POST)
    public String processRegistrationForm(@Valid Registration registration, BindingResult result, HttpServletRequest request) 
{
         if(result.hasErrors())
         {
            return "registration"; // the name of the view
         }

         ...
    }
}

これがお役に立てば幸いです。

ところで。Bean Validation API の公式 Web ページを知っている人がいたら教えてください...ありがとう。

他のヒント

私はこの1つが答えていることを知っている...しかし、ここで私の$、0.02の価値がとにかくだ:-)私はブラクが参照したのと同じ例を使用していた、と検証が自動的に呼び出されていませんでした...私は追加する必要がありました私のアプリケーションのコンテキストファイルに<mvc:annotation-driven /> ...(その後、検証がトリガされました)。あなたは、スキーマ宣言にMVC詳細を追加してください...例は...

を次の
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/mvc
        ">

    <mvc:annotation-driven />
...
</beans>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top