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
}
}