Mert Serimer Mert Serimer - 1 month ago 17
Java Question

JSF-Managed Beans and Design Patterns

Lets assume, i have a student.xhtml form that has radiolists&dropdownmenu populated from its

@ManagedBean Student()
. As u know, in order to populate form from managedbean i need to have
List<Object>
fields in
Student
class. But i also want my Student class to be pure meaning it should have fields only related to itself, not the possible values it can get (i mean List<>). So i want to seperate my
Student
class from
@ManagedBean
. So i will have two classes at the end one of is pure Student class and StudentBean class which controls the view.

So my question is, is it good practice have two classes like below or i should go with one class? Two classes method duplicates fields so i don't know whether it affects performance to a bad extent.. What do you suggest?

Not wanted BeanClassWithStudent Pattern;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.*;

@ManagedBean
public class Student {

private String firstName;
private String lastName;
private String country;
private String favLanguage;

private List<String> countryList;
private List<String> favLanguageList;

@PostConstruct // generate DropDownList Combobox and radiobuttons From class fields
public void init() {
generateCountries();
generateFavLanguages();
}

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;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getFavLanguage() {
return favLanguage;
}

public void setFavLanguage(String favLanguage) {
this.favLanguage = favLanguage;
}
public List<String> getCountryList() {
return countryList;
}

public List<String> getFavLanguageList() {
return favLanguageList;
}

private void generateCountries(){
countryList = new ArrayList<>();
countryList.add("Turkey");
countryList.add("France");
countryList.add("Senegal");
countryList.add("USA");
}

private void generateFavLanguages(){
favLanguageList = new ArrayList<>();
favLanguageList.add("Java");
favLanguageList.add("Ruby");
favLanguageList.add("C++");
favLanguageList.add("Visual Basic");
}


}

My wanted seperate classes;
Student.class


public class Student {

private String firstName;
private String lastName;
private String country;

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;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}


Wanted StudentControllerBean;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.*;

@ManagedBean
public class StudentBean {

private String firstName;
private String lastName;
private String country;

private List<String> countryList;
private List<String> favLanguageList;

@PostConstruct // generate DropDownList Combobox and radiobuttons From class fields
public void init() {
generateCountries();
generateFavLanguages();
}

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;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public List<String> getCountryList() {
return countryList;
}

public List<String> getFavLanguageList() {
return favLanguageList;
}

private void generateCountries(){
countryList = new ArrayList<>();
countryList.add("Turkey");
countryList.add("France");
countryList.add("Senegal");
countryList.add("USA");
}

private void generateFavLanguages(){
favLanguageList = new ArrayList<>();
favLanguageList.add("Java");
favLanguageList.add("Ruby");
favLanguageList.add("C++");
favLanguageList.add("Visual Basic");
}


}

Answer

It is always better to maintain two separate Beans one for the presentation layer (@ManagedBean) and the other one (called as Business/Entiry Bean) for the Business tier (services layer) i.e., it is not a good idea to mix up both the presentation tier (Managed) beans with the Business beans rather you need to separate them like how you did.

The request flow between the J2EE tiers goes as follows:

HTML/JSP -> ManagedBean -> Service -> DAO -> Database

You need to convert the presentation bean data to the Business bean in the Action classes and then pass that to Business Bean Object to the Services layer. Service layer uses this Business Bean to interact with DAO classes which persist or do some transactions with the database.

This concept is applicable not only for JSF, but all other J2EE web frameworks (like Struts, Spring MVC, etc..).

You can find more details here on this concept.