0

How I can pass params to another view using JSF? Will explain:

All begin in index.xhtml, index use the usuarioController (I called usuarioBean)

<f:metadata>
   <f:viewParam name="id" value="#{usuarioBean.usuario.id}" />
   <f:viewParam name="nome" value="#{usuarioBean.usuario.nome}" />
</f:metadata>
<h:form>    
<h:dataTable value="#{usuarioBean.listarUsuarios()}" var="usuario">
            <h:column>
                <f:facet name="header">Id</f:facet>
                <h:outputText value="#{usuario.id}"></h:outputText>
            </h:column>

            <h:column>
                <f:facet name="header">Nome</f:facet>
                <h:outputText value="#{usuario.nome}"></h:outputText>
            </h:column>

            <h:column>
                <f:facet name="header">Opcoes</f:facet>                   
                <h:link value="Cadastrar Endereco" outcome="endereco?faces-redirect=true" includeViewParams="true" />
            </h:column>
        </h:dataTable>
    </h:form>

My controller is basic:

@ViewScoped
@ManagedBean(name="usuarioBean")
public class UsuarioController implements Serializable {

    @Inject
    private EntityManager manager;
    private Usuario usuario;

    public UsuarioController(){
        this.usuario = new Usuario();
    }

    public List<Usuario> listarUsuarios(){
        return manager.createNativeQuery("SELECT * FROM usuario", Usuario.class).getResultList();
    }
    (...)

On click "Cadastrar Endereco" I want call the page endereco.xhtml (it use EnderecoController, called of enderecoBean) and I want get the params id and nome, but I'm having trouble getting these parameters.

In endereco.xhtml, I have:

<f:metadata>
            <f:viewParam name="id" value="#{enderecoBean.usuario.id}" />
            <f:viewParam name="nome" value="#{enderecoBean.usuario.nome}" />
        </f:metadata>

Id: <h:inputText value="#{enderecoBean.usuario.id}" />
        <br />
        Nome: <h:inputText value="#{enderecoBean.usuario.nome}" />
        <br />

And my controller too is basic:

@ViewScoped
@ManagedBean(name="enderecoBean")
public class EnderecoController implements Serializable {

    @Inject
    private EntityManager manager;
    private Endereco endereco;
    private Usuario usuario;

    public EnderecoController(){
        this.endereco = new Endereco();
    }

    public List<Endereco> listarEnderecos(){
        return manager.createNativeQuery("SELECT * FROM usuario", Endereco.class).getResultList();
    }

I need recovery this params because I need save enderecos, and enderecos is dependent of usuario.

For more information, my code is here: github.com/eltonsantos/embeddedClasses

Robert Deml
  • 12,390
  • 20
  • 65
  • 92
Elton Santos
  • 571
  • 6
  • 32
  • Maybe I need use just ONE controller instead of TWO controllers? – Elton Santos Oct 27 '17 at 16:17
  • Where is _usuario_ initialized in _EnderecoController_? You can not bind view parameters to uninitialized objects. As a side note: You do not need to append `faces-redirect=true` to the _outcome_ of your `h:link`. It will allready end up in a fresh GET request of the target. Also consider using a [_@PostConstruct_](https://docs.oracle.com/javaee/7/api/javax/annotation/PostConstruct.html) annotated method to initialize your controller instead of the constructor especially if you are using CDI. See [this question](https://stackoverflow.com/questions/3406555/why-use-postconstruct). – irieill Nov 01 '17 at 16:37

0 Answers0