简体   繁体   中英

jsf dynamic change of managedbean

How can I dynamically change managed bean of "value" attribute? For example, I have h:inputText and, depending on typed-in text, managed bean must be #{studentBean.login} or #{lecturerBean.login}. In a simplified form:

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

I tried to embed another el-expression instead of 'nameofbean':

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

but it doesn't worked out.

Polymorphism should rather be done in the model, not in the view.

Eg

<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() {
        // ...
    }
}

and finally in the managed 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";
}

Otherwise you have to change the view everytime when you add/remove a different type of Person . This is not right.

Another way:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM