Initialize a composite component based on the provided attributes

后端 未结 2 1282
滥情空心
滥情空心 2020-12-05 12:00

I\'m writing my custom table composite component with Mojarra JSF. I\'m also trying to bind that composite to a backing component. The aim is to be able to specify the numbe

2条回答
  •  广开言路
    2020-12-05 12:33

    A simpler solution would be to store and retrieve values as part of the components state. Storing can happen during encodeBegin, and retrieving could directly happen within the getter:

    @FacesComponent("components.myTable")
    public class TestTable extends UINamingContainer {
        public void action() {
            System.out.println("Called");
        }
    
        @Override
        public void encodeBegin(FacesContext context) throws IOException {
            // Initialize the list according to the element number
            List values = new ArrayList<>();
            Integer num = (Integer) getAttributes().get("itemNumber");
            for (int i = 0; i < num; i++) {
                values.add("item" + i);
            }
            getStateHelper().put("values",values);
            super.encodeBegin(context);
        }
    
        public List getValues() {
            return (List)getStateHelper().get("values");
        }
    }
    

    To avoid repeating the logic in getValues(), there could be additional parsing required in more complex cases, there should be a way to process and cache the attributes right after they become available, although I am not sure when and how at this point.

    Either way - this seemed to be the simplest way to solve this problem.

提交回复
热议问题