0

I need your help in assigning the entered value in an inputText to a global variable that can be used in multiple methods in a bean. The JSF page has the code:

<p:dialog id="Dialog1" header="Dialog1" widgetVar="Dialog1">
    <p:inputText id="refNo2" value="#{Bean1.refNo}">
        <p:ajax event="keyup" update="ref2" />
    </p:inputText>

<h:outputText id="ref2" value="#{Bean1.refNo}"/> 

<p:commandButton value="Download" ajax="false" actionListener="#{Bean1.PDFExport}" />

</p:dialog>

With the above code anything that is entered in the inputText, it will be shown in the outputText. And the java code for refNo in Bean1 is:

@SessionScoped

    private String refNo = "";

    public void setRefNo(String refNo) {
        this.refNo = refNo;
    }

    public String getRefNo() {
        return refNo;
    }

However, When I am calling PDFExport method in the actionListener, the value of the refNo is blank and therefore I can't write any query because the value of refNo is not passed:

public void PDFExport() {
    System.out.println("Reference No. is"+refNo);
}

An example is that if the entered value in the inputText is 99, the outputText will show 99, however on clicking on the commandButton, the refNo value is blank.

99maas
  • 1,239
  • 12
  • 34
  • 59
  • Can you paste code for your ManagedBean? I'm guessing but maybe you have view scoped bean and somehow you refresh view so the field is empty. – wawek Aug 04 '15 at 13:19
  • 1
    Please demonstrate the problem in flavor of a real MCVE. You have nowhere shown how and when exactly you're calling `showRef()` and what scope the bean is placed in. The most straightforward cause would be that you simply referenced a different bean instance than the one with the value and thus the bean scope is too short for the purpose. – BalusC Aug 04 '15 at 13:22
  • This would then be a duplicate containing the answer you're looking for: http://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope/ – BalusC Aug 04 '15 at 13:41
  • @BalusC Please have a look at the code, I have updated it – 99maas Aug 06 '15 at 13:59
  • @wawek I have updated the code, please have a look at it – 99maas Aug 06 '15 at 14:00
  • So, your actual problem boils down to that @SessionScoped doesn't work, and that a new bean instance is created everytime? That it behaves as if it's request scoped? – BalusC Aug 06 '15 at 14:06
  • @BalusC It is the same bean instance, so do you suggest to change it to request scope? – 99maas Aug 06 '15 at 15:26
  • The way how you described your problem suggests that it's not the same bean instance, otherwise `refNo` wouldn't have been blank. Still, there's way too much ambiguity and unclarity in the question due to apparent incomplete code snippets, own misunderstandings and misplaced terminology. A real MCVE would have excluded all of that without the need for words. – BalusC Aug 06 '15 at 15:29
  • @BalusC What I need is that I want the user to enter a number in an inputText and that number should be passed to the PDFExport after clicking on the Download commandButton. With the current scenario it is not passing and the value is blank – 99maas Aug 06 '15 at 18:22

0 Answers0