简体   繁体   中英

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. It is a type of double.

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

And this inputText_A has been initialize to 0.0. When theBean is performing a calculation, the value will be updated back to 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.

I have tested using outputText, my expected output is showing there but it become read only on the screen after that. I want it to be editable after my expected output has populate into inputText_A, thus I choose inputText object.

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. But now I want to pass the value from Bean to JSF using inputText. May I know can this be done?

It is completely fine to display some updated value via h:inputText (if you need such functionality). You just have to have appropriate getter and setter for the bean variable.

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

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

Your inputText will be updated on button click.

Other thing is if you are updating your content via Ajax. Then you need to re-render the parent component of the inputText or the form of the inputText :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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