Para alterar os dados não carregam no inputText, e também a remoção não é realizada.
AutorBean:
@ManagedBean
@ViewScoped
public class AutorBean {
private Autor autor = new Autor();
public Autor getAutor() {
return autor;
}
public RedirectView gravar() {
System.out.println("Gravando autor " + this.autor.getNome());
if(this.autor.getId() == null){
new DAO<Autor>(Autor.class).adiciona(this.autor);
this.autor = new Autor();
} else{
new DAO<Autor>(Autor.class).atualiza(this.autor);
}
return new RedirectView("livro");
}
public List<Autor> getAutores() {
return new DAO<Autor>(Autor.class).listaTodos();
}
public void carregar(Autor autor){
this.autor = autor;
System.out.println("nome: " + autor.getNome());
}
public void remover(Autor autor){
new DAO<Autor>(Autor.class).remove(autor);
}
}
autor.xhtml:
<?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://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<ui:composition template="_template.xhtml">
<ui:define name="titulo">
Novo Autor
</ui:define>
<ui:define name="conteudo">
<h:form>
<fieldset>
<legend>Dados do Autor</legend>
<h:panelGrid columns="2">
<h:outputLabel value="Nome:" for="nome" />
<h:inputText id="nome" value="#{autorBean.autor.nome}" />
<h:commandButton value="Gravar" action="#{autorBean.gravar}" />
</h:panelGrid>
</fieldset>
<fieldset>
<h:form id="formTabelaAutores">
<legend>Autores Cadastrados</legend>
<h:dataTable value="#{autorBean.autores}" var="autor"
id="tabelaAutores">
<h:column>
<f:facet name="header">ID do Autor</f:facet>
<h:outputText value="#{autor.id}" />
</h:column>
<h:column>
<f:facet name="header">Nome do Autor</f:facet>
<h:outputText value="#{autor.nome}" />
</h:column>
<h:column>
<f:facet name="header">Alterar</f:facet>
<h:commandLink value="Alterar"
action="#{autorBean.carregar(autor)}"></h:commandLink>
</h:column>
<h:column>
<f:facet name="header">Remover</f:facet>
<h:commandLink value="Remover"
action="#{autorBean.remover(autor)}"></h:commandLink>
</h:column>
</h:dataTable>
</h:form>
</fieldset>
</h:form>
</ui:define>
<ui:define name="texto">
Cadastro de Autores
</ui:define>
</ui:composition>
</html>