价值与约束力之间的区别 [英] Difference between value and binding

查看:95
本文介绍了价值与约束力之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaServer Faces中使用值和绑定之间有什么区别,什么时候使用一个相对于另一个相对?为了更清楚地说明我的问题,这里给出了两个简单的示例.

What is the difference between using value and binding with JavaServer Faces, and when would you use one as opposed to the other? To make it clearer what my question is, a couple of simple examples are given here.

通常在XHTML代码中使用JSF,您将在此处使用值":

Normally with JSF in the XHTML code you would use "value" as here:

<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代码是:

However, when using "binding", the XHTML code is:

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

和对应的Bean称为支持Bean,位于此处:

and the correspondibg bean is called a backing bean, and is here:

// 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";
}
}

这两个系统之间有什么实际区别,何时使用后备bean而不是常规bean?可以同时使用两者吗?

What practical differences are there between the two systems, and when would you use a backing bean rather than a regular bean? Is it possible to use both?

一段时间以来,我对此一直感到困惑,最希望能解决这个问题.

I have been confused about this for some time, and would most appreciate having this cleared up.

推荐答案

value属性表示组件的.这是在浏览器中打开页面时在文本框中看到的文本.

value attribute represents the value of the component. It is the text that you see inside your text box when you open the page in browser.

binding属性用于将您的组件绑定到bean属性.例如,在您的代码中,您的inputText组件就是这样绑定到bean的.

binding attribute is used to bind your component to a bean property. For an example in your code your inputText component is bound to the bean like this.

#{backing_hello.inputText}`

这意味着您可以在代码中作为UIComponent对象访问整个组件及其所有属性.您可以对该组件做很多工作,因为现在您的Java代码中可以使用它了. 例如,您可以像这样更改其样式.

It means that you can access the whole component and all its properties in your code as a UIComponent object. You can do lot of works with the component because now it is available in your java code. For an example you can change its style like this.

public HtmlInputText getInputText() {
    inputText.setStyle("color:red");
    return inputText;
}

或者只是根据bean属性禁用该组件

Or simply to disable the component according to a bean property

if(someBoolean) {
  inputText.setDisabled(true);
}

以此类推....

这篇关于价值与约束力之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆