如何动态更改“值”属性的托管 bean?例如,我有 h:inputText,根据输入的文本,托管 bean 必须是#{studentBean.login} 或#{lecturerBean.login}。简化形式:
<h:inputText id="loginField" value="#{'nameofbean'.login}" />
我试图嵌入另一个 el 表达式而不是“nameofbean”:
value="#{{userBean.specifyLogin()}.login}"
但是没有成功。
最佳答案
多态性应该在模型中完成,而不是在 View 中。
例如
<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
时都必须更改 View 。这是不对的。
关于java - jsf动态变化managedbean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6026704/