Fórum Como editar objetos com openfaces? #574098
22/01/2017
0
Olá, estou fazendo um trabalho em que preciso utilizar o Openfaces como interface do jsf, estou com dificuldades na parte de editar um objeto que já esta salvo no banco, segue como está meu código:
Tela Setor:
SetorBean
Minha maior dificuldade está em como fazer o <o:window> pegar o objeto respectivo da tabela...
Tela Setor:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:o="http://openfaces.org/">
<h:head>
<h:outputStylesheet library="css" name="menuStyle.css" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Setor</title>
</h:head>
<h:body
style="font-size:20px; background-image:url(/cartaoPonto/resources/imagens/fundoPages.jpg)">
<div align="center">
<ui:include src="/Template/menu.xhtml"></ui:include>
<h:form id="form">
<o:dataTable horizontalGridLines="1px dotted gray"
bodySectionStyle="background-color:#D3D3D3; color:#106574"
verticalGridLines="1px dotted gray" id="tabela" width="100%"
var="setor" value="#{setorBean.setores}" pageSize="20">
<f:facet name="below">
<o:dataTablePaginator id="paginator" />
</f:facet>
<o:column id="nome" sortingExpression="#{setor.nome}">
<f:facet name="header">
<h:outputText value="Nome" />
</f:facet>
<f:facet name="subHeader">
<o:inputTextFilter for="tabela" expression="#{setor.nome}" />
</f:facet>
<h:outputText value="#{setor.nome}" />
</o:column>
<o:column>
<f:facet name="header">
<h:outputText value="Opções" />
</f:facet>
<o:commandButton onclick="O$('form:editar').show();">
<h:graphicImage url="/resources/imagens/editar.png" />
<f:attribute name="setorSelecionado" value="#" />
</o:commandButton>
<o:commandButton id="btExcluir"
actionListener="#{setorBean.excluir}">
<o:confirmation message="Deseja mesmo excluir o item?"
details="Clique em sim para excluir" okButtonText="Sim"
cancelButtonText="Não" event="click" />
<h:graphicImage url="/resources/imagens/excluir.jpg" />
<f:attribute name="setorSelecionado" value="#" />
</o:commandButton>
</o:column>
</o:dataTable>
<input type="button" value="Novo Setor"
onclick="O$('form:window').show();" />
<o:window id="window" modal="true" height="60px" width="270px"
top="450" left="850"
modalLayerStyle="background-color: silver; opacity:0.3;">
<f:facet name="caption">
<h:outputText value="Cadastre um novo setor!" />
</f:facet>
<h:outputText value="Nome: " />
<o:inputText value="#{setorBean.setor.nome}" />
<o:commandButton action="#{setorBean.salvar}">
<h:graphicImage url="/resources/imagens/salvar.png" />
</o:commandButton>
</o:window>
</h:form>
</div>
</h:body>
</html>SetorBean
package br.edu.cartaoPontoOpen.bean;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;
import org.omnifaces.util.Messages;
import br.edu.cartaoPontoOpen.dao.SetorDAO;
import br.edu.cartaoPontoOpen.domain.Setor;
@SuppressWarnings("serial")
@ManagedBean
@ViewScoped
public class SetorBean implements Serializable {
private Setor setor;
public Setor getSetor() {
return setor;
}
public void setSetor(Setor setor) {
this.setor = setor;
}
public List<Setor> getSetores() {
return setores;
}
public void setSetores(List<Setor> setores) {
this.setores = setores;
}
private List<Setor> setores;
public void novo(){
setor = new Setor();
}
@PostConstruct
public void listar() {
setor = new Setor();
try {
SetorDAO setorDAO = new SetorDAO();
setores = setorDAO.listar();
} catch (RuntimeException erro) {
Messages.addGlobalError("Ocorreu um erro ao carregar os setor!!!");
erro.printStackTrace();
}
}
public void salvar() {
try {
System.out.println("cheguei aqui");
SetorDAO setorDAO = new SetorDAO();
setorDAO.salvar(setor);
setor = new Setor();
setores = setorDAO.listar();
Messages.addGlobalInfo("Setor salvo com sucesso");
} catch (RuntimeException erro) {
Messages.addGlobalError("Ocorreu um erro ao tentar salvar");
erro.printStackTrace();
}
}
public void editar(ActionEvent evento){
setor = (Setor) evento.getComponent().getAttributes().get("setorSelecionado");
System.out.println(setor.getCodigo() + setor.getNome());
}
public void excluir(ActionEvent evento){
try{
System.out.println("cheguei no excluir");
setor = (Setor) evento.getComponent().getAttributes().get("setorSelecionado");
SetorDAO setorDAO = new SetorDAO();
setorDAO.excluir(setor);
setores = setorDAO.listar();
Messages.addGlobalInfo("Setor removido com suscesso!");
}catch (RuntimeException erro) {
Messages.addFlashGlobalError("Ocorreu um erro ao excluir");
erro.printStackTrace();
}
}
}Minha maior dificuldade está em como fazer o <o:window> pegar o objeto respectivo da tabela...
Fernando Anjos
Curtir tópico
+ 0
Responder
Clique aqui para fazer login e interagir na Comunidade :)