Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bean validation VS JSF validation

Tags:

jsf

jakarta-ee

When facing the problem of validating a property in a JSF2 application there are two main approaches.

Defining the validation on the ManagedBean using an Annotation

@ManagedBean
public class MyBean {
    @Size(max=8)
    private String s;

    // Getters setters and other stuff.
}

or declaring it on the jsf page:

<h:inputText value="#{myBean.s}">
    <f:validateLength maximum="8"/>
</h:inputText>

It happens that I can't decide for none of them. The first one is nice because it removes some code from the jsf pages (which is always good since those pages are not eye friendly by definition) but makes harder to see 'at a glance' what's going on with the page when checking the jsf file.

Which one do you think is clearer? Nicer? Better?

like image 866
Toni Penya-Alba Avatar asked Mar 19 '10 11:03

Toni Penya-Alba


People also ask

What is JSF validator?

JSF provides inbuilt validators to validate its UI components. These tags can validate the length of the field, the type of input which can be a custom object. For these tags you need to use the following namespaces of URI in html node.

How can you explain Bean validation?

JavaBeans Validation (Bean Validation) is a new validation model available as part of Java EE 6 platform. The Bean Validation model is supported by constraints in the form of annotations placed on a field, method, or class of a JavaBeans component, such as a managed bean. Constraints can be built in or user defined.

What is Bean Validation in spring?

The Bean Validation API is a Java specification which is used to apply constraints on object model via annotations. Here, we can validate a length, number, regular expression, etc. Apart from that, we can also provide custom validations. As Bean Validation API is just a specification, it requires an implementation.

What is Jakarta validation API?

Jakarta Bean Validation is a Java specification which. lets you express constraints on object models via annotations. lets you write custom constraints in an extensible way. provides the APIs to validate objects and object graphs. provides the APIs to validate parameters and return values of methods and constructors.


3 Answers

I would pump for validation on the ManagedBean, this removes logic from the JSF the VIEW in model view Controller. and should keep the JSF souly responsible for displaying the Model. Also having this on the managed bean ensures that where ever this is updated validation is applied. This is more DRY(Don't repeat yourself).

like image 126
David Waters Avatar answered Dec 01 '22 01:12

David Waters


There is another advantage of the managedBean approach. If the information being displayed by the JSF is also available via a web service (WS) then the actual validation code can be factored out into a validation class and used for both the JSF and the WS ensuring that all information in the system is valid.

like image 28
Martin Avatar answered Dec 01 '22 02:12

Martin


Richfaces allows you to use them together. See <rich:graphValidator> (and beanValidator as well).

These tags say: "apply JSF validation based on javax.validation (or Hibernate validator) rules".

like image 30
Bozho Avatar answered Dec 01 '22 03:12

Bozho