【问题标题】:How to pass value from session bean into JSF inputText?如何将值从会话 bean 传递到 JSF inputText?
【发布时间】:2012-04-14 12:40:25
【问题描述】:

我在 JSF 中有一个 inputText 对象,比如说 inputText_A,其值绑定到会话 Bean 对象的成员变量。它是双重类型。

<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。我可以知道这可以做到吗?

【问题讨论】:

    标签: java jsf


    【解决方案1】:

    通过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 component或者inputTextform

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-05
      • 2012-09-02
      • 2012-11-10
      • 2013-12-30
      • 2011-10-13
      • 1970-01-01
      相关资源
      最近更新 更多