JSF composite component - weird behavior when trying to save state

前端 未结 1 1709
孤街浪徒
孤街浪徒 2020-12-20 05:12

I\'m using Glassfish 3.2.2 and JSF 2.1.11

I\'m trying to create a composite component that will take as parameters a string and a max number of characters and then w

1条回答
  •  生来不讨喜
    2020-12-20 06:05

    Basically, you should be overridding UIComponent#saveState() and restoreState() according their documentation when you add custom properties/attributes to the component which should survive across multiple HTTP requests on the same view. The component instance is namely recreated on every request.

    Since JSF 2.0, much better is to use the StateHelper directly in the attribute getters/setters. It's available by the inherited UIComponent#getStateHelper() method.

    private enum PropertyKeys {
        expanded;
    }
    
    public void toggleExpanded() {
        setExpanded(!isExpanded());
    }
    
    public void setExpanded(boolean expanded) {
        getStateHelper().put(PropertyKeys.expanded, expanded);
    }
    
    public boolean isExpanded() {
        return (boolean) getStateHelper().eval(PropertyKeys.expanded, false);
    }
    

    0 讨论(0)
提交回复
热议问题