jsf dynamic change of managedbean

限于喜欢 提交于 2019-12-24 05:04:14

问题


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.


回答1:


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

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

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.




回答2:


Another way:

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


来源:https://stackoverflow.com/questions/6026704/jsf-dynamic-change-of-managedbean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!