10. Quiz Questions

If you would like to look up the answers for the following quiz questions, you can check our discussion forum. If you don't find an answer in the forum, you may create a post asking for an answer to a particular question.

10.1. Question 1: String length validation

Complete the following code, with the correct Java Bean validation annotation, so that the attribute title has a minimum length of 2 and a maximum length of 40:

@Entity @Table( name="books")
public class Book {
  private String title;

10.2. Question 2: Requiring a value

Which validation annotations are needed for requiring a (not-null) value for an attribute both in the Java EE model layer and in the database of an app?

  1. @NotNull( message = "A value is required")

  2. @Column( nullable = false)

  3. @Size( min = 0)

  4. @Min( value = 0)

10.3. Question 3: Defining a custom validation annotation

Complete the following code for defining a @LessThan100YearsOld annotation that checks if a given Date attribute value does not date back more than 100 years and the Java implementation class is LessThan100YearsOldImpl:

@Target( ElementType.________________ )
@Constraint( validatedBy = ______________________________)
public @interface LessThan100YearsOld {
  String message() default "The date must not be older than 100 years!";
public class LessThan100YearsOldImpl implements 
    ConstraintValidator<LessThan100YearsOld, _____________________> {
  @Override public void initialize( LessThan100YearsOld arg0) { ... }
  @Override public boolean isValid( Date date, ConstraintValidatorContext context) { ... }

10.4. Question 4: JSF custom validator method

Complete the following JSF snippet such that the title attribute value of the entity book is validated by the custom method Book::checkTitle:

<h:form id="createBookForm">
  <h:outputLabel value="Title: ">
    <h:inputText id="title" value="#{book.title}" 
       ______________="#{__________________________}" />

10.5. Question 5: Show validation error messages with JSF

Complete the following JSF snippet, such that any (possible) error messages resulting from validating the title attribute value of the book entity are displayed in the user interface:

<h:form id="createBookForm">
  <h:outputLabel for="title" value="Title: ">
    <h:inputText id="title" value="#{book.title}" />
  <h:__________________ for="_______________" errorClass="error" />