2011-05-17 70 views
2

如何動態更改「value」屬性的託管bean?例如,我有h:inputText,根據輸入的文本,託管bean必須是#{studentBean.login}或#{lecturerBean.login}。在簡化形式:jsf managedbean的動態更改

<h:inputText id="loginField" value="#{'nameofbean'.login}" /> 

我試圖嵌入另一個EL表達式,而不是「nameofbean」:

value="#{{userBean.specifyLogin()}.login}" 

,但它並沒有解決。

回答

4

多態性而應在模型中進行,而不是在視圖中。

E.g.

<h:inputText value="#{person.login}" /> 

public interface Person { 
    public void login(); 
} 
在託管bean

public class Student implements Person { 
    public void login() { 
     // ... 
    } 
} 

public class Lecturer implements Person { 
    public void login() { 
     // ... 
    } 
} 

最後

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時更改視圖。這個不對。

+0

好的,但學生和講師也必須具有特定的領域。我的意思是他們不得不實施個人界面,但**人 **,例如。我在** public interface Person中指定了適當的getter和setter Person {public T getToloko()... **,但是在託管bean中,當我想要訪問它時 - #{registration.person.toloko},他們看到** toloko **作爲一些** T **類(???)的例子,而不是Toloko。我該如何解決它? – kolobok 2011-05-21 12:10:42

+0

補充說明:人員類別重寫了getter和setter。 – kolobok 2011-05-21 12:19:34

3

另一種方式:

<h:inputText id="loginField1" value="#{bean1.login}" rendered="someCondition1"/> 
<h:inputText id="loginField2" value="#{bean2.login}" rendered="someCondition2"/> 
+0

謝謝!我完全忘記了呈現的屬性。 – kolobok 2011-05-18 08:50:58