English 中文(简体)
JSf 2 with Hibernate Validator 4 and Tomcat 6
原标题:

The problem I m having is that my Bean Validation isn t working as I would expect.

I have a Session Scoped Managed Bean with a name field that is bound to an h:inputText. The name must be entered, and have a minimum length of 1 character, and a maximum length of 5 characters. I expect that when I enter the name into the textbox, it will be validated accordingly by the backing bean, and if it fails, then it would display the corresponding error messages.

However, this is not the case. The validations are always failing, even if I enter a valid case in the inputText (e.g. "abc"). On debugging the application, it seems that the getName() accessor is always called, and the setter is never reached. Am I doing something wrong? I assume the validator uses the accessor to validate, but the problem is that the setter never gets a chance to update the value of the name... I must be missing something.

Below is the Managed Bean:

@ManagedBean
@SessionScoped
public class James implements Serializable {

  public James() {
    super();
   }

  private String name;

  @NotNull
  @Min(value = 1)
  @Max(value = 5)
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

Below is the fragment of the JSF xhtml. I tried a few different variations with separate forms, but the first option is the one I would assume to work (I think it s equivalent to the third option, but tried just in case :) )

<h:messages/>
<h:form>
  <h:inputText id="jamesName" value="#{james.name}"/>
  <h:message for="jamesName"/>
  <h:commandButton value="submit"/>
</h:form>
<h:form>
  <h:inputText id="jamesName" value="#{james.name}" immediate="true"/>
  <h:message for="jamesName"/>
  <h:commandButton value="submit"/>
</h:form>
<h:form>
  <h:inputText id="jamesName" value="#{james.name}">
    <f:validateBean />
  </h:inputText>
  <h:message for="jamesName"/>
  <h:commandButton value="submit"/>
</h:form>

I m using JSF 2.0.2-FCS with Hibernate Entity Manager 3.3.2.GA and Hibernate Validator 4.0.2.GA (below are the relevant parts from my Maven POM), running in Tomcat 6.0.20 on Windows XP Pro Service Pack 3 (32-bit).

<dependency>
  <groupId>javax.faces</groupId>
  <artifactId>jsf-api</artifactId>
  <version>2.0.2-FCS</version>
</dependency>
<dependency>
  <groupId>javax.faces</groupId>
  <artifactId>jsf-impl</artifactId>
  <version>2.0.2-FCS</version>
</dependency>
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
</dependency>
<dependency>
  <groupId>javax.servlet.jsp</groupId>
  <artifactId>jsp-api</artifactId>
  <version>2.1</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-entitymanager</artifactId>
  <version>3.3.2.GA</version>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-validator</artifactId>
  <version>4.0.2.GA</version>
</dependency>
最佳回答

Your validation is working correctly. The problem is with your @Min and @Max annotations:

@NotNull
@Min(value = 1)
@Max(value = 5)
public String getName() {
    return name;
}

@Min and @Max are meant for numeric comparison. You wrote:

The name must be entered, and have a minimum length of 1 character, and a maximum length of 5 characters

You should use @Size annotation instead, as in:

@NotNull
@Size(min = 1, max = 5)
public String getName() {
    return name;
}

The bean fields are not updated because the validation fails. Only after the validation succeeds will the bean get updated.

问题回答

As for your additional question - see this matrix - no, they are not compatible.

About the JSF question - check both tomcat logs and the firefox javascript console.

In order to raise a compilation error in this kind of case, we can use Hibernate Validator Annotation Processor. hibernate validator





相关问题
JSF a4j:support with h:selectManyCheckbox

I m having trouble with a JSF selectManyCheckbox and A4J support. The purpose is to run some action when a checkbox is selected. This works perfectly in Firefox. Yet, when testing in any IE (ie6 / ie7 ...

Mojarra for JSF Encoding

Can anyone teach me how to use mojarra to encode my JSF files. I downloaded mojarra and expected some kind of jar but what i had downloaded was a folder of files i don t know what to do with

如何拦截要求终止?

在共同基金中,如果用户要求终止,就需要采取一些行动。 我需要某种拦截器,但我不知道如何这样做。 我需要帮助。 增 编

ICEFaces inputFile getting the file content without upload

Is there any way of just getting the content of the browsed file without any upload/file transfer operations? I currently use ICEFaces inputFile component but I do not need the default uploading ...

Weird behaviour of h:commandLink action (MethodExpression)

I have two JSPs where I am displaying some info from database in a h:dataTable. One of them is showing all the info, and one of them user specifically. I have showXML.jsp that shows the "XML" column ...

How to correctly use ResultSet with h:dataTable

The problem is, that after displaying the ResultSet with <h:dataTable>, the connection is left open. If I close it, it closes the ResultSet too. I m thinking about copying the ResultSet data ...

热门标签