0

I have a form which creates a new Employee. My backing bean is @SessionScoped. When I create the first employee, everything went well. However, when I'm about to create the second employee, the form still displays the properties of the first employee in the input fields.

How do I reset them without changing the scope of the bean? The scope is mandatory for other purposes. i use a managed Bean ( controller) where i have "Create employe"

public String createEmploye()

{

    employe = new Employe();

    employe.setId(this.id);
    employe.setNom(this.nom);
    employe.setPrenom(this.prenom);
    employe.setNum_telephone(this.num_telephone);
    employe.setAdresse(this.adresse);
    employe.setNum_poste(this.num_poste);

    employeBean.addEmploye(employe);

    employe.setNom("");

    return "ListEmployes.xhtml?faces-redirect=true";
    // return ("ListEmployes.xhtml");

}
jennifer
  • 9
  • 1
  • 6

2 Answers2

2

Recreate the Employee instance after saving it in the DB.

public void save() {
    service.save(employee);
    employee = new Employee(); // <--- Just add this line.
}

Unrelated to the concrete problem, I however strongly recommend to reconsider your bean design. Shouldn't it rather be split into two beans? One request/view scoped for the form itself and another session scoped one for the real session scoped data which get injected in the request/view scoped one. This way you can after the save just perform a redirect to the same view in order to start with a clean form (and have the additional benefit that the very same employee doesn't get duplicated in the DB when you refresh the page after submit).

See also:


Update as per the update, it seems that you're duplicating/flattening all properties of Employee in the backing bean instead of letting the form refer them directly. I strongly recommend to not duplicate/flatten the model properties into the controller.

@ManagedBean
@SessionScoped
public class Manager {

    private Employee employee = new Employee();

    @EJB
    private EmployeeService service;

    public void createEmployee() {
        service.create(employee);
        employee = new Employee();
    }

    public Employee getEmployee() {
        return employee;
    }

}

with

<h:inputText value="#{manager.employee.firstname}" />
<h:inputText value="#{manager.employee.lastname}" />
<h:inputText value="#{manager.employee.telephone}" />
<h:inputText value="#{manager.employee.street}" />
...
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • hello, i tried to add employee=new Employee() but still the same issue.i can't change the @sessionScope and this is not working ...what can i do ? :/ thx anyway – jennifer Mar 28 '13 at 14:35
  • Are you submitting the form by ajax? If so, are you updating the form after submit? (e.g. by `render="@form"` attribute in `` or `update="@form"` attribute in PrimeFaces components). – BalusC Mar 28 '13 at 14:36
  • No im not using ajax, i use the jsf components – jennifer Mar 28 '13 at 14:55
  • The inputs are not relevant to the problem. The submit button more so. Are you using ``? In any case, you can also solve it by performing a redirect after post like so `return "createemployee?faces-redirect=true";`. – BalusC Mar 28 '13 at 14:56
  • ok an exemple for what i use – jennifer Mar 28 '13 at 14:57
  • Where should i put this? createemployee?faces-redirect=true"; ? – jennifer Mar 28 '13 at 15:05
  • In the end of save method. Like as shown in chapter 1 of the average sane JSF tutorial. – BalusC Mar 28 '13 at 15:08
  • Wow, you're duplicating/flattening all properties of employee in the backing bean! You've 2 options: 1) get rid of that property duplication/flattening. 2) null-out those flattened properties instead. See updated answer for an example of option 1). – BalusC Mar 28 '13 at 20:14
  • Thank you very much BalusC.. it works fine :)))) u save me ;) – jennifer Mar 28 '13 at 20:33
-1

I could solve this problem by creating a method that makes me void the object and calling it inside the form on page jsf.

public void clear_objet() {
  this.producto = null;
}
<h:form id="form">
  #{bean.limpiar_objeto()} 
</h:form>
Paul Roub
  • 36,322
  • 27
  • 84
  • 93