Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Validation 在调用应用程序阶段将inputText标记为无效_Validation_Jsf 2_Primefaces_Error Handling - Fatal编程技术网

Validation 在调用应用程序阶段将inputText标记为无效

Validation 在调用应用程序阶段将inputText标记为无效,validation,jsf-2,primefaces,error-handling,Validation,Jsf 2,Primefaces,Error Handling,我在调用应用程序阶段执行一些业务规则验证,当出现错误时,将抛出一个自定义异常。自定义异常在自定义JSF ErrorHandler中处理,其中有问题的输入组件将被标记为无效,创建的FacesMessages以及FacesContext上的验证将失败 Bean public void performAction() { if ("aaa".equals(input)) { // custom exception: arg1 - Error Message, arg2 - c

我在调用应用程序阶段执行一些业务规则验证,当出现错误时,将抛出一个自定义异常。自定义异常在自定义JSF ErrorHandler中处理,其中有问题的输入组件将被标记为无效,创建的FacesMessages以及FacesContext上的验证将失败

Bean

public void performAction() {

    if ("aaa".equals(input)) {
        // custom exception: arg1 - Error Message, arg2 - clientId
        throw new ServiceValidationException("Something went wrong", ":f:input");
    }
}
XHTML

<h:form id="f">
    <p:inputText id="input" value="#{bean.input}" />
    <h:commandButton value="Submit" action="#{bean.performAction}"/>
</h:form>
问题
我遇到的问题是,在通过命令按钮提交表单时,页面重新显示,输入文本字段标记为红色(预期行为),但是输入到字段中的文本丢失。我希望输入的无效文本保留在字段中

在MarkComponentValid中,您可以尝试手动设置组件的值:

evh.setSubmittedValue("aaa");
evh.setValue("aaa");
当然,您可以向ServiceValidationClass添加一个“input”属性来代替硬编码“aaa”,这样您就可以将该值从操作方法传递到错误处理程序,然后再传递到Util类,例如

豆子:

错误处理程序:

JSFComponentUtil.markComponentAsInvalid(sve.getClientId(), sve.getInput());

等等。

这是个好主意。如果没有其他“JSF方式”来处理这个问题,我将接受这个答案。谢谢“JSF方式”是使用普通验证器执行验证。
evh.setSubmittedValue("aaa");
evh.setValue("aaa");
throw new ServiceValidationClass ("Something went wrong", ":f:input", input);
JSFComponentUtil.markComponentAsInvalid(sve.getClientId(), sve.getInput());