Home > Design, 趨勢, Java, JavaScript, Programming, Technologies, Trends, 技術 > Validation with Annotations — 驗證新趨勢

Validation with Annotations — 驗證新趨勢

September 28, 2006 (Thursday) Leave a comment Go to comments

Validation 是每個像樣一點的系統必備的功能,不過做法就有很多了。以 Java Web AP 為例,從 JavaScript 的 Validation 到使用 Java Code 去做,或是使用 Jarkata Commons Validator 都是很常見的方法,也各有優缺點。

最近在網路上出現了一些跟 Validation 相關的文章,包括有 Hibernate can meet your validation needsSpring Validation with Java Annotations。或許你會奇怪,Hibernate 跟 Spring 並不是在處理 Validation 的事情的。到底是怎麼回事呢?原來現在最流行的 Validation 方法是使用 Annotation!在 domain model class 的 getter/setter 上加上 Annotation Tag,例如:@Required 就代表這個值是必需填入的。@Length(max = 10) 代表最長不能起過 10 個字元等等。在 domain model 宣告完成後,在需要時再由 Java 程式去執行驗證,而驗證是自動依據 Annotation 的設定所做的。

不管用 Hibernate 或是 Spring 做,概念上都大同小異。把 Validation 資訊放在 domain model 的好處是,不管在哪個 level 想做驗證,都會用到同樣的資訊。否則很有可能會變成在 User Input 寫一段,存取 DB 時又寫一段。而事實上驗證的資訊放在 domain model 事實上是最適合的了,因為驗證限制可以說是 domain model 的一項註解。

Annotation 在 Java 5 出現以來,有很多範例應用。但相對實戰上使用,而且必需要使用 Annotation 才能達成的任務一直都不是很多。直到最近,不少 Annotation 應用都已經成熟。不久的將來,Annotation 就會變成常用的 Java 語法,而系統中出現 Annotation 就跟出現平常的 code 一樣自然。

technorati tags:, , ,

Blogged with Flock

Advertisements
  1. May 13, 2013 (Monday) at 21:04

    I have to show some thanks to

    this writer for rescuing me from this particular

    setting. After surfing around through the

    search engines and finding

    thoughts that were not

    pleasant, I figured my entire life was

    done. Being alive without the

    strategies to the issues you’ve solved all through your main article content is a critical case, as well as the ones which could have negatively

    affected my career if I hadn’t encountered your web blog.

    The know-how and kindness in touching all the pieces was

    tremendous. I don’t know what I would’ve done if I hadn’t discovered such a point like this. I can at

    this time relish my future. Thanks for your time so

    much for the skilled and

    results-oriented help. I will not

    think twice to refer your blog to any person who desires tips

    on this situation.

  2. August 20, 2013 (Tuesday) at 1:40

    You actually make it seem so easy with your presentation but I find this matter to
    be really

    something that I think I would never understand.
    It seems too complicated and very broad for

    me. I’m looking forward for your next post, I will try to get the hang of it!

  3. lavondalavigne
    September 7, 2013 (Saturday) at 5:58

    Very nice post. I just stumbled upon your blog and wanted to say that I have

    truly enjoyed surfing around your blog posts. After
    all I’ll be subscribing to your

    feed and I hope you write again very soon!

  4. September 12, 2013 (Thursday) at 9:45

    Hello there, You’ve done an excellent job. I will

    certainly digg it and personally suggest to my friends. I’m confident they
    will be benefited from this website.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: