简体   繁体   中英

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

Here's the situation: We want to have a Search 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM