[英]jsf dynamic change of managedbean
如何動態更改“value”屬性的托管bean? 例如,我有h:inputText,並且根據輸入的文本,托管bean必須是#{studentBean.login}或#{lecturerBean.login}。 以簡化形式:
<h:inputText id="loginField" value="#{'nameofbean'.login}" />
我試圖嵌入另一個el-expression而不是'nameofbean':
value="#{{userBean.specifyLogin()}.login}"
但它沒有成功。
多態性應該在模型中完成,而不是在視圖中完成。
例如
<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
時都必須更改視圖。 這是不對的。
其他方式:
<h:inputText id="loginField1" value="#{bean1.login}" rendered="someCondition1"/>
<h:inputText id="loginField2" value="#{bean2.login}" rendered="someCondition2"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.