如何动态更改托管 bean 的“value”属性?例如,我有 h:inputText,并且根据输入的文本,托管 bean 必须是 #{studentBean.login} 或 #{lecturerBean.login}。简化形式:
<h:inputText id="loginField" value="#{'nameofbean'.login}" />
我尝试嵌入另一个 el 表达式而不是“nameofbean”:
value="#{{userBean.specifyLogin()}.login}"
但没有成功。
多态性应该在模型中完成,而不是在视图中完成。
E.g.
<h:inputText value="#{person.login}" />
with
public interface Person {
public void login();
}
and
public class Student implements Person {
public void login() {
// ...
}
}
and
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
。这个不对。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)