After validation error subsequent ajax requests get values from UI Components and not from Beans

我们两清 提交于 2019-12-04 05:48:52
BalusC

This is a known problem and explained in depth in this answer. In a nutshell, the problem is caused because the invalidated components which are to be rendered by <f:ajax render> but are not been executed by <f:ajax execute> remains in an invalidated state along with the original submitted value. When JSF renders the input component, JSF will first check if the submitted value is not null and then display it, else it will display the model value. You basically need to reset the submitted value of input components which are to be rendered, but which are not been executed by ajax.

To achieve this, you can use an ActionListener which basically does the following:

UIViewRoot viewRoot = context.getViewRoot();
PartialViewContext partialViewContext = facesContext.getPartialViewContext();
Set<EditableValueHolder> inputs = new HashSet<EditableValueHolder>();

// First find all to be rendered inputs and add them to the set.
findAndAddEditableValueHolders(partialViewContext.getRenderIds(), inputs);

// Then find all executed inputs and remove them from the set.
findAndRemoveEditableValueHolders(partialViewContext.getExecuteIds(), inputs);

// The set now contains inputs which are to be rendered, but which are not been executed. Reset them.
for (EditableValueHolder input : inputs) {
    input.resetValue();
}

This has been reported as JSF issue 1060 and a complete and reuseable solution has been implemented in the OmniFaces library as ResetInputAjaxActionListener (source code here and showcase demo here).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!