1
resposta

ViewParam e ViewAction não atualizam a tela usando parâmetro

<?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:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://xmlns.jcp.org/jsf/core">
    <!-- xmlns:f="http://java.sun.com/jsf/core" -->

<ui:composition template="_template.xhtml">
    <f:metadata>
        <f:viewParam name="autorId" value="#{autorBean.autorId}" />
        <f:viewAction action="#{autorBean.carregarAutorPelaId}" />
    </f:metadata>
    <ui:define name="titulo">Novo Autor</ui:define>
    <ui:define name="conteudo">
        <h:form id="autor">
            <fieldset>
                <legend>Dados do Autor</legend>
                <h:panelGrid columns="3">
                    <h:outputLabel value="Nome:" for="nome" />
                    <h:inputText id="nome" value="#{autorBean.autor.nome}"
                        required="true">
                        <f:validateLength minimum="5" />
                    </h:inputText>
                    <h:message for="nome" />
                    <h:commandButton value="Gravar" action="#{autorBean.gravar}" />
                </h:panelGrid>
            </fieldset>
        </h:form>
    </ui:define>
    <ui:define name="texto">
      Cadastro de Autores
    </ui:define>
</ui:composition>

</html>
package br.com.caelum.livraria.bean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import br.com.caelum.livraria.dao.DAO;
import br.com.caelum.livraria.modelo.Autor;

@ViewScoped
@ManagedBean
public class AutorBean {

    private Autor autor = new Autor();

    private Integer autorId;

    public Integer getAutorId() {
        return autorId;
    }

    public void setAutorId(Integer autorId) {
        this.autorId = autorId;
    }

    public Autor getAutor() {
        return autor;
    }

    public void carregarAutorPelaId() {
        this.autor = new DAO<Autor>(Autor.class).buscaPorId(autorId);
    }

    public String gravar() {
        System.out.println("Gravando autor " + this.autor.getNome());

        new DAO<Autor>(Autor.class).adiciona(this.autor);
        this.autor = new Autor();

        return "livro?faces-redirect=true";
    }
}
No console não mostra nenhum erro:
FASE: RESTORE_VIEW 1
FASE: RENDER_RESPONSE 6
FASE: RESTORE_VIEW 1
FASE: RENDER_RESPONSE 6
FASE: RESTORE_VIEW 1
FASE: RENDER_RESPONSE 6
1 resposta

Oi Leonardo,

Você entrou na página passando o parametro na url?

Tipo: autor.xhtml?autorId=2

Coloque um System.out.println nos métodos setAutorId e carregarAutorPelaId, para ver se eles estão sendo chamados.