2012-04-14 101 views
0

我在JSF中有一个inputText对象,让我们说inputText_A,并将该值绑定到会话Bean对象的成员变量。这是一种双重的。如何将会话bean中的值传递给JSF inputText?

<h:inputText value="#{theBean.theMemberVar}" /> 

而且这个inputText_A已经初始化为0.0。当Bean执行计算时,该值将更新回Bean.theMemberVar。我已在调试控制台中追踪它,并且该值已更新为我的预期值。但屏幕上的inputText_A仍显示原始值,即0.0。

我已经使用outputText进行了测试,我的预期输出显示在那里,但是之后它变成只读在屏幕上。我希望它是可编辑的,因为我的预期输出已经填充到inputText_A中,因此我选择了inputText对象。

我知道,当我们将一些JSF值传给Bean时,我们使用inputText,并且当某些值从Bean传递给JSF时,我们使用outputText。但是现在我想使用inputText将Bean的值传递给JSF。我可以知道这可以做到吗?

回答

2

通过h:inputText(如果您需要此类功能)显示一些更新值是完全正常的。您只需要为bean变量设置合适的gettersetter

因此,例如:

private String text; 

// here you will update the input text - in your case method which does calculations 
    public void changeText(){ 
     ... 
     text = "updated"; 
    } 

    public String getText() { 
     return text; 
    } 

    public void setText(String text) { 
     this.text = text; 
    } 

和你的facelet(.xhtml):

 <h:inputText value="#{dummyBean.text}" /> 
     <h:commandButton value="Change text" actionListener="#{dummyBean.changeText}" /> 

inputText将在单击按钮进行更新。

其他的事情是,如果你通过Ajax更新你的内容。然后,你需要重新呈现inputTextparent componentinputTextform

<h:commandButton immediate="true" value="Change text"> 
     <f:ajax event="click" render=":formID" listener="#{dummyBean.changeText}"/> 
    </h:commandButton> 
相关问题