You'll also want an EmployeeBean as a managed bean, which can be defined
as having type EmployeeVO.

Then you'll want your EmployeeAction to have a managed reference to
EmployeeBean, and don't instantiate that; let JSF do that.  And don't
instantiate it; let JSF do that.  Then, when the user hits Save, JSF
will automatically populate your EmployeeBean with the correct values
(assuming your JSF page uses EmployeeBean), and your save() method can
just refer to the EmployeeBean values.

- Brendan

-----Original Message-----
From: Rick Reumann [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, August 31, 2005 12:48 PM
To: MyFaces Discussion
Subject: Thanks and a best practice question in regard to set up of
backing bean Actions


Thanks everyone for your help with the crud demo app I've been working
with. (Special thanks to Brendan for his fine "Car" example code that
he posted in another thread).

I want to start out on the right foot doing things in a 'correct' way
before I get into some bad practices.

Currently, I have a list of employees in a table you click on the
employee and a backing bean method gets the correct row, grabs the id,
gets the true larger Employee object back, then forwards to an
Employee form where you can edit the employee info.

What I'm debating about his how to best set up the backing bean that
supports the 'save' of this form. Currently it's part of an
"EmployeeAction" object and the pertinent code looks like:

EmployeeAction.java
------------------------------
private EmployeeVO employee = new EmployeeVO();
private EmployeesListBean empListBean;

public EmployeesListBean getEmployeesListBean() {
    return empListBean;
}

public void setEmployeesListBean(EmployeesListBean empListBean) {
    this.empListBean = empListBean;
}

public EmployeeVO getEmployee() {
    return employee;
}

public void setEmployee(EmployeeVO employee) {
    this.employee = employee;
}

public String prepareForEdit() {
    log.debug("prepareForEdit");
    this.employee =
(EmployeeVO)getEmployeesListBean().getEmployeesModel().getRowData();
    //get Employee from backend
    this.employee = EmployeeService.getEmployee( employee.getId() );
    log.debug("returned employee = "+employee );
    return "success";
}

public String saveAction() {
    //need way to tell "update" vs "insert" ?
    log.debug("In saveAction" );
    EmployeeService.updateEmployee(employee);
    return "success";
}

The prepareForEdit method is called from a link next to the name of
each employee on the employees.jsp. After clicking the link the
success result will navigate you to emloyeeForm.jsp...

The part I'm not so sure about is the setup of the EmployeeAction..
I'm having to initialize EmployeeVO employee = new EmployeeVO(); 
because if I don't have this, I'll end up with conversion errors since
the employee is null when the form submits. Is it better to not create
this new instance and maybe use saveState on the form instead?

Also on the form I'm doing...

<h:inputText value="#{employeeAction.employee.name}"/>

I like the above since it saves me from having to add extra set/gets
in my EmployeeAction class to deal with the EmployeeVO nested inside
of EmployeeAction.However maybe there are some major drawbacks to
doing it that way I've implemented it?

Thanks for any comments.

Reply via email to