发生jsf验证错误时执行操作 [英] Perform action when jsf validation errors occur

查看:103
本文介绍了发生jsf验证错误时执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果发生jsf验证错误,是否可以执行一些操作(一些清理)?
使用xhtml中的标记来验证字段,例如'required ="true"','f:validateRegex pattern ="\ d *"','f:validator validateatorId ="someValidator"'.
我需要将托管bean的某些属性字段设置为null(当页面上出现任何故障时). 但是,如果验证失败,则JSF会进入渲染响应阶段",并且不会调用托管bean方法. 在这种情况下,也不会调用侦听器(使用f:actionListener标记).

现在我正在考虑通过使用@ AssertTrue,@ Size等bean注释进行验证来替换xhtml验证. 然后,在某些验证方法中,有可能进行清理:

Is it possible to perform some actions (some cleanup) if jsf validation errors occur?
Fields are validated with tags in xhtml, for example 'required="true"', 'f:validateRegex pattern="\d*"', 'f:validator validatorId="someValidator"'.
I need to set some property field of managed bean to null (when there are any failures on the page). But if validation fails, then JSF goes to Render Response Phase and managed bean method is not invoked. Listener (using f:actionListener tag) also is not invoked in that case.

Now I'm thinking about to replace xhtml validation by validation using bean annotations like @AssertTrue, @Size, etc. Then in some of this validating methods it would be possible to make a cleanup:

@ManagedBean
class SomeBean {
...
    @AssertTrue
    public void isClenup() {
        cleanup();
    }
...
}

但是对我来说似乎不是一个好的解决方案.
我还注意到用@AssertTrue注释的几种方法是按未定义的顺序调用的.因此,从xhtml验证切换到bean批注验证变得不那么容易.
是否可以定义以@AssertTrue注释的调用方法的某些顺序?

But it seems not a good solution to me.
Also I noticed that several methods annotated with @AssertTrue are called in undefined order. Therefore switching from xhtml validation to bean annotations validation is getting not so easy.
Is it possible to define some order of calling methods annotated with @AssertTrue?

推荐答案

事实上,我有一个很普通的任务:有一个具有搜索功能的页面.
如果搜索成功且没有错误,则应显示结果,但如果发生验证错误(在下一次搜索期间),则不应显示先前的结果(但可见,这是一个问题).

我的计划如下:使用facesContext.isValidationFailed()在initialize()方法中检查验证失败,如果为true,则隐藏(删除)以前的搜索结果:

In fact I have quite ordinary task: there is a page with search functionality.
On search success with no errors the result should be shown, but if validation errors occur (during next search), then previous result should not be shown (but it was visible and that was a problem).

My plan was as follows: to check for validation failures in initialize() method using facesContext.isValidationFailed() and if it is true, then to hide (delete) previous search results:

public void initialize() {
    FacesContext context = FacesContext.getCurrentInstance();
    boolean validationFailed = context.isValidationFailed();
    if(validationFailed) {
        clearPreviousSearchResult();
    }
}

但是后来我发现使用bean批注(例如@AssertTrue)进行验证不会设置facesContext.validationFailed();! IE.之后

But then I found out that validation using bean annotations (like @AssertTrue) does not set facesContext.validationFailed();! I.e. after this

@AssertTrue(message = "Some error message")
public boolean isValidateSomeField() {
    return validate(getSomeFieldValue());
}

当失败发生时,您会得到context.isValidationFailed()== false(尽管我期望是true)
(xhtml验证或验证器或f:validator确实按预期设置facesContext.validationFailed())

you get context.isValidationFailed() == false when fails occur (though I expected true)
(xhtml validation or validator or f:validator do set facesContext.validationFailed() as expected)

因此有必要手动设置上下文失败:

Therefore it is necessary to set context failed manually:

@AssertTrue(message = "Some error message")
public boolean isValidateSomeField() {
    if(!validate(getSomeFieldValue())) {
        FacesContext.getCurrentInstance().validationFailed();
        return false;
    }
    return true;
}

但是由于如果验证失败,如何在验证阶段的后阶段签入?我意识到可以更轻松地解决问题!只是用几行换行:

But due to How to check in after phase of Validations phase if validation has failed? I realized that the problem can be resolved much more easier! Just to wrap with couple of lines:

<h:panelGroup rendered="#{!facesContext.validationFailed}">
    ...block not to show if validation errors occur...
</h:panelGroup>

不需要使用注释bean验证和一些initialize()方法!

And no need to use annotations bean validation and some initialize() method!

这篇关于发生jsf验证错误时执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆