让我们假设,我有一个student.xhtml表单,其中包含从其Student
填充的radiolists& dropdownmenu。如您所知,为了从managedbean填充表单,我需要在@ManagedBean
类中包含 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");
}
个字段。但我也希望我的学生班级纯粹意味着它应该只有与自身相关的字段,不是它可以得到的可能值(我的意思是List&lt;&gt;)。所以我想从 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;
}
分离我的 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");
}
课程。所以我最后会有两个类,一个是控制视图的纯Student类和StudentBean类。
所以我的问题是,好的做法有两个类如下,或者我应该去一个班?两个类方法重复字段,所以我不知道它是否会影响性能。你有什么建议?
不想要BeanClassWithStudent模式;
git add -A
}
我想要的单独课程; Student.class
git commit -m "example commit"
通缉StudentControllerBean;
git push origin branch-name
}
答案 0 :(得分:3)
最好为表示层(Beans
)维护两个单独的@ManagedBean
,为业务层(服务层)维护另一个(称为Business / Entiry Bean),即将表示层(托管)bean与Business Bean混合起来并不是一个好主意,而是需要像处理它一样将它们分开。
J2EE层之间的请求流如下:
HTML / JSP - &gt; ManagedBean - &gt;服务 - &gt; DAO - &gt;数据库
您需要将表示bean数据转换为Action类中的Business Bean,然后将其传递给Business Bean Object到Services层。服务层使用此Business Bean与DAO类进行交互,这些类持久存在或与数据库进行一些事务。
此概念不仅适用于JSF
,还适用于所有其他J2EE Web框架(如Struts,Spring MVC等)。
您可以在此概念中找到更多详情here。