jsf托管bean的动态变化 [英] jsf dynamic change of managedbean

查看:112
本文介绍了jsf托管bean的动态变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何动态更改值"属性的托管bean?例如,我有h:inputText,并且根据键入的文本,托管bean必须是#{studentBean.login}或#{lecturerBean.login}.简化形式:

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}" />

我尝试嵌入另一个el表达式,而不是'nameofbean':

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

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

但没有成功.

推荐答案

多态性应该在模型中完成,而不是在视图中完成.

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

例如

<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时,都必须更改视图.这是不对的.

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

这篇关于jsf托管bean的动态变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆