I'm trying out some bean validation and I'm stumbling upon 'strange' behavior. I'm working with Glassfish and Primefaces as a front-end (if it makes any difference). Elsewhere in my project I use the Hibernate-validator, I'm not sure if it is validating JSF (else it is the default in Glassfish). I have a managed bean:
@javax.faces.bean.ManagedBean
@javax.faces.bean.ViewScoped
public class TestBean
{
@Size(min=8)
private String _testString;
public String getTestString()
{
return _testString;
}
public String setTestString(String testString)
{
_testString = testString;
}
public void doSomethind()
{
// Do something with the test string
}
}
And a JSF page containing this:
<h:form id="testForm">
<h:outputLabel for="testInput" value="Input:"/>
<p:inputText id="testInput" value="#{testBean.testString}"/>
<p:message id="testInputMsg" for="testInput"/>
<p:commandButton value="Aanmaken" action="#{testBean.doSomething}" update="@form"/>
</h:form>
The _testString
does not get validated this way. However, bean validation does work when I change the field to:
@Size(min=8)
private String testString;
or when I annotate the getter in stead of the field:
private String _testString;
@Size(min=8)
public String getTestString()
{
return _testString;
}
Following our coding guidelines, we must prefix private fields with an underscore. Which leaves me with one option; annotate the getter.
Could someone care to explain why it is behaving like this?
From the JSR 303 specification:
3.2. Constraint declaration
Constraint declarations are placed on classes or interfaces primarily through annotations. A constraint annotation (see Section 2.1), can be applied to a type, on any of the type's fields or on any of the JavaBeans-compliant properties.
(emphasis mine)
From the JavaBeans specification:
8.3.1 Simple properties
By default, we use design patterns to locate properties by looking for methods of the form:
public <PropertyType> get<PropertyName>(); public void set<PropertyName>(<PropertyType> a);
So, with a property name of _testString
, it's looking for a getter/setter called get_testString()
and set_testString()
which doesn't exist in your case.
Your code guidelines conflicts the JavaBeans specficiation and hence JSR-303 bean validation simply won't work when you put the annotation on a property whose name conflicts the JavaBeans specification. JSR-303 can't find the getter/setter associated with the property name and hence won't be able to perform validation when they are called.
Either fix your code guidelines to comply the standards, or put the annotation on the getter instead and live with it. See further also the Standard Java Code Conventions.
来源:https://stackoverflow.com/questions/11074802/bean-validation-not-working-if-name-variable-is-prefixed-with-underscore