bean validation (JSR 303) for primefaces not working

十年热恋 提交于 2021-02-08 06:52:39

问题


I'm using:

  • Primefaces 6.1
  • JSF: 2.2.10
  • javax.validation:1.1.0.Final
  • validator impl: hibernate-validator 5.0.1.Final
  • GAE: 1.9.52

I follow the example for CSV (Client Side Validation) using backend bean from: https://www.primefaces.org/showcase/ui/csv/bean.xhtml

The expected result should be:

And what I get right now:

The bean validation is not working.

I have below configure in web.xml

<context-param> 
    <param-name>primefaces.CLIENT_SIDE_VALIDATION</param-name> 
    <param-value>true</param-value> 
</context-param>

Few similar posts said need downgrade jsf to 2.2.2, I tried but still not working.

Right now the workaround for CSV is either

  • using jsf tag validation based on the demo

https://www.primefaces.org/showcase/ui/csv/basic.xhtml

For example:

<p:inputText id="age" value="#{beanValidationView.age}" label="Age">
    <f:validateLongRange for="age" minimum="10" maximum="20" />
</p:inputText>
  • Or create my own validator

for example: http://www.supermanhamuerto.com/doku.php?id=java:validatorinprimefaces

BTW, I don't think it is related to GAE. Because I tried with a new Dynamic Web Project using Tomcat 9, it give me the same result as shown in below screen capture.

Is that any thing(s) I miss configured or having diff version of jar causing that problem?


回答1:


I got the same error. I fixed it by upgrading hibernate-validator from: 5.1.3.Final to: 5.3.5.Final

I kept Primefaces 6.1.




回答2:


By placing dependencies (i.e slf4j-jdk14, slf4j-api and jboss-el), Hibernate Validator work on Tomcat 9 but not GAE. After configured the log level to FINER , logger show below entriies:

May 04, 2017 9:10:08 AM com.sun.faces.config.processor.ApplicationConfigProcessor addSystemEventListener
FINE: Subscribing for event javax.faces.event.PostConstructApplicationEvent and source javax.faces.application.Application using listener org.primefaces.extensions.application.PostConstructApplicationEventListener
May 04, 2017 9:10:08 AM com.sun.faces.config.processor.ApplicationConfigProcessor isBeanValidatorAvailable
FINE: java.lang.NoClassDefFoundError: javax.naming.InitialContext is a restricted class. Please see the Google  App Engine developer's guide for more details.
java.lang.NoClassDefFoundError: javax.naming.InitialContext is a restricted class. Please see the Google  App Engine developer's guide for more details.
    at com.google.appengine.tools.development.agent.runtime.Runtime.reject(Runtime.java:50)
    at com.sun.faces.config.processor.ApplicationConfigProcessor.isBeanValidatorAvailable(ApplicationConfigProcessor.java:434)
    at com.sun.faces.config.processor.ApplicationConfigProcessor.registerDefaultValidatorIds(ApplicationConfigProcessor.java:396)
    at com.sun.faces.config.processor.ApplicationConfigProcessor.process(ApplicationConfigProcessor.java:353)
    at com.sun.faces.config.processor.AbstractConfigProcessor.invokeNext(AbstractConfigProcessor.java:152)
    at com.sun.faces.config.processor.LifecycleConfigProcessor.process(LifecycleConfigProcessor.java:137)

That is a "NoClassDefFoundError", however log in FINE level instead of Warning and return more meaningful message. That bad.

So I make a small change to the isBeanValidatorAvailable() as below to make it work on GAE

static boolean isBeanValidatorAvailable() {

    boolean result = false;
    final String beansValidationAvailabilityCacheKey = 
            "javax.faces.BEANS_VALIDATION_AVAILABLE";
    Map<String,Object> appMap = FacesContext.getCurrentInstance().getExternalContext().getApplicationMap();

    if (appMap.containsKey(beansValidationAvailabilityCacheKey)) {
        result = (Boolean) appMap.get(beansValidationAvailabilityCacheKey);
    } else {
        try {       
            // Code for Google App Engine 
            ValidatorFactory validatorFactory = null;
            try{                
                Object cachedObject=FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().get(BeanValidator.VALIDATOR_FACTORY_KEY);
                if (cachedObject instanceof ValidatorFactory) {
                    validatorFactory=(ValidatorFactory)cachedObject;
                } else {
                    validatorFactory=Validation.buildDefaultValidatorFactory();
                }       
            }catch(ValidationException e) {
                LOGGER.log(Level.WARNING, "Could not build a default Bean Validator factory",e);
            }               

            if (null != validatorFactory) {
                appMap.put(BeanValidator.VALIDATOR_FACTORY_KEY, validatorFactory);
                result = true;
            }             
            LOGGER.log(Level.FINE, "result=" +result +", validatorFactory=" +validatorFactory);

         /* incompatible with Google App Engine
          *
            Thread.currentThread().getContextClassLoader().loadClass("javax.validation.MessageInterpolator");
            // Check if the Implementation is available.
            Object cachedObject = appMap.get(BeanValidator.VALIDATOR_FACTORY_KEY);
            if(cachedObject instanceof ValidatorFactory) {
                result = true;
            } else {
                Context initialContext = null;
                try {
                    initialContext = new InitialContext();
                } catch (NoClassDefFoundError nde) {
                    // on google app engine InitialContext is forbidden to use and GAE throws NoClassDefFoundError 
                    if (LOGGER.isLoggable(Level.FINE)) {
                        LOGGER.log(Level.FINE, nde.toString(), nde);
                    }
                } catch (NamingException ne) {
                    if (LOGGER.isLoggable(Level.WARNING)) {
                        LOGGER.log(Level.WARNING, ne.toString(), ne);
                    }
                }

                try {
                    Object validatorFactory = initialContext.lookup("java:comp/ValidatorFactory");
                    if (null != validatorFactory) {
                        appMap.put(BeanValidator.VALIDATOR_FACTORY_KEY, validatorFactory);
                        result = true;
                    }
                } catch (NamingException root) {
                    if (LOGGER.isLoggable(Level.FINE)) {
                        String msg = "Could not build a default Bean Validator factory: " 
                                + root.getMessage();
                        LOGGER.fine(msg);                       
                    }
                }

                if (!result) {
                    try {
                        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
                        Validator validator = factory.getValidator();
                        appMap.put(BeanValidator.VALIDATOR_FACTORY_KEY, factory);
                        result = true;
                    } catch(Throwable throwable) {
                    }
                }
            }
          */
        } catch (Throwable t) { // CNFE or ValidationException or any other
            if (LOGGER.isLoggable(Level.FINE)) {
                LOGGER.fine("Unable to load Beans Validation");
            }
        }
        appMap.put(beansValidationAvailabilityCacheKey, result);
    }
    return result;
}

After all this JSR 303 (Bean Validation) problem is related to GAE restriction on JSF2.

A working copy can get from Google Drive.



来源:https://stackoverflow.com/questions/43740371/bean-validation-jsr-303-for-primefaces-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!