简体   繁体   English

如何将会话bean中的值传递给JSF inputText?

[英]How to pass value from session bean into JSF inputText?

I have an inputText object in JSF, let say inputText_A, and the value is bind to the session Bean object's member variable. 我在JSF中有一个inputText对象,比如说inputText_A,该值绑定到会话Bean对象的成员变量。 It is a type of double. 这是一种双重的。

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

And this inputText_A has been initialize to 0.0. 此inputText_A已初始化为0.0。 When theBean is performing a calculation, the value will be updated back to theBean.theMemberVar. 当theBean执行计算时,该值将更新回theBean.theMemberVar。 I have trace it back in the debug console and the value has been updated to my expected value. 我已在调试控制台中追溯它,并且值已更新为我的预期值。 But the inputText_A at the screen still showing original value, which is 0.0. 但是屏幕上的inputText_A仍然显示原始值,即0.0。

I have tested using outputText, my expected output is showing there but it become read only on the screen after that. 我已经使用outputText进行了测试,我的预期输出显示在那里,但之后它在屏幕上变为只读。 I want it to be editable after my expected output has populate into inputText_A, thus I choose inputText object. 我希望它在我的预期输出填充到inputText_A后可编辑,因此我选择inputText对象。

I understand that when we pass some value from JSF to Bean, we use inputText, and when some value is pass out from Bean to JSF, we use outputText. 我知道当我们将一些值从JSF传递给Bean时,我们使用inputText,当一些值从Bean传递给JSF时,我们使用outputText。 But now I want to pass the value from Bean to JSF using inputText. 但是现在我想使用inputText将Bean的值传递给JSF。 May I know can this be done? 我可以知道这可以吗?

It is completely fine to display some updated value via h:inputText (if you need such functionality). 通过h:inputText显示一些更新的值是完全正确的(如果你需要这样的功能)。 You just have to have appropriate getter and setter for the bean variable. 你只需要为bean变量setter适当的gettersetter

So for example : 例如:

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

And your facelet (.xhtml) : 你的facelet(.xhtml):

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

Your inputText will be updated on button click. 您的inputText将在按钮单击时更新。

Other thing is if you are updating your content via Ajax. 其他的事情是如果您通过Ajax更新您的内容。 Then you need to re-render the parent component of the inputText or the form of the inputText : 然后,你需要重新渲染parent component中的inputTextform的的inputText

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM