JSF:是否有一种方法可以在呼叫`setValueExpression()`?`?

发布于 2025-01-18 08:28:59 字数 1411 浏览 2 评论 0原文

我们正在开发一些JSF输入字段组件。我们想做的一件事是让组件检查bean bean bean验证注释,并自动将相同的验证应用于客户端。

setValueExpression(“ value”,“”#{bean.fieldname})value作为第一个参数,这似乎是抓住back back bean的正确时间 阅读bean验证注释

    @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'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.

Why does this happen? We've tried 100 different things and can't magically get JSF to save the component state.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文