I have a screen with 2 JSF Input Components
: inputText
and inputText with suggestionBox
. They are both bound to the s开发者_如何学运维ame field, but only one can be visible/rendered (mutual exclusion).
How can I have 2 mutually exclusive input components referring the same value working as I want ?
The key is to use the rendered attribute to show/hide the components so that only one or the other is actually updating the model at a time. Here is a very basic example to illustrate:
<h:form id="exampleForm" prependId="false">
<h:inputText id="test1" value="#{exampleBean.testString}" rendered="#{exampleBean.toggle}" style="border: 1px solid red;" />
<h:inputText id="test2" value="#{exampleBean.testString}" rendered="#{!exampleBean.toggle}" style="border: 1px solid blue;" />
<h:commandButton id="testButton" action="#{exampleBean.toggle()}" />
</h:form>
and the example bean with shared property testString
:
@ManagedBean(name = "exampleBean")
@ViewScoped
public class ExampleBean {
private String testString;
public String getTestString() { return testString; }
public void setTestString(String testString) {
this.testString = testString;
System.out.println(testString);
}
private boolean toggle;
public boolean isToggle() { return toggle; }
public void setToggle(boolean toggle) { this.toggle = toggle; }
public void toggle() {
toggle = (toggle) ? false : true;
}
}
As I stated I can't use rendered, so in this case using readonly true
with visible false
gives me the behavior I need. Thanks.
精彩评论