JSF 2 — Save All Valid Component Values

半世苍凉 提交于 2019-12-05 02:18:05

问题


I have a requirement to create a javascript function that when invoked will save all of the valid components on a JSF 2.0 form. Since the complete form will never be valid as a whole I need to figure out a way to run the lifecycle per component so that if the validation phase is successful the model will be updated and eventually saved.

Ideally, this needs to be a single ajax request as iterating over each component with a separate ajax request would be painfully inefficient.

Has anyone solved the problem before? If not could you give me some pointers on possible implementations?

Edit:

Here's what I have that seems to be working well so far:

@ManagedBean(name = "partialAppSaveBean")
@RequestScoped
public class PartialAppSaveBean implements Serializable {

    protected static final Logger LOGGER = LoggerFactory.getLogger(PartialAppSaveBean.class);
    private static final long serialVersionUID = 1L;

    /**
     * Save any valid Application values
     *
     * @param event
     */
    public void saveData(AjaxBehaviorEvent event) {
        final FacesContext context = FacesContext.getCurrentInstance();

        UIForm form = getParentForm(event.getComponent());
        Set<VisitHint> hints = EnumSet.of(VisitHint.SKIP_UNRENDERED);

        form.visitTree(VisitContext.createVisitContext(context, null, hints), new VisitCallback() {

            @Override
            public VisitResult visit(VisitContext context, UIComponent component) {
            if (component instanceof UIInput) {
                UIInput input = (UIInput) component;
                input.validate(context.getFacesContext());

                if (input.isValid() && input.getValue() != null) {
                    ValueExpression valueExpression = input.getValueExpression("value");

                    if (valueExpression != null
                            && valueExpression.getExpressionString() != null) {
                        try {
                            valueExpression.setValue(context.getFacesContext().getELContext(), input.getValue());
                        } catch (Exception ex) {
                            LOGGER.error("Expression [ " + valueExpression.getExpressionString() + 
                                    "] value could not be set with value [" + input.getValue() + "]", ex);
                        }                            
                    }
                }
            }

            return VisitResult.ACCEPT;
            }
        });

        //Save data here
    }

    /**
     * Returns the parent form for this UIComponent
     * 
     * @param component
     * @return form
     */
    private static UIForm getParentForm(UIComponent component) {
        while (component.getParent() != null) {
            if (component.getParent() instanceof UIForm) {
                return (UIForm) component.getParent();
            } else {
                return getParentForm(component.getParent());
            }
        }

        return null;
    }

}

Invoked with something like:

<h:commandButton
    id="saveData">
    <f:ajax listener="#{partialAppSaveBean.saveData}" execute="@form" immediate="true" onevent="onPartialSave" />
</h:commandButton>

回答1:


You could use UIComponent#visitTree() on the UIForm.

FacesContext context = FacesContext.getCurrentInstance();
UIForm form = getFormSomehow();
Map<String, Object> validValues = new HashMap<String, Object>();
Set<VisitHint> hints = EnumSet.of(VisitHint.SKIP_UNRENDERED);

form.visitTree(VisitContext.createVisitContext(context, null, hints), new VisitCallback() {

    @Override
    public VisitResult visit(VisitContext context, UIComponent component) {
        if (component instanceof UIInput) {
            UIInput input = (UIInput) component;

            if (input.isValid()) {
                validValues.put(input.getClientId(context.getFacesContext()), input.getValue());
            }
        }

        return VisitResult.ACCEPT;
    }
});


来源:https://stackoverflow.com/questions/10742512/jsf-2-save-all-valid-component-values

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