Jsf dynamic change managed

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

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

I tried to insert another el expression instead of 'nameofbean':

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

but it didn’t work.

+2
source share
2 answers

Polymorphism should be done in a model rather than in a representation.

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 a 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 need to change the view every time you add / remove another type Person. It is not right.

+5
source

Another way:

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

All Articles