`

Java EE6中的新框架 Bean的验证

 
阅读更多

转:http://xiangtui.iteye.com/blog/970951,

 

验证数据是应用程序生命周期中一个常见的任务,例如,在应用程序的表示层,你可能想验证用户在文本框中输入的字符数最多不超过20个,或者想验证用户在数字字段输入的字符只能是数字。
                                                                                    
     开发人员在应用程序的各层中通常使用相同的验证逻辑,或者将验证逻辑放在数据模型中。

     Bean验证(JSR 303)使验证变得更简单了,减少了重复,错误和凌乱,Bean验证提供了一个标准的验证框架,在框架中相同的验证集可以在应用程序的所有层之间共享。

    Bean验证提供了一个框架验证遵循JavaBean规范编写的Java类,你可以使用 注解指定一个JavaBean上的约束,你可以注解一个JavaBean类、字段或属性,你也可以通过XML描述符扩展或覆盖这些约束,验证类验证每个约 束时,你要指定那个验证类用于给定的约束类型。

 


例如,下面是通过Bean验证注解声明一些约束
public class Address{ 
  @NotNull @Size(max=30)
  private String addressline1;
  @Size(max=30)
  private String addressline2;
   ... 
   public String getAddressline1(){ 
    return addressline1;
   } 
   public void setAddressline1(String addressline1) { 
      this.addressline1=addressline1; 
    } 
     ... 
   }



@NotNull注解指定了注解元素addressline1绝不能为空,@Size注解指定注解元素addressline1和addressline2决不能大于指定的长度(30个字符)。

     验证Address对象时,addressline1的值传递给为@NotNull约束定义的验证类,同时还要传递给为@Size约束定义的验证类,addressline2的值也要传递给为@Size约束定义的验证类。

    @NotNull 和@Size约束内置于Bean验证框架中,因此你不需要为它们定义验证类,但你可以在内置约束上添加自己的约束,那时就需要定义自己的验证类了。例如, 你可以定义如下的@ZipCode约束:
@Size(min=5,max=5)
@ConstraintValidator(ZipcodeValidator.class)
@Documented
@Target({ANNOTATION_TYPE, METHOD,?FIELD})
@Retention(RUNTIME)
public @interface ZipCode{
  String message() default "Wrong zipcode";
  String[] groups() default {};
}

然后你可以在类、字段或属性上象其它约束定义那样定义@ZipCode约束,如:

public class Address {
...
@ZipCode
private String addressline3;

public String getZipCode(){
return zipCode;
}

public void setZipCode(String zipCode){
this.zipCode=zipCode;
}

Address对象验证时,addressline3的值传递给 ZipcodeValidator类进行验证,注意约束定义包括另一个约束:@Size(min=5, max=5),这意味着由@ZipCode注解注解的元素必须恰好是5个字符,除了执行ZipcodeValidator进行主要的约束检查外,需要再次 使用这个约束对元素进行验证。Bean验证允许你创建一个由其它约束组成的约束,还要注意约束定义了一个错误消息,如果验证检查约束失败就返回这个错误消 息,这里的错误消息是“Wrong zipcode”。

你也可以使用Bean验证验证一个完整的对象图(object graph),一个对象图是由其它对象组成的一个对象,如果你在对象图的根对象上指定@Valid注解,它会指引相关的验证器递归验证对象图中的关联对象,如:

public class Order{

 @OrderNumber private String orderNumber;
 @Valid @NotNull private Address delivery;
}

Order对象验证时,Address对象和对象图中关联的对象也被验证。

为了满足在应用程序各层之间共享相同的验证集,Java EE 6平台整合了Bean验证。例如,表示层技术,如JSF,以及企业级技术,如JPA,都可以通过Bean验证框架访问约束定义和验证器,你不再需要在多个地方指定约束。

 

 

-------------------------------------《Spring in Action3》7.4.3 Validating input-------------

 

When a user registers with the Spitter application, there are certain requirements that
we’d like to place on that registration. Specifically, a new user must give us their full
name, email address, a username, and a password. Not only that, but the email
address can’t be just freeform text—it must look like an email address. Moreover, the
password should be at least six characters long.
The @Valid annotation is the first line of defense against faulty form input. @Valid
is actually a part of the JavaBean validation specification.4 Spring 3 includes support
for JSR-303, and we’re using @Valid here to tell Spring that the Spitter object should
be validated as it’s bound to the form input.
Should anything go wrong while validating the Spitter object, the validation error
will be carried to the addSpitterFromForm() method via the BindingResult that’s
passed in on the second parameter. If the BindingResult’s hasErrors() method
returns true, then that means that validation failed. In that case, the method will
return spitters/edit as the view name to display the form again so that the user can
correct any validation errors.
But how will Spring know the difference between a valid Spitter and an invalid
Spitter?
DECLARING VALIDATION RULES
Among other things, JSR-303 defines a handful of annotations that can be placed on
properties to specify validation rules. We can use these annotations to define what
“valid” means with regard to a Spitter object. The following shows the properties of
the Spitter class that are annotated with validation annotations.

@Size(min=3,max=20,message=
"Usernamemustbebetween3and20characterslong.")
@Pattern(regexp="^[a-zA-Z0-9]+$",
message="Username mustbealphanumericwithnospaces")
private String  username;
@Size(min=6,max=20,
message="The passwordmustbeatleast6characterslong.")
private String  password;
@Size(min=3,max=50,message=
"Yourfullnamemustbebetween3and50characterslong.")
private String fullName;
@Pattern(regexp="[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}",
message="Invalid emailaddress.")
private String  email;
 

With these annotations in place, when a user submits a registration form to
SpitterController’s addSpitterFromForm() method, the values in the Spitter
object’s fields will be weighed against the validation annotations. If any of those rules
are broken, then the handler method will send the user back to the form to fix the
problem.
When they arrive back at the form, we’ll need a way to tell them what the problem
was. So we’re going to have to go back to the form JSP and add some code to display
the validation messages. DISPLAYING VALIDATION ERRORS
Recall that the BindingResult passed in as a parameter to addSpitterFromForm()
knew whether the form had any validation errors. And we were able to ask if there
were any errors by calling its hasErrors() method. But what we didn’t see was that the
actual error messages are also in there, associated with the fields that failed validation.
One way of displaying those errors to the users is to access those field errors
through BindingResult’s getFieldError() method. But a much better way is to use
Spring’s form binding JSP tag library to display the errors. More specifically, the
<sf:errors> tag can render field validation errors. All we need to do is sprinkle a few
<sf:errors> tags around our form JSP.

 

<%@ taglibprefix="sf"uri="http://www.springframework.org/tags/form"%>
<div>
<h2>CreateafreeSpitteraccount</h2>
<sf:form method="POST"modelAttribute="spitter"
enctype="multipart/form-data">
<fieldset>
<tablecellspacing="0">
<tr>
<th><sf:label path="fullName">Fullname:</sf:label></th>
<td><sf:input path="fullName"size="15"/><br/>
<sf:errors path="fullName"cssClass="error"/>
</td>
</tr>
<tr>
<th><sf:label path="username">Username:</sf:label></th>
<td><sf:input path="username"size="15"maxlength="15"/>
<small id="username_msg">Nospaces,please.</small><br/>
<sf:errors path="username"cssClass="error"/>
</td>
</tr>
<tr>
<th><sf:label path="password">Password:</sf:label></th>
<td><sf:password path="password"size="30"
showPassword="true"/>
<small>6 charactersormore(betricky!)</small><br/>
<sf:errors path="password"cssClass="error"/>
</td>
</tr>
<tr>
<th><sf:label path="email">EmailAddress:</sf:label></th>
<td><sf:input path="email"size="30"/>
<small>In caseyouforgetsomething</small><br/>
<sf:errors path="email"cssClass="error"/>
</td>
</tr>
<tr>
<th></th>
<td>
<sf:checkbox path="updateByEmail"/>

<sf:label path="updateByEmail"
>Send meemailupdates!</sf:label>
</td>
</tr>
<tr>
<th><label for="image">Profileimage:</label></th>
<td><input name="image"type="file"/>
</tr>
<tr>
<th></th>
<td><input name="commit"type="submit"

value="I accept.Createmyaccount."/></td>
</tr>
</table>
</fieldset>
</sf:form>
</div>

 

The <sf:errors> tag’s path attribute specifies the form field for which errors should
be displayed. For example, the following <sf:errors> displays errors (if there are
any) for the field whose name is fullName:
<sf:errorspath="fullName"cssClass="error"/>
If there are multiple errors for a single field, they’ll all be displayed, separated by an
HTML <br/> tag. If you’d rather have them separated some other way, then you can
use the delimiter attribute. The following <sf:errors> snippet uses delimiter to
separate errors with a comma and a space:
<sf:errorspath="fullName"delimiter=","
cssClass="error"/>
Note that there are four <sf:errors> tags in this JSP, one on each of the fields for
which we declared validation rules. The cssClass attribute refers to a class that’s
declared in CSS to display in red so that it catches the user’s attention.
With these in place, errors will be displayed on the page if any validation errors
occur. For example, figure 7.7 shows what the form would look like if the user were to
submit the form without filling in any of the fields.
Figure 7.7 With the <sf:errors> JSP tag on the registration page, validation problems
will be shown to the user for them to fix and try again.

 

 

 

 

 

分享到:
评论

相关推荐

    从Java走向Java+EE+.rar

    第4章 在Java EE中使用XML 26 4.1 什么是XML 26 4.1.1 理解XML 26 4.1.2 XML的语法 27 4.1.3 XML命名空间 31 4.2 XML能用来干什么 32 4.3 用DTD验证XML文档 33 4.4 用Schema验证XML文档 35 4.4.1 ...

    经典JAVA.EE企业应用实战.基于WEBLOGIC_JBOSS的JSF_EJB3_JPA整合开发.pdf

    第二部分详细讲解了jsf ri、jta、jndi、rmi、jms、javamail、ejb 3的session bean、message driven bean、jpa、jax-ws 2、jaas等java ee知识,这部分知识以jsf+ejb 3+jpa整合开发为重点,通过使用netbeans ide工具...

    Java高手真经 高级编程篇 part4/7

    本书讲解Java Web开发中的高级开发技术,包括企业级的开发技术EJB、各种Java EE的分布式开发技术、Java Web的各种开源技术与框架,这3部分内容层层递进,涵盖了Java EE开发中的各种分布式与业务核心技术。...

    Java高手真经 高级编程篇 part7/7

    本书讲解Java Web开发中的高级开发技术,包括企业级的开发技术EJB、各种Java EE的分布式开发技术、Java Web的各种开源技术与框架,这3部分内容层层递进,涵盖了Java EE开发中的各种分布式与业务核心技术。...

    Java高手真经 高级编程篇 part6/7

    本书讲解Java Web开发中的高级开发技术,包括企业级的开发技术EJB、各种Java EE的分布式开发技术、Java Web的各种开源技术与框架,这3部分内容层层递进,涵盖了Java EE开发中的各种分布式与业务核心技术。...

    Java高手真经 高级编程篇part2/7

    本书讲解Java Web开发中的高级开发技术,包括企业级的开发技术EJB、各种Java EE的分布式开发技术、Java Web的各种开源技术与框架,这3部分内容层层递进,涵盖了Java EE开发中的各种分布式与业务核心技术。...

    Java高手真经 高级编程篇part3/7

    本书讲解Java Web开发中的高级开发技术,包括企业级的开发技术EJB、各种Java EE的分布式开发技术、Java Web的各种开源技术与框架,这3部分内容层层递进,涵盖了Java EE开发中的各种分布式与业务核心技术。...

    Java高手真经 高级编程篇 part5/7

    本书讲解Java Web开发中的高级开发技术,包括企业级的开发技术EJB、各种Java EE的分布式开发技术、Java Web的各种开源技术与框架,这3部分内容层层递进,涵盖了Java EE开发中的各种分布式与业务核心技术。...

    java-demo:Java EE 8演示集合

    -&gt; Angular特征Java EE 8功能Bean验证2.0(alpha) CDI 2.0(alpha) JSONB Java EE 7功能Web套接字如何安装先决条件您需要maven,npm,git和应用程序服务器Java EE 该演示使用了一些仍在开发中的Java 8功能。...

    Java高手真经 高级编程篇 下载(一)

    本书讲解Java Web开发中的高级开发技术,包括企业级的开发技术EJB、各种Java EE的分布式开发技术、Java Web的各种开源技术与框架,这3部分内容层层递进,涵盖了Java EE开发中的各种分布式与业务核心技术。...

    将 Flex 集成到 Java EE 应用程序的最佳实践(完整源代码)

    这是一个典型的 Java EE 应用,使用了流行的 Spring 框架。为了简化数据库操作,我们使用了内存数据库 HSQLDB。对这个简单的应用,省略了 DAO,直接在 Façade 中通过 Spring 的 JdbcTemplate 操作数据库。最后,...

    Java高手真经 高级编程篇 part1/7

    本书讲解Java Web开发中的高级开发技术,包括企业级的开发技术EJB、各种Java EE的分布式开发技术、Java Web的各种开源技术与框架,这3部分内容层层递进,涵盖了Java EE开发中的各种分布式与业务核心技术。...

    validation-api-1.1.0.Final.jar

    JSR-303 是JAVA EE 6 中的一项子规范,叫做Bean Validation,现在一共有两个规范:BeanValidation1.0(即JSR303)和BeanValidation1.1(即JSR349),主要用于对数据进行校验,确保输入进来的数据从语 义上来讲是正确的。...

    Java高手真经 高级编程篇 下载(三)

    本书讲解Java Web开发中的高级开发技术,包括企业级的开发技术EJB、各种Java EE的分布式开发技术、Java Web的各种开源技术与框架,这3部分内容层层递进,涵盖了Java EE开发中的各种分布式与业务核心技术。...

    Java高手真经 高级编程篇 下载(四)

    本书讲解Java Web开发中的高级开发技术,包括企业级的开发技术EJB、各种Java EE的分布式开发技术、Java Web的各种开源技术与框架,这3部分内容层层递进,涵盖了Java EE开发中的各种分布式与业务核心技术。...

    Java高手真经 高级编程篇 下载(二)

    本书讲解Java Web开发中的高级开发技术,包括企业级的开发技术EJB、各种Java EE的分布式开发技术、Java Web的各种开源技术与框架,这3部分内容层层递进,涵盖了Java EE开发中的各种分布式与业务核心技术。...

    +Flex+集成到+Java+EE+应用程序的最佳实践(完整源代码)

    这是一个典型的 Java EE 应用,使用了流行的 Spring 框架。为了简化数据库操作,我们使用了内存数据库 HSQLDB。对这个简单的应用,省略了 DAO,直接在 Fa?ade 中通过 Spring 的 JdbcTemplate 操作数据库。最后,...

    java-ee-7-example

    rs类具有事务范围验证:用于验证现有模型文件夹结构: Java| _ bean:用于容纳数据库外部模型和dto模型的软件包| _ config:包以重命名配置类| _ dto:数据传输对象类的软件包| _实体:所有类的包都映射到数据库中的...

    JAVA程序开发大全---上半部分

    第15章 Java EE中EJB的开发 256 15.1 EJB概述 256 15.2 WebLogic服务器的安装与配置 257 15.2.1 WebLogic服务器的安装 257 15.2.2 WebLogic服务器的配置 258 15.2.3 MyEclipse中集成WebLogic服务器 260 15.3 使用...

    java web 开发详解

    Java 的 Web框架虽然各不相同,但基本也都是遵循特定的路数的:使用Servlet或者Filter拦截请求,使用MVC的思想设计架构,使用约定,XML或 Annotation实现配置,运用Java面向对象的特点,面向抽象实现请求和响应的...

Global site tag (gtag.js) - Google Analytics