开发者

How to iterate through all components displayed on the screen

I have a highly dynamic single page interface where various user events trigger new fields to display on the screen. I am using jsf 1.1 and basic ajax without the ability to use partial submit. Because of these limitations I have marked all of my fields required false and immediate false. The idea is to validate ONLY when the user attempts to transition to the next page in an action method NOT when they tab off and execute an ajax event. I set immediate false because I need input to make it to the modal so I can conditionally render certain fields based on business logic.

PROBLEM: I know ahead of time a list of "possible" UIInput ids which may or may not be displayed on the screen at any given time. My goal is to iterate through all these UIInputs, check to see if they are rendered, not null, and not empty otherwise mark the UIInput in error.

This works except for fields which are not displayed on the screen. For example, I have a field "amountInput" which has an XHTML rendered property set to false so it does not show up on the screen. From a display perspective, this works, but upon invoking my action, this field is still showing its rendered property as true so I have no way to know whether I should validate it or not.

What are my options here?

    public boolean validate() {
    boolean hasValidMinimumData = true;

    final List<UIInput> componentList = getComponentList();
    if (CollectionUtils.isNotEmpty(componentList)) {
        for (UIInput curComponent : componentList) {
            final Object curComponentValue = curComponent.getValue();
            if (curComponent.isRendered() && (curComponentValue == null || isNoValue(curComponent))) {
        开发者_开发问答        setError(curComponent); // applies the error
                hasValidMinimumData = false;
            }
        }
    }

    return hasValidMinimumData;
}


You need to take care that exactly the same condition responsible for the outcome of the rendered attribute is preserved during the request of the form submit as it was during the initial display.

Easiest way is to put the bean responsible for the conditions in the session scope. If you prefer to have it in the request scope (which is a very reasonable decision), then I'd suggest to grab Tomahawk's t:saveState for this.

<t:saveState value="#{bean}" />

That said, have you ever considered Ajax4jsf? It's compatible with JSF 1.1 and offers transparent integration of Ajax with JSF.


RESOLVED! Issue was that in my XHTML I had set the rendered attribute on a table component which contained my input component. I thought that this would in turn set the rendered attribute appropriately on all of its children but apparently not.

Simply setting the conditional render attribute in my XHTML on the actual input component solved the issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜