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
JSF2.0:如何跳过JSR-303bean验证?_Jsf_Jsf 2_Facelets_Bean Validation - Fatal编程技术网

JSF2.0:如何跳过JSR-303bean验证?

JSF2.0:如何跳过JSR-303bean验证?,jsf,jsf-2,facelets,bean-validation,Jsf,Jsf 2,Facelets,Bean Validation,单击按钮时,如何使用JSF跳过JSR-303bean验证 一个有点长的问题来解释一些方法。。。考虑一个表格中的列表: <h:form id="form"> <h:commandButton value="Add row"> <f:ajax execute="foo" listener="#{bean.add()}" render="foo" /> </h:commandButton> <h:dataTa

单击按钮时,如何使用JSF跳过JSR-303bean验证

一个有点长的问题来解释一些方法。。。考虑一个表格中的列表:

<h:form id="form">
    <h:commandButton value="Add row">
        <f:ajax execute="foo" listener="#{bean.add()}" render="foo" />
    </h:commandButton>
    <h:dataTable id="foo" var="foo" value="#{bean.foos}">
        <h:column>
            Name: <h:inputText id="field" value="#{foo.name}" required="true" />
            <h:messages for="field" />
        </h:column>
        <h:column>
            <h:commandButton value="Remove">
                <f:ajax execute=":form:foo" listener="#{bean.remove(foo)}" render=":form:foo" />
            </h:commandButton>
        </h:column>
    </h:dataTable>
</h:form>
然后设置Save按钮(实际上应该尝试保存表单)以发送该参数:

<h:commandButton>
    <f:param name="SHOW_VALIDATION" value="true" />
</h:commandButton>
和保存按钮:

<h:commandButton>
    <f:param name="VALIDATE" value="true" />
</h:commandButton>

这将导致字段仅在存在
验证
参数(=按下保存按钮时)时验证


但这些似乎有点像黑客。如何简单地使用JSR-303 Bean验证,但在声明时跳过它?

将事件处理程序设置为
immediate=true
,并在退出它们之前调用
FacesContext.renderResponse()

更新

示例表单中的修改:

<h:form id="form">
    <h:commandButton value="Add row">
        <!-- Added immediate="true" to call bean.add() before validation phase -->
        <f:ajax execute="foo" listener="#{bean.add()}" render="foo" immediate="true"/>
    </h:commandButton>
    <h:dataTable id="foo" var="foo" value="#{bean.foos}">
        <h:column>
            Name: <h:inputText id="field" value="#{foo.name}" required="true" />
            <h:messages for="field" />
        </h:column>
        <h:column>
            <h:commandButton value="Remove">
                <!-- Added immediate="true" to call bean.remove() before validation phase -->
                <f:ajax execute=":form:foo" listener="#{bean.remove(foo)}" render=":form:foo" immediate="true"/>
            </h:commandButton>
        </h:column>
    </h:dataTable>
</h:form>

您可以使用标记f:validateBean禁用bean验证,该标记具有一个已禁用的属性

例如:

<h:inputText value="#{bean.name}">
   <f:validateBean disabled="#{anotherBean.flag}"/>
</h:inputText>

我们刚刚发布了这个问题的解决方案。 详情如下:

简而言之,它利用validateBean标记的binding属性。 这样做使您能够截获验证方法,并根据单击的按钮决定是否让其通过。

情况: 在页面代码中有一个包含大量输入字段的大表单。在单独的bean中有几个按钮,每个按钮都指向单独的操作

一个对我有效的解决方案

  • 在页面代码中:向按钮添加
    immediate=“true”
    ,并为要在bean中使用的输入字段提供id

  • 你在实践中是怎么做到的?你能举个例子吗?为了清楚起见:这样做会使JSF跳过第3-5阶段(流程验证、更新模型、调用应用程序),直接进入第6阶段(呈现响应)。这没关系,因为结果类似于验证在第3阶段失败,JSF将跳到第6阶段。错了就纠正我。下一步是对其进行泛化/组件化,这样就不需要调用
    FacesContext.getCurrentInstance().renderResponse()
    。选择作为公认的答案,感谢您提供的优秀解决方案!:)我试过上面的方法,总的来说效果很好。我的情况如下-我使用命令按钮向页面动态添加许多文本框。如果我使用
    immediate=“false”
    ,页面验证将阻止更新,并且也不会显示。如果我使用
    immediate=“true”
    ,并在执行AJAX请求之前修改一些输入值,则不会提交修改。有没有办法既读取回调中更改的输入数据,又不同时触发验证?提前谢谢我也有同样的问题。我希望更新模型,但跳过验证。我有一个带有不同标签的表单。我在选择不同的复选框时添加选项卡。Ajax呈现p:tabView。但是我不想丢失选项卡中已经输入或更改的信息。您可以在
    inputText
    上设置
    immediate=“true”
    ,它应该在调用任何操作之前更改值。除了JSR-303和带JSF的facelets之外,我还没有尝试过任何其他验证解决方案。我认为这个问题与JSF有关,而不是JSF-303的错——就像他们忘记了“本机”支持跳过JSF中的验证一样。关于这一点似乎有很多困惑……这也是一个很好的信息。但是,为表单中的每个字段设置此选项仍然有点麻烦…您也可以使用
    f:validateBean
    包装多个输入组件,如下所示:
    <h:form id="form">
        <h:commandButton value="Add row">
            <!-- Added immediate="true" to call bean.add() before validation phase -->
            <f:ajax execute="foo" listener="#{bean.add()}" render="foo" immediate="true"/>
        </h:commandButton>
        <h:dataTable id="foo" var="foo" value="#{bean.foos}">
            <h:column>
                Name: <h:inputText id="field" value="#{foo.name}" required="true" />
                <h:messages for="field" />
            </h:column>
            <h:column>
                <h:commandButton value="Remove">
                    <!-- Added immediate="true" to call bean.remove() before validation phase -->
                    <f:ajax execute=":form:foo" listener="#{bean.remove(foo)}" render=":form:foo" immediate="true"/>
                </h:commandButton>
            </h:column>
        </h:dataTable>
    </h:form>
    
    ...
    public void add() {
        // Your add() code
        ...
        // Added FacesContext.renderResponse() call to skip to render response phase
        FacesContext.getCurrentInstance().renderResponse();
    }
    ...
    public void remove() {
        // Your remove() code
        ...
        // Added FacesContext.renderResponse() call to skip to render response phase
        FacesContext.getCurrentInstance().renderResponse();
    }
    ...
    
    <h:inputText value="#{bean.name}">
       <f:validateBean disabled="#{anotherBean.flag}"/>
    </h:inputText>
    
     <p:inputText id="someHtmlInputId"
                   maxlength="30" 
                   value="#{beanName.attr}" />
    
    
     <p:commandButton id="someHtmlButtonId"
                       actionListener="#{beanName.doStuff}" 
                       value="Button Label"
                       ajax="false"
                       immediate="true"/>
    
    Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
    if (params != null) {
      for (String k: params.keySet())
          if (k.indexOf("someHtmlInputId") != -1)
              params.get(k); // This is Your input parameter value waiting to be processed ;)
    }