Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6076901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:39:37+00:00 2026-05-23T10:39:37+00:00

I tried to ask a more specific question but I think i may have

  • 0

I tried to ask a more specific question but I think i may have been overly specific so I’ll zoom out to get a better advice.
I’m trying to create a composite/custom component that will accept two attributes

  1. A list of strings –> names of fields
  2. a list of lists of <String, Value> –> groups of fields with <fieldName, fieldValue>

example values:

  1. [Street, Country, State, ZipCode]
  2. [{(Street, Greenfield), (Country, USA)}, {(Country, Canada), (ZipCode, 3333)}]

The component will render this, based on the attributes:

enter image description here

The reason I am having difficulties with this component is that I don’t know what’s the proper way to maintain placeholders for fields that were not entered originally but can be added by the user through the component.

In the above example, for the first set, these would be State and ZipCode.

My Idea was to create a dummy object with all the fields and on submittion copy the dummy object’s values to the data structure passed in the attributes.
The problem I was facing was not knowing how to read the values on component creation and altering the List passed through the attribute on submission.

I will add sample code soon (Although that shouldn’t be a crucial for answering this question)

Thank you just for reading this far!! 🙂

My Code (Again, not required for answering the question but may be helpful to understand my challenge)

The Composite Component Code:

<cc:interface componentType="dynamicFieldList">
    <cc:attribute name="styleClass" default="fieldType" />
    <cc:attribute name="form" default="@form"
        shortDescription="If used, this is the name of the form that will get executed" />
    <cc:attribute name="list" type="java.util.List" required="true"
        shortDescription="The values of the list. The type must be List of FieldGroup" />
    <cc:attribute name="groupTypes" type="java.util.List" required="true"
        shortDescription="The types that will be available to choose from for each field. The type of this must be List of FieldType" />
</cc:interface>

<cc:implementation>
    <h:dataTable id="table" value="#{cc.model}" var="fieldGroup">
        <h:column>
            <ui:repeat var="field" value="#{fieldGroup.values}">
                <utils:fieldType value="#{field.value}" type="#{field.type}"/>
            </ui:repeat>
        </h:column>
        <h:column>
            <h:commandButton value="delete" action="#{cc.remove}">
                <f:ajax render="#{cc.attrs.form}" execute="#{cc.attrs.form}" />
            </h:commandButton>
        </h:column>
    </h:dataTable>
    <h:commandButton value="add" action="#{cc.add}">
        <f:ajax render="#{cc.attrs.form}" execute="#{cc.attrs.form}" />
    </h:commandButton>
</cc:implementation>

The component bean java code:

@FacesComponent(value = "dynamicFieldGroupList")
// To be specified in componentType attribute.
@SuppressWarnings({ "rawtypes", "unchecked" })
// We don't care about the actual model item type anyway.
public class DynamicFieldGroupList extends UIComponentBase implements NamingContainer
{
    private transient DataModel model;
    private List<FieldGroup> displayList;

    public DynamicFieldGroupList()
    {
        super();
        List<FieldGroup> list = new ArrayList<FieldGroup>();

        for (FieldGroup group : getList()){
            FieldGroup currGroup = new FieldGroup("Untitled");

            //Assumption - Each type may exist only once in a group.
            Map<FieldType, FieldDetail> hash = new HashMap<FieldType, FieldDetail>();

            for (FieldDetail detail: group.getDetails()){
                hash.put(detail.getType(), detail); 
            }

            // While creating the dummy object, insert values the exist or placeholders if they don't.
            for (FieldType type : getGroupTypes()){
                if (hash.containsKey(type)){
                    currGroup.addDetail(hash.get(type));
                } else {
                    currGroup.addDetail(new FieldDetail(type,null));
                }
            }

            list.add(currGroup);
        }

        // Assign the created list to be the displayed (dummy) object
        setDisplayList(list);
    }

    public void add()
    {
        // Add a new group of placeholders
        FieldGroup group = new FieldGroup("Untitled");

        for (FieldType type: getGroupTypes()){
            group.addDetail(new FieldDetail(type, null));
        }

        getList().add(group);
    }

    public void remove()
    {
        getDisplayList().remove(model.getRowData());
    }

    @Override
    public String getFamily()
    {
        return "javax.faces.NamingContainer"; // Important! Required for
                                                // composite components.
    }

    public DataModel getModel()
    {
        if (model == null)
            model = new ListDataModel(getDisplayList());
        return model;
    }

    private List<FieldGroup> getList()
    { // Don't make this method public! Ends otherwise in an infinite loop
        // calling itself everytime.
        return (List) getAttributes().get("list");
    }

    private List<FieldType> getGroupTypes()
    { // Don't make this method public! Ends otherwise in an infinite loop
        // calling itself everytime.
        return (List) getAttributes().get("groupTypes");
    }

    public void setDisplayList(List<FieldGroup> displayList)
    {
        this.displayList = displayList;
    }

    public List<FieldGroup> getDisplayList()
    {
        return displayList;
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:39:38+00:00Added an answer on May 23, 2026 at 10:39 am

    You can use a Map to hold the values for each group.

    Map<String, Object> values = new HashMap<String, Object>();
    

    Assuming that you’ve all those maps in a List<Map<String, Object>> and the field names in a List<String>, then you can get/set them all basically as follows

    <ui:repeat value="#{allValues}" var="values">
        <ui:repeat value="#{fieldNames}" var="fieldName">
            <h:outputLabel value="#{fieldName}" />
            <h:inputText value="#{values[fieldName]}" />
            <br/>
        </ui:repeat>
    </ui:repeat>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to ask this question before but I guess I didnt explain myself
Well, I tried to ask this question as a comment on this question, but
Sorry to ask this question again but I tried several solutions on stackoverflow and
I tried to ask this question earlier, but I was unclear in my question.
I have tried to ask a variant of this question before. I got some
This may be a simple question but I will throw it out anyway. My
I know this might be more appropriate at Ask Different, but as I tried
I want to ask,i tried to call webapp.RequestHandler ,but this handler didn't called: this
Hey, guys, I tried to ask this yesterday but I didn't explain myself clearly
I have an button just as have Ask Question on SO and here is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.