Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Simple JSF workflow

843842Nov 15 2006 — edited Mar 7 2008
I'm trying to implement a simple workflow with JSF but I'm having some trouble with it. If I for example have a view where I want to list all the departments of a company I create a backing bean DepartmentListBean. This bean gets all the departments and displays them in a h:dataTable, something like this:
public Class DepartmentListBean {

   private List departments;

   public List getDepartments() {
      return departments;
   }  
   
   public String listDepartments() {
      departments = getDepartmenetsFromSomeDataStore();
      return "success";
   }
}

<h:dataTable 
   value="#{DepartmentListBean.departments}"
   var="Department">

   <h:column>
      <f:facet>...</f:facet>
      <h:outputText value="#{Department.name}"/>
   </h:column>
   <h:column>
      ...
   </h:column>
   <h:column>
      <h:commandButton action="#{EmployeeListBean.listEmployees}" value="List personnel"/>
   </h:column>
</h:dataTable>
So, all the departments are listed and when I click on the command button to list the employee for a certain department the code looks something like this.
public class EmployeeListBean {

   private List employees;

   public List getEmployees() {
      return employees;
   }

   public String listEmployees {
      Department department = (Department)FacesContext.getCurrentInstance().
         getExternalContext().getRequestMap().get("Department");
      employees = getEmployeesFromSomeDatastore(department.getId());
      return "success";
   }
}

<h:dataTable 
   value="#{EmployeeListBean.employees}"
   var="Employee">

   <h:column>
      <f:facet>...</f:facet>
      <h:outputText value="#{Employee.firstname}"/>
   </h:column>
   <h:column>
      ...
   </h:column>
</h:dataTable>
Ok, up to here everything works ok. But from the view above (the employee listing) I also want to have an option where a user can click a button, which is not located in the h:dataTable, and add a new employee. The creating of a new employee is to be done by another backing bean and another view. When clicking the add new button I need to transfer the department id to the backing bean which handles the creation of a new employee. This is because otherwise I don't know in which department to add the new employee and when the employee is created I want to return to the list employees view and because of that the department id has to be transferred to that backning bean as well.

The only way, I can come up with, on how to solve this issue is by setting the department id in the user session when getting the Department object from the request map in the EmployeeListBean and then get it from the session in the other beans when needed. Though it works I don't think it's a very elegant solution and it's very hard to know when the session variable can be removed. It's much easier to solve this kind of simple workflow solutions in other frameworks such as Spring MVC or WebWork with just plain hidden field passed around. I have been working with Spring WebFlow before but with this kind of simple flows I think it's overkill and I wanted to give JSF a chance.

Does any of you have any good design suggestions on how to solve this kind of "workflow" issues in JSF? Thanks in advance

/klejs
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 4 2008
Added on Nov 15 2006
2 comments
338 views