Getting data back from dynamically generated fields in a JSF composite component
===

I have a composite component, with a backing class, which takes in an attribute which tells it what sort of data to pull from the database, display and take back in. Depending on the type of data it needs to pull, it may need to dynamically generate some additional input fields for taking in more data, but the number of fields isn't known until run time.

My thought for handling list was to have a map on the back end containing all the field values and an identifying key, which entries I would convert to a list iterate over when generating the component, and so I implemented it as follows

    @FacesComponent("lab")
    public class Lab extends UINamingContainer{
   
    ... //Known compile time properties here

    private HashMap<SecondaryLabType, String> secondaryLabValues;
    private LinkedList<Map.Entry<SecondaryLabType, String>> secondaryLabs;
    
    ... //Other methods here

    public HashMap<SecondaryLabType, String> getSecondaryLabValues(){
		if(secondaryLabValues == null){
			System.out.println("Null Secondary Values " + getClientId() );
			secondaryLabValues = new HashMap<SecondaryLabType,String>();
			if(getMyType().getSecondaryLabs() != null){
				for(SecondaryLabType sLab : getMyType().getSecondaryLabs()){
					SecondaryLabValues mslv = new SecondaryLabValues(getConn());
					secondaryLabValues.put(sLab, null);
					for(SecondaryLabValues value : mslv.getMostRecentByPatientIdAndType(getMyPatientId(), sLab, 1)){
						secondaryLabValues.put(sLab, value.getValue());
					}
				}
			}
		}
		return secondaryLabValues;
	}

	public LinkedList<Map.Entry<SecondaryLabType, String>> getSecondaryLabs(){
		if(secondaryLabs == null){
			System.out.println("Null Secondary Labs List " + getClientId() );
			secondaryLabs =  new LinkedList<Map.Entry<SecondaryLabType, String>>(getSecondaryLabValues().entrySet());
		}
		return secondaryLabs;
	}
    
    ...

    public void saveNewLabResults(){

		... //Saving other properties to the database

			for(Map.Entry<SecondaryLabType, String> entry : getSecondaryLabValues().entrySet()){

				... //Saving each dynamic field to the database
				}
		}
    }

And in the component XHTML:

    <ui:component xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui"
	xmlns:cc="http://java.sun.com/jsf/composite">

	<cc:interface componentType="lab">
		... //Attributes
	</cc:interface>
	
	<cc:implementation>
		
    ... //Various layout and control toggling code
	
		<h:form prependId="false">
			...
    <f:facet name="header">
    ...
						<span id="#{cc.clientId}_savebuttons" class="labPanel-controls" style="display:none;">
							<span id="#{cc.clientId}_loader" style="display:none;">
									<h:graphicImage name="ajax-loader.gif" library="images" height="16" width="16"/>
							</span>
							<h:commandLink action="#{cc.saveNewLabResults}">
								<f:ajax execute="newLabValue newSource newDate newAtGoal newSecondaryLabs" render="@form" onevent="#{cc.clientId}rowHighlight"/>
								<h:graphicImage name="action_save.gif" library="images"/>
							</h:commandLink>
							<span onclick="#{cc.clientId}Toggle();">
								<h:graphicImage name="page_cross.gif" library="images"/>
							</span>
						</span>
					</h:panelGroup>
				</f:facet> <!-- header -->
				<div id="#{cc.clientId}_newLab" class="labPanel-new" style="display:none;">
    
					... // Non dynamic components
    
					<h:panelGroup id="newSecondaryLabs" style="display:none;">
						<p:dataGrid columns="1" value="#{cc.secondaryLabs}" var="lab">
							#{lab.key.word}: 
							<h:selectOneMenu id="#{lab.key}_selector" value="#{lab.value}">
								<f:selectItems value="#{cc.getSecondarySelections(lab.key.dbValue)}"/>
							</h:selectOneMenu>
						</p:dataGrid>
					</h:panelGroup>
				</div>
				... // Rest of form
		</h:form>
	
	</cc:implementation>


</ui:component>

The component renders perfectly, and when the save button is clicked, it appears to be submitting the appropriate values from the select one menu items, however, once it reaches the saving code in the backing bean the values stored in the dynamic field's backing map are all empty, so I never get what the user entered on the form. 

I had a thought that this might have something to do with scoping, and so attempted to add @ViewScoped to the backing class, but that didn't appear to work. I later found a comment by BalusC in this question (http://stackoverflow.com/questions/7167170/injecting-resources-into-uicomponent-aka-does-cdi-work-here) that suggests that components behave as being view scoped anyway in JSF, though when I run the code, the backing map is null every request so that doesn't appear to be so either.

Lastly I thought then to add a view scoped managed bean and inject it into the composite component backing via @ManagedProperty, unfortunately that doesn't appear to work as the property is never injected and is always null. I'm not convinced that's a good solution anyway because on the pages these components will be used on there will be any number of the components, and my limited understanding of the view scope suggests to me that each one having an injection of that one view scoped bean would have them all trampling over the other's data.

So long story short, the question is, how can I get to the data the user enters into those dynamically generated fields?