【发布时间】:2012-11-20 19:51:03
【问题描述】:
在 JavaServer Faces 中使用值和绑定有什么区别,什么时候使用一个而不是另一个?为了更清楚我的问题是什么,这里给出了几个简单的例子。
通常在 XHTML 代码中使用 JSF,您会在此处使用“值”:
<h:form>
<h:inputText value="#{hello.inputText}"/>
<h:commandButton value="Click Me!" action="#{hello.action}"/>
<h:outputText value="#{hello.outputText}"/>
</h:form>
那么bean就是:
// Imports
@ManagedBean(name="hello")
@RequestScoped
public class Hello implements Serializable {
private String inputText;
private String outputText;
public void setInputText(String inputText) {
this.inputText = inputText;
}
public String getInputText() {
return inputText;
}
// Other getters and setters etc.
// Other methods etc.
public String action() {
// Do other things
return "success";
}
}
但是,当使用“绑定”时,XHTML 代码是:
<h:form>
<h:inputText binding="#{backing_hello.inputText}"/>
<h:commandButton value="Click Me!" action="#{backing_hello.action}"/>
<h:outputText value="Hello!" binding="#{backing_hello.outputText}"/>
</h:form>
对应的ibg bean称为backing bean,在这里:
// Imports
@ManagedBean(name="backing_hello")
@RequestScoped
public class Hello implements Serializable {
private HtmlInputText inputText;
private HtmlOutputText outputText;
public void setInputText(HtmlInputText inputText) {
this.inputText = inputText;
}
public HtmlInputText getInputText() {
return inputText;
}
// Other getters and setters etc.
// Other methods etc.
public String action() {
// Do other things
return "success";
}
}
这两个系统之间有什么实际区别,您什么时候会使用 backing bean 而不是常规 bean?可以同时使用吗?
我对此感到困惑有一段时间了,如果能解决这个问题,我将不胜感激。
【问题讨论】:
标签: jsf