Mojara 2.1.21、Primefaces 3.5.11、Omnifaces 1.5 Java 文字列に保存されたテキストがあります。
this is my text with markers {1}
second row {2} ....
各マーカーには、HashMap 内の関連付けられた値 (例: {1} => 23、{2} => 45) があります。各マーカーが関連付けられた値を持つ「p:inputText」フィールドに置き換えられ、テキストが「h:outputText」に置き換えられる jsf コンポーネント ツリーを生成したいと考えています。ユーザーが入力フィールドで何かを変更した場合、ユーザーが「保存」ボタンをクリックしたときに、それがバッキング Bean に反映される必要があります。テキストの書式は保持する必要があります。どうすればこの問題を解決できますか? 出力 .xhtml レンダリング ツリー (Java で動的に作成されるか、何らかの .xhtml コードから生成される) は次のようになります。
<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>
このコンポーネント ツリーを作成するにはどうすればよいですか? 入力テキストの値をバッキング Bean にバインドするにはどうすればよいですか (すべての値の数が固定されていないため)。
編集:私の考え:テキストを「前のテキスト、プレースホルダー」のペアに分割します。c:forEach ループで繰り返し、コンポーネントを生成します。
<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
}
}