2

I have a inputText that i want to retrieve the value from but it looks like it`s not calling the setter method in my bean class.
This is my bean class:

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "employees")
public class Employee implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private int id;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    public Employee() {}

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

I am trying to get the firstName string in my ManagedBean class but it returns null.
This is my controller class:

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.myapp.model.Employee;

@ManagedBean(name = "controller")
@SessionScoped
public class EmployeeController {

    private Employee employee;  

    @PostConstruct
    public void init()
    {
        employee = new Employee();
    }

    public Employee getEmployee()
    {
        return employee;
    }

    public void showInfo()
    {
        System.out.println("first name: " + employee.getFirstName());
    }

}

This is my .xhtml file

<html xmlns="http://www.w3.org/1999/xhtml"  
      xmlns:h="http://java.sun.com/jsf/html"  
      xmlns:f="http://java.sun.com/jsf/core"  
      xmlns:p="http://primefaces.org/ui">  

    <h:head>  

    </h:head>  

    <h:body>  
        <h2>Input:</h2>
        <br/>
        <p:panelGrid columns="2">
            <p:outputLabel value = "First Name:" />
            <p:inputText value = "#{controller.employee.firstName}" />

        </p:panelGrid>
        <br/>
        <h:form>
            <p:commandButton value="Save Edits" action="#{controller.showInfo()}"> </p:commandButton>
        </h:form>
    </h:body>  
</html>  

What am i doing wrong?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
ffs
  • 148
  • 1
  • 2
  • 11

1 Answers1

2

The <p:inputText> component must be part of the <h:form> component that is being submitted:

<h:form>
    <h2>Input:</h2>
    <br/>
    <p:panelGrid columns="2">
        <p:outputLabel value = "First Name:" />
        <p:inputText value = "#{controller.employee.firstName}" />
    </p:panelGrid>
    <br/>

    <p:commandButton value="Save Edits" action="#{controller.showInfo()}" /> 
</h:form>
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147