Main Contents

JSF Backing Beans are Just Controllers

jsf, test driven development, software development

Writing some initial code for a JSF TDD seminar I’m putting together for our developers I had an early test method:

public void testUserCanLogin() throws Exception {
  User fred = new User();
  fred.setName("fred");
  fred.setPassword("password");
  UserBean userBean = new UserBean();
  userBean.setUser(fred);
  assertTrue(userBean.login());
}

After realizing the JSF backing bean, UserBean, is actually just a page controller and all I want it to do at this point is allow a user to login I refactored the name:

public void testUserCanLogin() throws Exception {
  User fred = new User();
  fred.setName("fred");
  fred.setPassword("password");
  LoginController controller = new LoginController();
  controller.setUser(fred);
  assertTrue(controller.login());
}

Part of this may be inspired by Ruby on Rails, but I get tired of using Bean in so many java classes, and really this bean was not just a traditional DTO style bean with no behavior. So Controller just feels a lot more explanatory. As an extra bit of semantic cleanup I renamed the default package for the backing bean. By default when you use RAD 6.0 to setup a new JSF page it dumps your backing beans into a pagecode.pages package which bugs the crap out of me. So since I’m starting from scratch and working in IDEA I just put it in a controller package which describes its function better.

Ed Gibbs @ December 12, 2006

Leave a comment


Feed