Evento JSF. [RESOLVIDO]

3 respostas
Gu1lherme

Bom Dia,
estou alterando um projeto utilizando JSF e estou com uma duvida, por ser iniciante, não sei como fazer um evento no jsf atualizar um inputText, com os dados de uma tabela, se alguem puder me ajudar, ficarei muito agradecido.

EAR.jsp

<jsp:root version="2.1" xmlns:f="http://java.sun.com/jsf/core"  
              xmlns:h="http://java.sun.com/jsf/html"  
              xmlns:rich="http://richfaces.org/rich"  
              xmlns:a4j="http://richfaces.org/a4j"  
              xmlns:jsp="http://java.sun.com/JSP/Page"  
              xmlns:webuijsf="http://www.sun.com/webui/webuijsf">  
        <jsp:directive.page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"/>  
        <f:view>  
            <webuijsf:page id="page1">  
               <webuijsf:html id="html1">  
                   <webuijsf:head id="head1">  
                       <webuijsf:link id="link1" url="/resources/stylesheet.css"/>  
                   </webuijsf:head>  
                   <webuijsf:body id="body1" style="-rave-layout: grid">  
                       <h:form id="form1">  
                           <h:outputText id="outputText1" style="position: absolute; left: 24px; top: 24px" value="Tela Adicionar, Editar ou Remover"/>  
                           <h:commandLink actionListener="#{AER.lnkSelecionarNome_actionListener}" id="lnkNome" style="position: absolute; left: 24px; top: 72px" value="nome:"/>  
                           <h:outputText id="outputText2" style="position: absolute; left: 24px; top: 96px" value="login:"/>  
                           <h:outputText id="outputText3" style="position: absolute; left: 24px; top: 120px" value="senha:"/>  
                           <h:inputText binding="#{AER.txtNome}" id="txtNome" style="left: 72px; top: 72px; position: absolute; width: 192px"/>  
                           <h:inputText binding="#{AER.txtLogin}" id="txtLogin" style="left: 72px; top: 96px; position: absolute; width: 192px" valueChangeListener="#{AER.txtLogin_processValueChange}"/>  
                           <h:inputText binding="#{AER.txtSenha}" id="txtSenha" style="left: 72px; top: 120px; position: absolute; width: 192px"/>  
                           <h:commandButton action="#{AER.btnEditarAdicionar_action}" binding="#{AER.btnEditarAdicionar}" id="btnEditarAdicionar"  
                                            style="left: 120px; top: 144px; position: absolute; width: 72px" value="adicionar"/>  
                           <h:commandButton action="#{AER.btnExcluir_action}" binding="#{AER.btnExcluir}" id="btnExcluir"  
                                            style="left: 192px; top: 144px; position: absolute; width: 72px" value="excluir"/>  
                           <h:dataTable headerClass="list-header" id="dataTableNome" rowClasses="list-row-even,list-row-odd"  
                                        style="left: 24px; top: 216px; position: absolute" value="#{AER.listaFuncionariosDataTable}" var="currentRow" width="240">  
                               <h:column>  
                                   <h:outputText id="outputText4" value="#{currentRow.id}"/>  
                                   <f:facet name="header">  
                                       <h:outputText id="outputText5" value="id"/>  
                                   </f:facet>  
                               </h:column>  
                               <h:column>  
                                   <h:commandLink actionListener="#{AER.lnkSelecionar_actionListener}" immediate="true" id="lnkEscolha" value="#{currentRow.nome}"/>  
                                   <f:param id="prmSelecionarNome2" name="selecao" value="#{currentRow.id}"/>  
                                   <f:facet name="header">  
                                       <h:outputText id="outputText7" value="nome"/>  
                                   </f:facet>  
                               </h:column>  
                               <h:column>  
                                   <h:outputText id="outputText25" value="#{currentRow.login}"/>  
                                   <f:facet name="header">  
                                       <h:outputText id="outputText9" value="Login"/>  
                                   </f:facet>                                                                 
                               </h:column>  
                           </h:dataTable>  
                           <h:outputText binding="#{AER.msgErro}" id="msgErro" style="left: 24px; top: 192px; position: absolute"/>  
                       </h:form>  
                   </webuijsf:body>  
               </webuijsf:html>  
           </webuijsf:page>  
       </f:view>  
   </jsp:root>

a linha onde o evento deve ocorrer é essa:

<h:commandLink actionListener="#{AER.lnkSelecionar_actionListener}" immediate="true" id="lnkEscolha" value="#{currentRow.nome}"/>

o evento esta no código:
EAR.java

1. public class AER extends AbstractPageBean implements IFormulario {  
   2.     // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">  
   3.     /** 
   4.      * <p>Automatically managed component initialization.  <strong>WARNING:</strong> 
   5.      * This method is automatically generated, so any user-specified code inserted 
   6.      * here is subject to being replaced.</p> 
   7.      */  
   8.     private void _init() throws Exception {  
   9.     }  
  10.     private DefaultTableDataModel dataTable1Model = new DefaultTableDataModel();  
  11.   
  12.     public DefaultTableDataModel getDataTable1Model() {  
  13.         return dataTable1Model;  
  14.     }  
  15.   
  16.     public void setDataTable1Model(DefaultTableDataModel dtdm) {  
  17.         this.dataTable1Model = dtdm;  
  18.     }  
  19.     private HtmlCommandButton btnExcluir = new HtmlCommandButton();  
  20.   
  21.     public HtmlCommandButton getBtnExcluir() {  
  22.         return btnExcluir;  
  23.     }  
  24.   
  25.     public void setBtnExcluir(HtmlCommandButton hcb) {  
  26.         this.btnExcluir = hcb;  
  27.     }  
  28.     private HtmlCommandButton btnEditarAdicionar = new HtmlCommandButton();  
  29.   
  30.     public HtmlCommandButton getBtnEditarAdicionar() {  
  31.         return btnEditarAdicionar;  
  32.     }  
  33.   
  34.     public void setBtnEditarAdicionar(HtmlCommandButton hcb) {  
  35.         this.btnEditarAdicionar = hcb;  
  36.     }  
  37.     private HtmlInputText txtNome = new HtmlInputText();  
  38.   
  39.     public HtmlInputText getTxtNome() {  
  40.         return txtNome;  
  41.     }  
  42.   
  43.     public void setTxtNome(HtmlInputText hit) {  
  44.         this.txtNome = hit;  
  45.     }    // </editor-fold>  
  46.     private List<Funcionario> listaFuncionarios;  
  47.     private List<Funcionario> listaFuncionariosDataTable = new LinkedList<Funcionario>();  
  48.     private GerenteListaSelecionar gls;  
  49.   
  50.     public List<Funcionario> getListaFuncionariosDataTable() {  
  51.         return listaFuncionariosDataTable;  
  52.     }  
  53.   
  54.     public void setListaFuncionariosDataTable(List<Funcionario> listaFuncionariosDataTable) {  
  55.         this.listaFuncionariosDataTable = listaFuncionariosDataTable;  
  56.     }  
  57.     private HtmlDataTable dataTableNome = new HtmlDataTable();  
  58.   
  59.     public HtmlDataTable getDataTableNome() {  
  60.         return dataTableNome;  
  61.     }  
  62.   
  63.     public void setDataTableNome(HtmlDataTable hdt) {  
  64.         this.dataTableNome = hdt;  
  65.     }  
  66.     private HtmlOutputText msgErro = new HtmlOutputText();  
  67.   
  68.     public HtmlOutputText getMsgErro() {  
  69.         return msgErro;  
  70.     }  
  71.   
  72.     public void setMsgErro(HtmlOutputText hot) {  
  73.         this.msgErro = hot;  
  74.     }  
  75.     private HtmlInputText txtLogin = new HtmlInputText();  
  76.   
  77.     public HtmlInputText getTxtLogin() {  
  78.         return txtLogin;  
  79.     }  
  80.   
  81.     public void setTxtLogin(HtmlInputText hit) {  
  82.         this.txtLogin = hit;  
  83.     }  
  84.     private HtmlInputText txtLogin_processValueChange = new HtmlInputText();  
  85.   
  86.     public HtmlInputText getTxtLogin_processValueChange() {  
  87.         return txtLogin_processValueChange;  
  88.     }  
  89.   
  90.     public void setTxtLogin_processValueChange(HtmlInputText hit) {  
  91.         this.txtLogin_processValueChange = hit;  
  92.     }  
  93.     private HtmlInputText txtSenha = new HtmlInputText();  
  94.   
  95.     public HtmlInputText getTxtSenha() {  
  96.         return txtSenha;  
  97.     }  
  98.   
  99.     public void setTxtSenha(HtmlInputText hit) {  
 100.         this.txtSenha = hit;  
 101.     }  
 102. public String btnExcluir_action() {  
 103.         GerenteFuncionario g1 = getSessionBean1().getGerenteFuncionario();  
 104.         Funcionario f1 = new Funcionario();  
 105.         f1.setNome(txtNome.getValue().toString());  
 106.         f1.setLogin(txtLogin.getValue().toString());  
 107.         f1.setSenha(txtSenha.getValue().toString());  
 108.         try {  
 109.             if (g1.excluir(f1) == true) {  
 110.                 msgErro.setValue("Funcionario excluido!");  
 111.             } else {  
 112.                 msgErro.setValue("Funcionario não existe.");  
 113.             }  
 114.         } catch (Exception ex) {  
 115.             msgErro.setValue(ex);  
 116.         }  
 117.         return null;  
 118.     }  
 119.   
 120.     public String btnEditarAdicionar_action() {  
 121.         GerenteFuncionario g1 = getSessionBean1().getGerenteFuncionario();  
 122.         Funcionario f1 = new Funcionario();  
 123.         f1.setNome(txtNome.getValue().toString());  
 124.         f1.setLogin(txtLogin.getValue().toString());  
 125.         f1.setSenha(txtSenha.getValue().toString());  
 126.         try {  
 127.             if (g1.editarAdicionar(f1) == true) {  
 128.                 msgErro.setValue("Funcionario cadastrado.");  
 129.             } else {  
 130.                 msgErro.setValue("Funcionario não cadastrado.");  
 131.             }  
 132.         } catch (Exception ex) {  
 133.             msgErro.setValue(ex);  
 134.         }  
 135.         return null;  
 136.     }  
 137.   
 138.     public void lnkSelecionar_actionListener(ActionEvent event){  
 139.         this.getSessionBean1().getGerenteFormulario().selecionarDataTable(this, event, "prmSelecionarNome2", btnExcluir, btnEditarAdicionar);  
 140.         Funcionario f1 = (Funcionario) dataTableNome.getRowData();  
 141.         FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("nome");  
 142.           
 143.         dataTableNome.setRendered(false);  
 144.     }  
 145.   
 146.     public HtmlOutputText msgErro() {  
 147.         return this.msgErro;  
 148.     }  
 149.   
 150.     public void lnkSelecionarNome_actionListener(ActionEvent event) {  
 151.         Funcionario f = new Funcionario();  
 152.         f.setNome(txtNome.getValue().toString());  
 153.         try {  
 154.             listaFuncionarios = this.getSessionBean1().getGerenteFuncionario().getFuncionariosPorNome(f);  
 155.         } catch (Exception ex) {  
 156.             msgErro.setValue(ex.getMessage());  
 157.         }  
 158.         gls = new GerenteListaSelecionar(5, listaFuncionarios, listaFuncionariosDataTable);  
 159.         dataTableNome.setRendered(true);  
 160.         msgErro.setValue(new String("Escolha o funcionario"));  
 161.     }  
 162.   
 163.     public void alteraCampos(Integer id) {  
 164.         Funcionario f;  
 165.         try {  
 166.             f = this.getSessionBean1().getGerenteFuncionario().getFuncionarioPorIs(id);  
 167.             txtLogin.setValue(f.getLogin());  
 168.             txtSenha.setValue(f.getSenha());  
 169.             txtNome.setValue(f.getNome());  
 170.         } catch (Exception ex) {  
 171.             msgErro.setValue(new String("Exception"));  
 172.         }  
 173.     }  
 174.   
 175.     public boolean existeId(Integer id) {  
 176.         return true;  
 177.     }  
 178. }

Obrigado

3 Respostas

Gu1lherme

Estou desconfiado que meu “actionListener” não está sendo executado, e por isso meus componentes não estão sendo atualizados. Aguém tem uma solução para meu problema???

Gu1lherme

Fiz alguns testes e meu actionListener só n funciona dentro da minha tabela (h:dataTable). Aguém sabe o que pode estar acontecendo???

Gu1lherme

Problema resolvido… da uma olhada na solução ai, pode te ajudar um dia…
http://www.urubatan.com.br/jsf_commandlink_datatable_problemas/

Criado 11 de julho de 2008
Ultima resposta 18 de jul. de 2008
Respostas 3
Participantes 1