【问题标题】:Escape a primefaces/jsf page that has required fields转义包含必填字段的 primefaces/jsf 页面
【发布时间】:2013-03-05 02:17:53
【问题描述】:

我有一个 jsf 页面

<h:inputText required="true"> 属性。 当我单击我的添加按钮时,如果该字段为空白,我希望看到一个错误。

我有一个<f:message> 来显示这个。

但是我怎样才能逃避(取消)该字段为空白的页面?在我的情况下,它会不断发布错误消息,我必须在该字段中输入一些内容才能转到另一个页面。

【问题讨论】:

  • 那么,如果你想通过这个页面而不在这个字段中插入一个值,为什么需要这个组件呢?
  • 使用 p:button 取消

标签: validation jsf primefaces


【解决方案1】:

如果您的取消按钮是 POST 请求,例如<p:commandButton><h:commandButton><p:commandLink><h:commandLink>

您可以将取消按钮放在具有 <h:inputText> 的表单之外,这样表单就不会被提交

<h:form>
    <h:inputText value="#{bean.string}" required="true"/>
</h:form>
<h:form>
     <p:commandButton value="Cancel" action="anotherpage.xhtml"/>
</h:form>


如果您在没有任何操作或方法调用的情况下进行导航,那么您可以使用 GET 请求,例如&lt;h:button&gt;&lt;p:button&gt;&lt;h:link&gt;&lt;h:outputLink&gt;

示例:

 <h:form>
    <h:inputText value="#{bean.string}" required="true"/>
    <p:button value="Cancel" outcome="anotherpage.xhtml"/>
 </h:form>

【讨论】:

  • 没有所谓的“正确”答案。在给定特定场景的情况下,几乎总是有更好(即使不切实际)的答案,但从来没有“正确”的答案。任何解决问题的答案,即使是最丑陋的答案都是正确的。
【解决方案2】:

您可以在您的&lt;p:commandButton&gt; 中使用immediate="true"。然后它将跳过验证。

例如:

<p:commandButton action="#{managedBean.action}" value="Cancel" immediate="true" />

【讨论】:

  • immediate="true" 将跳过验证阶段,因此通过将其设置为 commandbutton 将起作用
  • 在命令按钮中使用immediate="true" 基本上可以跳过JSF 生命周期中的几个阶段。最好了解 JSF 生命周期是如何工作的。以下是一篇值得阅读的好文章。 ibm.com/developerworks/library/j-jsf2
【解决方案3】:

如果您想取消或离开,请不要在提交时处理表单。

即不要这样做

<h:form>
    ...
    <p:commandButton value="Cancel" action="#{bean.cancel}" />
</h:form>

只是做

<h:form>
    ...
    <p:commandButton value="Cancel" action="#{bean.cancel}" process="@this" />
</h:form>

或者,如果您只是在 cancel() 方法中返回导航结果,那就更好了

<h:form>
    ...
    <p:button value="Cancel" outcome="otherpage.xhtml" />
</h:form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-14
    • 2014-07-22
    • 2012-05-17
    • 1970-01-01
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多