开发者

Is it possible to generate form fields in a webapp from a list of generic objects?

开发者 https://www.devze.com 2023-01-19 23:54 出处:网络
Here\'s the situation: We want to have a Sea开发者_JS百科rch page that takes in an ordered list of Attribute objects, and based on their \'type\' (text input, dropdown, checkbox) generates and display

Here's the situation: We want to have a Sea开发者_JS百科rch page that takes in an ordered list of Attribute objects, and based on their 'type' (text input, dropdown, checkbox) generates and displays it in the appropriate manner. We'd also need to process the values for these fields in order to filter results. I'm at a loss for how we can accomplish this, any ideas/solutions? This is for a java webapp backed by struts2.


Yes, it's possible. I'm not familiar with struts, but I guess it can't be that hard. Some pseudo-java-code to get you started:

private void init() {
    for(Attribute a : attributes) {
        SomeWebComponent c = createComponent(a);
        components.put(a, c);
    }
    renderComponents(components.values());
}

private SomeWebComponent createComponent(Attribute a) {
    if(a.getType().equals("text") return createTextInput();
    else if(a.getType().equals("list") return createListInput(a.getItems());
    ...
}

private void performSearch() {
    for(Attribute a : attributes) {
        SomeWebComponent c = components.get(a);;
        searchValues.put(a, c.getValue());
    }
    doSearch(searchValues);
}
0

精彩评论

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