Bean does not show parameters sent from DTO

1

I am programming a mini form, the question is that I have 2 user and institute beans, the user logs in and enters an index where he has to fill out a form, in index I want to show both the user's name and the institute that it logs both in an outpuLabel only that the value of institute does not show it to me ...

  

Bean User

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import dao.usuarioDAO;
import java.sql.SQLException;
import sesion.SessionUtils;

@Named(value = "usuario")
@SessionScoped
public class usuarioDTO implements Serializable {

public String getId_usuario(){
    return id_usuario;
}

public void setId_usuario(String id_usuario) {
    this.id_usuario = id_usuario;
}

public String getUsuario() {
    return usuario;
}

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

public String getContraseña() {
    return contraseña;
}

public void setContraseña(String contraseña) {
    this.contraseña = contraseña;
}

public String getId_instituto() {
    return id_instituto;
}

public void setId_instituto(String id_instituto) {
    this.id_instituto = id_instituto;
}

public String getId_rol() {
    return id_rol;
}

public void setId_rol(String id_rol) {
    this.id_rol = id_rol;
}

private String id_usuario;
private String usuario;
private String contraseña;
private String id_instituto;
private boolean loggedIn = false;
private String id_rol;

public boolean isLoggedIn() {
    return loggedIn;
}

public void setLoggedIn(boolean loggedIn) {
    this.loggedIn = loggedIn;
}
/**
 * Creates a new instance of usuario
 */
public usuarioDTO() {
}

public String inicioSesion() throws SQLException{
    boolean validar = usuarioDAO.validarSesion(usuario, contraseña);
    FacesMessage message = null;
    String username;
    institutoDTO dto = new institutoDTO();
    if(validar){
        HttpSession session = SessionUtils.getSession();
        session.setAttribute("usuario", usuario);
        username = (String)session.getAttribute("usuario");
        dto.setNombre(username);
        return "index";
    } else{
        message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Error de Logueo", "Usuario o Contraseña incorrecto");
        return "login";
    }
}


public String cerrarSesion(){
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
    session.invalidate();
    loggedIn = false;
    return "login";
}

}

  

Bean Institute

package dto;

import conexion.conectar;
import javax.inject.Named;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.enterprise.context.SessionScoped;


@Named(value = "instituto")
@SessionScoped
public class institutoDTO implements Serializable {

/**
 * Creates a new instance of institutoDTO
 */
public institutoDTO() {
}

private String id_instituto;
private String nombre;

public String getId_instituto() {
    return id_instituto;
}

public void setId_instituto(String id_instituto) {
    this.id_instituto = id_instituto;
}

public String getNombre() { 
    return nombre;
}

/*public void setNombre(String nombre) {
    this.nombre = nombre;
}*/

public void setNombre(String user)throws SQLException{
            Connection con = null;
    PreparedStatement ps = null;
            conectar conexion = new conectar();
            con = conexion.conectar();
            String instituto = null;
            try{
                ps = con.prepareStatement("SELECT NOMBRE "
                                        + "FROM INSTITUTO "
                                        + "INNER JOIN USUARIO ON USUARIO.ID_INSTITUTO = INSTITUTO.ID_INSTITUTO "
                                        + "WHERE USUARIO = ?");
                ps.setString(1, user);
                ResultSet rs = ps.executeQuery();

                if (rs.next()){
                    instituto = rs.getString(1);
                }   
            }catch(SQLException ex){
                System.out.println("Login error -->" + ex.getMessage());
            }
            System.out.println(""+instituto);
            nombre = instituto;             
}   
}
  

Here the login .xhtml

<h:body style="margin: 0 auto; text-align: center;">
    <p:growl id="mensajes" showDetail="true" life="2000" />
    <h:form>
    <p:panel header="Inicio de Sesión" style="position: relative; width: 25%; border-width: 2px; margin: 0 auto; text-align: center;">
            <p:panelGrid style="margin: 0 auto; text-align: center; width: 25%; margin-bottom: 0;">
                <p:row>   
                    <p:column>
                        <p:inputText required="true"
                                     requiredMessage="Por favor ingresar usuario"
                                     placeholder="Usuario"
                                     value="#{usuario.usuario}"> </p:inputText>
                    </p:column>
                </p:row>

                <p:row>
                    <p:column>
                        <p:password required="true"
                                    validatorMessage="Contraseña incorrecta"
                                    placeholder="Contraseña"
                                    value="#{usuario.contraseña}"></p:password>
                    </p:column>
                </p:row>
            </p:panelGrid>
            <f:facet name="footer" >
                <div align="center">
                    <p:commandButton value="Acceder&#187;"
                                     action="#{usuario.inicioSesion()}">
                    </p:commandButton>    
                </div>
            </f:facet>
        </p:panel>
        </h:form>
</h:body>

  

Code of the index.xhtml

<p:toolbar style="background: #004a93; color: #f5f5f5;">
        <f:facet name="left">
                        Bienvenido: <p:outputLabel value="#{usuario.usuario}"></p:outputLabel>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        Instituto: <p:outputLabel value="#{instituto.nombre}"></p:outputLabel>
        </f:facet>
        <f:facet name="right">
                        <h:commandLink value="Cerrar Sesión"
                                       style="background: #004a93; color: #f5f5f5;"
                                       action="#{usuario.cerrarSesion()}"></h:commandLink>
        </f:facet>
    </p:toolbar>
    
asked by runjavcos 12.09.2018 в 21:35
source

1 answer

0
  

institutoDTO dto = new institutoDTO ();

The managed beans must be created and managed by the framework that controls them (in this case CDI); The beans that you instantiate on your own will simply be out of the control of the framework.

Thus, you are creating an instance of institutoDto 1 to which you assign data, but that instance disappears when you exit the method. When you do #{institutoDto} you are accessing an instance managed by CDI, different 2 .

If you need a managed instance from another managed instance, just ask the framework to inject it; for example:

public class usuarioDTO implements Serializable {

   @Inject
   private institutoDto instituto;

   ...
}

There are more forms and it has its rules and restrictions, I advise you to review some tutorial.

As a final note, I recommend that you generally make the setters and getters of the beans as "stupid" as possible; apart from some validation (and when you can, use javax.validation instead of code) do not logically. You can add logical beans by methods other than setters or getters (for example, a cargarDatos method). The getters and the setters are called automatically when processing the JSFs, the more logic you have there the longer it will take.

In addition, making the beans "dumb" and letting the logic be in separate classes facilitates the reuse of code, since your beans do not depend on other layers (for example, SQL).

1 Note: It would be nice to review the Java naming guides (for example, class names start with a capital letter). Once you've got used to it (and 99% of programmers get used to it) it becomes very rare to read code that does not follow the rules.

2 You can check it by putting a log in the constructor of institutoDto , for example.

    
answered by 12.09.2018 / 23:10
source