Why does? with-faces-redirect = true change the value?

0

Good, the issue is that when entering the name in the login.xhtml the attribute "user" takes the value that I enter in the inputText, but when opening the "welcome.xhtml" it changes to null. For me to pass the value correctly I had to remove "? Faces-redirect = true" from the url and I do not understand why.

login.xhtml

<ui:composition template="/WEB-INF/templates/BasicTemplate.xhtml">
    <ui:define name="content">
        <h:form>
            <h:panelGrid columns="2">
                <h2><h:outputText value="${lbl.lbl_Usuario}"></h:outputText></h2>
                <p><h:inputText value="${loginBean.usuario}"></h:inputText></p>
                <h2><h:outputText value="${lbl.lbl_Password}"></h:outputText></h2>
                <p><h:inputSecret value="${loginBean.password}"></h:inputSecret></p>
            </h:panelGrid>
            <h:panelGroup>
                <h:outputText value="${lbl.lbl_EresNuevo} "></h:outputText>
                <h:commandButton value="${lbl.lbl_Registrate}" action="${loginBean.usunuevo}"></h:commandButton>
            </h:panelGroup>
            <br />
            <br />
            <h:commandButton value="Login" action="${loginBean.logusuario}"></h:commandButton>
        </h:form>
    </ui:define>
</ui:composition>

LoginBean.java

package com.tutorial.beans;

import java.io.Serializable;
import java.util.ArrayList;

import javax.faces.bean.ManagedBean;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;


@Component
@ManagedBean
@Scope("session")
public class LoginBean implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String usuario;
    private String pass;
    private boolean intento;


    public String getUsuario (){
        return usuario;
    }


    public void setUsuario (String usuario){
        this.usuario = usuario;
    }


    public String getPassword (){
        return pass;
    }


    public void setPassword (String password){
        this.pass = password;
    }


    public boolean getIntento() {
        return intento;
    }


    public void setIntentos(boolean intento) {
        this.intento = intento;
    }

    public String logearse(){
        String url = "welcome?faces-redirect=true";
        return url;
    }
    public String usunuevo(){
        String url = "nuevousu?faces-redirect=true";
        return url;
    }

    public String logusuario(){
        String url;
        ArrayList<NuevoUsuBean> users = NuevoUsuBean.getUsers();
        for(int x=0;x<users.size();x++) {
              System.out.println(users.get(x));
        }

        for(NuevoUsuBean usu:users){
            if(usu.getUser().equals(usuario) && usu.getPassword().equals(pass)){
                url = "welcome?faces-redirect=true";
                System.out.println(getUsuario());
                return url;
            }
        }
        url = "login?faces-redirect=true";
        return url;
    }

welcome.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">

<ui:composition template="/WEB-INF/templates/BasicTemplate.xhtml">
    <ui:define name="content">
            <h:outputLabel value="Bienvenido, ${loginBean.usuario}"></h:outputLabel>
    </ui:define>
</ui:composition>
</html>
    
asked by Aitor Braojos 06.04.2017 в 09:23
source

1 answer

0

In the end I managed to solve it by adding my variable to a Flash Scope. This way I can redirect my page and not lose the value of the variable. It seems that in the other way when doing the redirect I could not access the previous page, therefore when trying to access the value I returned null.

* This block got into the for each of the LoginBean:

Flash fScope = FacesContext.getCurrentInstance().getExternalContext().getFlash();
fScope.put("usuario",usuario);
    
answered by 20.04.2017 / 08:25
source