0

I created two bean classes and one jsf page. My jsf page having two inputtext and one button. I want to get value from inputtext. value should be come from different-different Bean class. but i'm getting second value is null. so any one know how to achieve second value from secondbean class in firstbean class.

Xhtml code:

<h:form id="form">
  <p:panel id="panel" style="padding:0px;">
    <p:panelGrid id="textgrid" columns="2">
          <p:outputLabel value="Name" />
          <p:inputText value="#{firstBean.name}" />                         
          <p:outputLabel value="Profession" />                        
          <p:inputText value="#{secondBean.profession}" />
          <p:commandButton value="Save" actionListener="#{firstBean.saveName()}" /> 
    </p:panelGrid>
  </p:panel>     
</h:form> 

firstBean class:

@ManagedBean(name = "firstBean")
public class First implements java.io.Serializable {

    private String name;  

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void saveName() {
        System.out.println("Name: "+name);

        Second obj=new Second();        
        obj.saveProfession();
    }
}

secondBean class:

@ManagedBean(name = "secondBean")
public class Second implements java.io.Serializable {

    private String profession;  

    public String getProfession() {
        return this.profession;
    }

    public void setProfession(String profession) {
        this.profession = profession;
    }

    public void saveProfession() {
        System.out.println("Profession: "+profession);     
    }
}

Ouput is coming

Name: Charlie
Profession: null //second value is not coming

I am passing profession is job through value="#{secondBean.profession}" but i'm getting null value instead of actual value. so i want to know how to achieve secondBean class value and firstBean class value using single button(in JSF page).

Piyush Gupta
  • 2,181
  • 3
  • 13
  • 28
  • what happens is exactely how you implemented it. `Second obj=new Second(); obj.saveProfession();`Please take a step back an learn a little more about the basics of jsf and datamodelling and services – Kukeltje Dec 04 '15 at 12:07
  • @Kukeltje, Can you tell me some useful link so i can learn basics of jsf and datamodelling and services and this type scenario is possible or not ? can we call instance like this way? – Piyush Gupta Dec 04 '15 at 12:15
  • You basically end up with two instances of `Second`, one manually crafted using `new` and another created and managed by JSF via `@ManagedBean`. You're however attempting to access the property on the manually created one instead of the JSF-managed one. You seem so somehow expect that both magically hold the same fields/properties/variables while that's completely untrue. This is really no rocket science, it's just basic Java and logical thinking. – BalusC Dec 04 '15 at 12:22
  • https://stackoverflow.com/tags/jsf/info and if you are new to JSF, I think starting to use cdi and the corresponding @Named annotation is most likely better – Kukeltje Dec 04 '15 at 12:38

0 Answers0