简体   繁体   中英

Spring + JSF2 + JSR 303 bean validation

I'm using Spring 3.2.2 with Primefaces 4.0 and Hibernate 4.2. I have JSR 303 validation annotations on my entities, and I have spring configured to validate them at the service layer - that much works fine.

But I'd like JSF validation to kick in before calling the service but it doesn't. All the research I've done says I just have to add a validator to the classpath and JSF2 will automatically apply.

I've added the suggested jars to my classpath:

[INFO] |     +- com.sun.faces:jsf-api:jar:2.2.4:compile
[INFO] |     +- com.sun.faces:jsf-impl:jar:2.2.4:compile
[INFO] |     +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] |     +- org.hibernate:hibernate-validator:jar:4.3.1.Final:compile
[INFO] |     +- org.primefaces.extensions:primefaces-extensions:jar:1.0.0:compile
[INFO] |     |  +- org.primefaces:primefaces:jar:4.0:compile
[INFO] |     +- org.slf4j:slf4j-api:jar:1.7.5:compile
[INFO] |     +- org.slf4j:slf4j-log4j12:jar:1.7.5:compile
[INFO] |     |  \- log4j:log4j:jar:1.2.17:compile
[INFO] |     \- org.jboss.logging:jboss-logging:jar:3.1.3.GA:compile

Full dependency tree can be seen at https://gist.github.com/prule/7411171

My validated object and managed bean:

public class ValidatedObject {
    @NotEmpty
    String name;

    public String getName() {
        return name;
    }

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

@ManagedBean
@RequestScoped
public class ValidatedController {
    private ValidatedObject object = new ValidatedObject();

    public ValidatedObject getObject() {
        return object;
    }


    public void onSave() {
        System.out.println("saving");
    }
}

Facelets page:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui">

<f:view contentType="text/html">

    <h:body>
        <h:form >
            <p:messages />
            <p:outputLabel for="name" value="Name"/>
            <p:inputText id="name" value="#{validatedController.object.name}"/>
            <p:commandButton value="Save" action="#{validatedController.onSave}" ajax="false"/>
        </h:form>
    </h:body>

</f:view>
</html>

Spring config contains:

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
<bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor">
    <property name="validator" ref="validator"/>
</bean>

All the questions and answers I've found are basic JSF2 (without spring) or spring-web based. Have I got something missing or incorrect with my spring/jsf integration?

Thanks.

AFAIK, Spring is not compatible (yet) with JEE7/JSF2.2 Bean validation. Either wait for a compatible set or use a JEE7 compatible DI framework like JBoss WELD that is shipped by default with GF4.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM