0

Mojara 2.1.21, Primefaces 3.5.11, Omnifaces 1.5 I have a text saved in Java String:

this is my text with markers {1} 
second row {2} ....

Each marker has associated value (ex. {1} => 23, {2} => 45) in a HashMap. I want to generate jsf component tree where each marker will be replaced with "p:inputText" fields with associated values and text will be replaced with "h:outputText". If user changed something in an input field, that must be reflected in a backing bean when user click "Save" button. The formatting of text should be preserved. How can I solve this problem? The output .xhtml-rendered tree (dynamicaly created with java or generated from some .xhtml code)should be:

<h:form>
   <h:outputText value="this is my text with markers " />
   <p:inputText value="{mybean.value1}" />
   <h:outputText value="newline seconde row" />
   <p:inputText value={mybean.value2} />
   ....
   <p:button value="save" actionListener="#{mybean.save()}"/>
</h:form>

How can I create this component tree ? How can I bind the values in input text to the backing bean (because the number of all values is not fixed) ?

EDIT: my idea: split text in "text before, placeholder" pairs. Iterate with c:forEach loop and generate the components.

<h:form>
   <c:forEach value="#{bean.pairs}" var="pair">
       <h:outputText value="#{pair.text}" />
       <c:if test="#{not empty pair.value}">
          <p:inputText value="#{pair.value}" />
       </c:if>
   </c:forEach>
   <p:commandButton value="save" />
</h:form>


class Pair {
   String text; 
   int placeholderNum; 
   int value;
   //Getter and setter
}

@SessionScoped @Named
class Bean {
   public List<Pair> getPairs () {  //compute pairs with a help of regex and split
       //from input text, and replace the values
   }

   public save() {
       //iterate over pairs and save the values in hashmap
   }

}
John N
  • 844
  • 4
  • 12
  • 20
  • @BalusC Sorry for my misleading question. I've updated it to be more clear and JSF-specific. – John N Sep 12 '13 at 04:54
  • JSF doesn't work that way. Use a Facelet template/tagfile or programmatically create components. See for some hints also http://stackoverflow.com/questions/3510614/how-to-create-dynamic-jsf-1-2-form-fields and http://stackoverflow.com/questions/5713718/how-to-make-a-grid-of-jsf-composite-component – BalusC Sep 12 '13 at 10:58
  • @BalusC I have written some code with a help of jstl. (see my updated question) Is it possible to generate the component tree that way ? – John N Sep 12 '13 at 21:26

0 Answers0