2
respostas

Problema para recuperar os dados de um Bean em outra página

Pessoal, ao digitar o valor na página Usuario.xhtml que está mapeado por um bean, e enviar os dados para a página mostrausuario.xhtml, nenhuma informação está aparecendo.

Alguém ajuda? Abaixo estão os códigos.

Usuario.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">

<h:body>
    <h:form>
        <fieldset>
            <legend>Cadastrar Usuário</legend>
            <h:panelGrid columns="2">
                <h:outputLabel value="Nome:" for="nome" />
                <h:inputText id="nome" value="#{usuarioBean.nome}" required="true" requiredMessage="Nome obrigatório" />
                <h:outputLabel value="Sobrenome:" for="sobrenome" />
                <h:inputText id="sobrenome" value="#{usuarioBean.sobrenome}" required="true" requiredMessage="Sobrenome obrigatório"/>
                <h:outputLabel value="E-mail:" for="email" />
                <h:inputText id="email" value="#{usuarioBean.email}" required="true" requiredMessage="E-mail obrigatório" />
                <h:outputLabel value="Senha:" for="senha" />
                <h:inputSecret id="senha" value="#{usuarioBean.senha}"/>
                <h:outputLabel value="Confirmar Senha:" for="confirmarsenha" />
                <h:inputSecret id="confirmarsenha" value="#{usuarioBean.confirmaSenha}"/>
            </h:panelGrid>
            <h:messages />

            <h:commandButton action="#{usuarioBean.salvar}" value="Enviar" />
        </fieldset>
    </h:form>
</h:body>
</html>

UsuarioBean.java

package br.edu.ifal.academico.bean;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name = "usuarioBean", eager=true)
@RequestScoped
public class UsuarioBean {
    private String nome;
    private String sobrenome;
    private String email;
    private String senha;
    private String confirmaSenha;

    public String getNome() {
        return this.nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getSobrenome() {
        return this.sobrenome;
    }

    public void setSobrenome(String sobrenome) {
        this.sobrenome = sobrenome;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getSenha() {
        return this.senha;
    }

    public void setSenha(String senha) {
        this.senha = senha;
    }

    public String getConfirmaSenha() {
        return this.confirmaSenha;
    }

    public void setConfirmaSenha(String confirmaSenha) {
        this.confirmaSenha = confirmaSenha;
    }

    public String salvar() {
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Gravando usuario: " + this.getNome(), "Gravando usuario: " + this.getNome()));
        System.out.println("Gravando usuario: " + this.getNome());
        return "mostrausuario" + "?faces-redirect=true";
    }
}

mostrausuario.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://xmlns.jcp.org/jsf/core">

<h:head>
    <title>Usuário cadastrado</title>
</h:head>

<h:body>
    <h1>Usuário Cadastrado</h1>

    Nome:<h:outputText value="#{usuarioBean.nome}"/>
    Sobrenome:<h:outputText value="#{usuarioBean.sobrenome}"/>

</h:body>

</html>

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
    version="2.2">

    <navigation-rule>
        <from-view-id>usuario.xhtml</from-view-id>
        <navigation-case>
            <from-action>#{usuarioBean.salvar}</from-action>
            <from-outcome>mostrausuario</from-outcome>
            <to-view-id>mostrausuario.xhtml</to-view-id>
            <redirect />
        </navigation-case>
    </navigation-rule>

</faces-config>
2 respostas

Acho que o problema pode estar no @RequestScoped. Já tentou usar o @ViewScoped?

Pessoal, vi uma solução utilizando o Flash Scope: http://javabeat.net/jsf-2-flash-scope/

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software