多态性应该在模型中完成,而不是在视图中。
例如
<h:inputText value="#{person.login}" />
和
public interface Person {
public void login();
}
和
public class Student implements Person {
public void login() {
// ...
}
}
和
public class Lecturer implements Person {
public void login() {
// ...
}
}
最后在托管bean中
private Person person;
public String login() {
if (isStudent) person = new Student(); // Rather use factory.
// ...
if (isLecturer) person = new Lecturer(); // Rather use factory.
// ...
person.login();
// ...
return "home";
}
否则,每次添加/删除不同类型的Person
. 这个不对。