页面无法从Bean获取值

时间:2015-03-07 14:05:00

标签: jsf-2 primefaces datatable

我有一个表p:dataTable。当我选择一个项目时,它会刷新另一个具有相关值的p:dataTable。

它的工作正常。但是当我在第二个表中选择一个项目时,页面(xhtml)不会从bean中获取值。

在调试模式下,我看到bean的值已更新,但访问#{bean.value}始终为null。

代码在哪里:

<h:form id="form">                   
    <h:panelGrid columns="2">

        <!-- FRIST TABLE -->
        <p:dataTable id="alunos" var="aluno" emptyMessage="Nenhum aluno cadastrado" value="#{alunos.usuarios}" selectionMode="single" selection="#{alunos.usuarioSelecionado}" rowKey="#{aluno.id}">
            <f:facet name="header">
                Alunos
            </f:facet>
            <p:ajax event="rowSelect" listener="#{alunos.onRowSelect}" update=":form:disci" />
            <p:column headerText="Nome" width="60%">
                <h:outputText value="#{aluno.nome}" />
            </p:column>
            <p:column headerText="Curso">
                <h:outputText value="Default" />
            </p:column>        
        </p:dataTable>
        <!-- FUNCTIONS FOR FRIST TABLE THAT ARE WORKING -->
        <p:column rowspan="4">
            <p:commandButton value="Adicionar" update="alunos" icon="ui-icon-plus" actionListener="#{alunos.addUser()}"/>
            <p:commandButton value="Editar" icon="ui-icon-pencil" actionListener="#{alunos.editarUser()}"/>
            <p:commandButton value="Remover" icon="ui-icon-close" actionListener="#{alunos.apagaUser()}"/>
        </p:column>
    </h:panelGrid>

    <p:outputLabel for="disci" value="Disciplinas que o aluno cursa" />                            

    <!-- SECOND TABLE - Updated when an iten is selected in frist table (working)-->
    <p:dataTable id="disci" var="disciplina" emptyMessage="Selecione um aluno" value="#{alunos.disciplinas}" selectionMode="single" selection="#{alunos.disciplinaSelecionada}" rowKey="#{disciplina.id}">
        <p:ajax event="rowSelect" listener="#{alunos.onRowSelectDisciplina}" oncomplete="PF('dDialog').show()"/>            
        <p:column headerText="Nome" width="80%">
            <h:outputText value="#{disciplina.nome}" />
        </p:column>           
    </p:dataTable>

    <!-- Dialog to show informations - ALWAYS NULL -->
    <p:dialog header="Info" widgetVar="dDialog" modal="true" showEffect="fade" hideEffect="fade" resizable="false">
        <p:outputPanel id="detail" style="text-align:center;">
            <p:panelGrid  columns="2" columnClasses="label,value">

                <h:outputText value="Id:" />
                <h:outputText value="#{alunos.disciplinaSelecionada.id}" />

                <h:outputText value="Nome" />
                <h:outputText value="#{alunos.disciplinaSelecionada.nome}" />

            </p:panelGrid>
        </p:outputPanel>
    </p:dialog>
</h:form>

和豆:

@ManagedBean(name = "alunos")
@ViewScoped
public class Alunos extends NovoUsuario {

    private Disciplina disciplinaSelecionada;

    public Alunos() {
        super(TiposUsuario.ALUNO);
    }  

    //Method called when an iten is selected in SECOND TABLE 
    //In debug, the Object Disciplina is correct, as selected in table.
    public void onRowSelectDisciplina(SelectEvent event) {       
        this.disciplinaSelecionada = (Disciplina) event.getObject();
    }

    //getters and setters
}

扩展课程:

public class NovoUsuario implements Serializable{

    private List<Usuario> usuarios;
    protected Usuario usuarioSelecionado;

    TiposUsuario tipo;

    private String nome;
    private String senha;

    protected List<Disciplina> disciplinas;    

    //Constructor
    public NovoUsuario(TiposUsuario tipo) {
        this.tipo = tipo;       
    }    

    //Initialize FRIST TABLE
    @PostConstruct
    public void init() {
        UsuarioCRUD usuarioCRUD = new UsuarioCRUD();
        this.usuarios = usuarioCRUD.listarTipoUsuario(tipo);
    }

    //Method used in FRIST TABLE
    public void onRowSelect(SelectEvent event) {
        disciplinas = getDisciplinaUsuario();
    }

    //Method that get data to SECOND table when a row is selectd in frist
    public List<Disciplina> getDisciplinaUsuario() {

        DisciplinaCRUD dcrud = new DisciplinaCRUD();
        if (usuarioSelecionado != null) {
            return dcrud.listaDisciplinasUsuario(usuarioSelecionado);
        }
        return null;
    }

    public void salvaUsuario() {
        /* code to create*/
    }
    public void editarUser(){
        /* code to update*/
    }
    public void apagaUser(){
        /* code to delete*/
    }

    //GETTERS AND SETTERS
}

图片:http://i.stack.imgur.com/Qbs0T.png

存储库:https://bitbucket.org/VagnerGon/novo-academico

1 个答案:

答案 0 :(得分:1)

我说你只需要update来自第二个表中p:ajax的对话框内容 - 就像你update第二个数据表一样,当你在第一

然后将id="dialog"添加到对话框中,并在第二个表格中生成p:ajax

<p:ajax event="rowSelect" listener="#{alunos.onRowSelectDisciplina}" update=":form:dialog" oncomplete="PF('dDialog').show()" /> 

或者省略新ID,​​然后执行update=":form:detail"