简体   繁体   English

Primefaces / jsf中的条件更新

[英]Conditional update in Primefaces/jsf

First of all, so that there are no misunderstandings, while this is the same question as PrimeFaces: conditional update on validation I am asking it again, because the answer to that question is not acceptable to me , because while it may have given the original poster a workaround for what they were asking, it doesn't really answer the question, and this workaround doesn't work for me. 首先,所以没有误解,虽然这是与PrimeFaces相同的问题:验证的条件更新我再次问它,因为这个问题答案对我来说是不可接受的 ,因为虽然它可能已经给了原件海报是他们所要求的解决方法,它并没有真正回答这个问题,而且这种解决方法对我不起作用。

So, here's my question: how do I conditionally update a primefaces component based on the results of the submition? 所以,这是我的问题:我如何根据submition的结果有条件地更新primefaces组件?

I have a component that must not, under any circumstances be updated UNLESS the validation is successful and backend code was executed successfully. 我有一个组件,在任何情况下都不能更新,除非验证成功并且后端代码已成功执行。 (ie if the validation succeeds but there was an SQL exception on the back end, the component still shouldn't be updated). (即,如果验证成功但后端存在SQL异常,则组件仍不应更新)。

Why can't I always update it? 为什么我不能一直更新它? If the logic doesn't succeed it'll be updated to the same thing it was before the submit button was clicked. 如果逻辑没有成功,它将被更新为单击提交按钮之前的相同内容。 Because I can't. 因为我不能。 It's the captcha component. 这是验证码组件。 If you update it via ajax, it disappears, end of story. 如果你通过ajax更新它,它就会消失,故事结束。 There was a ticket opened at Primefaces and they closed it as won't fix, because flash compoents aren't supposed to be updated w/ ajax. 在Primefaces打开了一张票,他们关闭了它,因为无法解决,因为闪存组件不应该用ajax更新。

So when I submit the form, unless the logic succeeds I need to leave the captcha alone. 因此,当我提交表单时,除非逻辑成功,否则我需要单独保留验证码。 IF the logic succeeds, I need to make sure the captcha disappears off screen. 如果逻辑成功,我需要确保验证码在屏幕外消失。 What's the easiest, cleanest way to do it? 什么是最简单,最干净的方法?

Excuse me if this is a n00b question. 如果这是一个n00b问题,请原谅。 I'm really new to Primefaces and JSF. 我是Primefaces和JSF的新手。

RequestContext.getCurrentInstance().update("clientId")

Helps for conditional updates. 有助于条件更新。

For recaptcha try 重新尝试

RequestContext.getCurrentInstance().execute("Recaptcha.destroy()");

You can use a combination of JavaScript and RemoteCommand: 您可以结合使用JavaScript和RemoteCommand:

<p:commandLink action="#{bean.doIt()}" process="@form" update="@none" oncomplete="if(!args.validationFailed){updateMyImportantUI();}" />

<p:remoteCommand name="updateMyImportantUI" process="@this" update=":myImportantID" />     

You can use a variable at the backing bean and use it on the xhtml part. 您可以在辅助bean中使用变量并在xhtml部分上使用它。
Like this: 像这样:

update="#{backingMB.updateString}"

So, after the condition you can put the value for updateString , but when you define it on the backingMB , you need to put it as: 因此,在条件之后您可以为updateString赋值,但是当您在backingMB上定义它时,您需要将其设置为:

String updateString=""; // this will be id of the component to be updated  

This way you won't get a NullPointerException . 这样你就不会得到NullPointerException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM