1 year ago

#364252

test-img

Jonathan S. Fisher

JSF: Is there a way to save some component state during a call to `setValueExpression()`?

We're developing a few JSF Input Field Components. One thing we'd like to do is have the components examine the backing bean for Bean Validation annotations and automatically apply the same validations to the client side.

When setValueExpression("value", "#{bean.fieldName}) is invoked with the value as the first argument, that seems like the correct time to grab the backing bean and read the Bean Validation annotations.

    @Override
    public void setValueExpression(final String name, final ValueExpression binding) {
        super.setValueExpression(name, binding);
        if ("value".equals(name)) {
            try {
                final ValueReference valueReference = binding.getValueReference(getFacesContext().getELContext());
                final Object base = valueReference.getBase();
                final String propertyName = valueReference.getProperty().toString();
                final Field declaredField = ElUtils.findDeclaredField(base.getClass(), propertyName);
                if (declaredField != null) {
                    final Set<Class<?>> validationGroups = ElUtils.convertOrDefault(getValidationGroups());
                    final List<Annotation> annotations = ElUtils.getValidatorAnnotations(declaredField, validationGroups);

// we'd like to cache the result of the above reflection code if the valueExpression hasn't changed
getStateHelper().put(PropertyKeys.reflectedOnBinding, binding.getExpressionString());

                    }
                }
            }
        }
    }
 

We would really like to avoid that somewhat expensive reflection code, every single request if the valueBinding hasn't changed.

Here's the problem, getStateHelper().get(PropertyKeys.reflectedOnBinding) never returns the value after the request is complete.

How come? We've tried 100 different things and can't magically get JSF to save the component state. Any help much appreciated, thank you!

java

jsf

0 Answers

Your Answer

Accepted video resources