Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未在JSF页面上显示Bean值_Jsf_Jsf 2_Managed Bean_Backing Beans - Fatal编程技术网

未在JSF页面上显示Bean值

未在JSF页面上显示Bean值,jsf,jsf-2,managed-bean,backing-beans,Jsf,Jsf 2,Managed Bean,Backing Beans,这就是我努力实现的目标: 1) 当用户尝试使用其用户ID/密码登录时,将根据我的数据库表验证这些凭据,以确保用户有效。 2) 当验证通过时,我检索有关用户的一些详细信息,并在登录后立即在JSF页面中显示这些值 这两个过程都发生在用户输入用户名/密码并点击提交时的一次单击(ie)上 我所做的: 当用户在登录时点击submit时,我使用一个查询来检查bean中名为“login”(managedbean名称)的表中的值。当值出现在表中时,我使用另一个查询来检索用户的其他信息,并将这些值填充到另一个名为

这就是我努力实现的目标:

1) 当用户尝试使用其用户ID/密码登录时,将根据我的数据库表验证这些凭据,以确保用户有效。 2) 当验证通过时,我检索有关用户的一些详细信息,并在登录后立即在JSF页面中显示这些值

这两个过程都发生在用户输入用户名/密码并点击提交时的一次单击(ie)上

我所做的:

当用户在登录时点击submit时,我使用一个查询来检查bean中名为“login”(managedbean名称)的表中的值。当值出现在表中时,我使用另一个查询来检索用户的其他信息,并将这些值填充到另一个名为“fields”的bean中的setter方法中。现在,使用faces重定向,我传递了名为“userindex.xhtml”的JSF页面的名称。现在,我只是尝试访问要显示在userindex页面中的“fields”bean的getter方法

前两个查询成功运行。唯一的问题似乎是当我试图从“userindex”页面访问“fields”bean对象的值时,该对象被初始化/重新创建。如果我错了,请纠正我

总而言之,我使用bean“login”通过后端表验证用户输入值,从登录bean访问另一个名为“fields”的bean来设置所有与用户相关的信息,并使用这些“fields”值填充“userindex”页面

下面给出了代码片段:

登录页面:

    <h:form style="margin:auto;width:90%;height:98%;text-align:left; background-color:  whitesmoke; border-top-style: outset   ">
            <p:panelGrid columns="2" style="margin-left:400px">
                <h:outputLabel for="npi" value="Enter your NPI: *" />  
                <p:inputText id="npi" value="#{login.contactid}" label="NPI" />  

                <h:outputLabel for="pwd" value="Password: *" />  
                <p:password id="pwd" value="#{login.pwd}" required="true" label="password"/>  


                <f:facet name="footer">  
                    <p:commandButton id="prologin" value="Login" action="#{login.checkUser()}" />  

                </f:facet>  

            </p:panelGrid>
   </h:form>
package com.superlist;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;


@ManagedBean(name = "login")
@SessionScoped 

public class UserBean {


@ManagedProperty(value = "#{fields}")
private ProviderFieldsBean fields;

public void setFields(ProviderFieldsBean fields) {
    this.fields = fields;

}

private String contactid;
private String pwd;


@Resource(name = "jdbc/mysuperlist")
private DataSource ds;

PreparedStatement searchQuery, query = null;
Connection conn = null;
ResultSet rs, rs1;

public void UserBean() {
    try {
        Context ctx = new InitialContext();
        ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysuperlist");
    } catch (NamingException e) {
        e.printStackTrace();
    } 
}

/**
 * @return the firstname
 */
public String logout() {
    FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
    return "index.xhtml?faces-redirect=true";
}

/**
 * @return the pwd
 */
public String getPwd() {
    return pwd;
}

/**
 * @param pwd the pwd to set
 */
public void setPwd(String pwd) {
    this.pwd = pwd;
}

/**
 * @return the contactID
 */
public String getContactid() {
    return contactid;
}

/**
 * @param contactID the contactID to set
 */
public void setContactid(String contactid) {
    this.contactid = contactid;
}

public String checkUser() throws SQLException {


    System.out.println("inside check provider");
    String url;
    int rowcount = 0;

 try {

        conn = ds.getConnection();
        String q = "Select npi from providerlogin where npi =" + "'" + contactid + "'"
                + " and password=" + "'" + pwd + "'";

        System.out.println("query is " + q);
        searchQuery = conn.prepareStatement(q);
        rs = searchQuery.executeQuery();

        rs.last();
        rowcount = rs.getRow();
        rs.beforeFirst();
        System.out.println("total no of rows is " + rowcount);


        if (rowcount > 0) {
            String q1 = "select ContactID, FirstName,LastName,Email,Phone from fulltable where ContactID = "
                    + "'" + contactid + "'";
            query = conn.prepareStatement(q1);
            System.out.println("the query is " + q1);
            rs1 = query.executeQuery();

   while(rs1.next())
    {
                        fields.setAll(rs1.getString(2),rs1.getString(3),rs1.getString(4),rs1.getString(5));

    }

}
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            conn.close();
            searchQuery.close();
            query.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    if (rowcount > 0) {
        System.out.println("rowcount > 0");
            url = "userindex?faces-redirect=true";
    } 
    else {
        System.out.println("rowcount = 0");
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "User Error", "Invalid creditientials"));
        url = "login?faces-redirect=true";
    }

    return url;
}
 package com.superlist;

 import java.io.Serializable;
 import java.sql.SQLException;
 import javax.faces.bean.ManagedBean;
 import javax.faces.bean.ManagedProperty;
 import javax.faces.bean.SessionScoped; 

 @ManagedBean(name="fields")
 @SessionScoped  

 public class ProviderFieldsBean{
  private String firstname;
  private String lastname;
  private String contactid;
  private String phone;
  private String email;  


 public ProviderFieldsBean(){

}

public  void setAll(String firstname, String lastname, String email, String phone) 
{

    this.firstname = firstname;
    this.lastname = lastname;
    this.email = email;
    this.phone = phone;
   }

public String getFirstname() {
    return firstname;
}

public void setFirstname(String firstname) {
    this.firstname = firstname;
}

public String getLastname() {
    return lastname;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}


public String getPhone() {
    return phone;
}


public void setPhone(String phone) {
    this.phone = phone;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
} 
}
字段bean:

    <h:form style="margin:auto;width:90%;height:98%;text-align:left; background-color:  whitesmoke; border-top-style: outset   ">
            <p:panelGrid columns="2" style="margin-left:400px">
                <h:outputLabel for="npi" value="Enter your NPI: *" />  
                <p:inputText id="npi" value="#{login.contactid}" label="NPI" />  

                <h:outputLabel for="pwd" value="Password: *" />  
                <p:password id="pwd" value="#{login.pwd}" required="true" label="password"/>  


                <f:facet name="footer">  
                    <p:commandButton id="prologin" value="Login" action="#{login.checkUser()}" />  

                </f:facet>  

            </p:panelGrid>
   </h:form>
package com.superlist;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;


@ManagedBean(name = "login")
@SessionScoped 

public class UserBean {


@ManagedProperty(value = "#{fields}")
private ProviderFieldsBean fields;

public void setFields(ProviderFieldsBean fields) {
    this.fields = fields;

}

private String contactid;
private String pwd;


@Resource(name = "jdbc/mysuperlist")
private DataSource ds;

PreparedStatement searchQuery, query = null;
Connection conn = null;
ResultSet rs, rs1;

public void UserBean() {
    try {
        Context ctx = new InitialContext();
        ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysuperlist");
    } catch (NamingException e) {
        e.printStackTrace();
    } 
}

/**
 * @return the firstname
 */
public String logout() {
    FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
    return "index.xhtml?faces-redirect=true";
}

/**
 * @return the pwd
 */
public String getPwd() {
    return pwd;
}

/**
 * @param pwd the pwd to set
 */
public void setPwd(String pwd) {
    this.pwd = pwd;
}

/**
 * @return the contactID
 */
public String getContactid() {
    return contactid;
}

/**
 * @param contactID the contactID to set
 */
public void setContactid(String contactid) {
    this.contactid = contactid;
}

public String checkUser() throws SQLException {


    System.out.println("inside check provider");
    String url;
    int rowcount = 0;

 try {

        conn = ds.getConnection();
        String q = "Select npi from providerlogin where npi =" + "'" + contactid + "'"
                + " and password=" + "'" + pwd + "'";

        System.out.println("query is " + q);
        searchQuery = conn.prepareStatement(q);
        rs = searchQuery.executeQuery();

        rs.last();
        rowcount = rs.getRow();
        rs.beforeFirst();
        System.out.println("total no of rows is " + rowcount);


        if (rowcount > 0) {
            String q1 = "select ContactID, FirstName,LastName,Email,Phone from fulltable where ContactID = "
                    + "'" + contactid + "'";
            query = conn.prepareStatement(q1);
            System.out.println("the query is " + q1);
            rs1 = query.executeQuery();

   while(rs1.next())
    {
                        fields.setAll(rs1.getString(2),rs1.getString(3),rs1.getString(4),rs1.getString(5));

    }

}
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            conn.close();
            searchQuery.close();
            query.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    if (rowcount > 0) {
        System.out.println("rowcount > 0");
            url = "userindex?faces-redirect=true";
    } 
    else {
        System.out.println("rowcount = 0");
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "User Error", "Invalid creditientials"));
        url = "login?faces-redirect=true";
    }

    return url;
}
 package com.superlist;

 import java.io.Serializable;
 import java.sql.SQLException;
 import javax.faces.bean.ManagedBean;
 import javax.faces.bean.ManagedProperty;
 import javax.faces.bean.SessionScoped; 

 @ManagedBean(name="fields")
 @SessionScoped  

 public class ProviderFieldsBean{
  private String firstname;
  private String lastname;
  private String contactid;
  private String phone;
  private String email;  


 public ProviderFieldsBean(){

}

public  void setAll(String firstname, String lastname, String email, String phone) 
{

    this.firstname = firstname;
    this.lastname = lastname;
    this.email = email;
    this.phone = phone;
   }

public String getFirstname() {
    return firstname;
}

public void setFirstname(String firstname) {
    this.firstname = firstname;
}

public String getLastname() {
    return lastname;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}


public String getPhone() {
    return phone;
}


public void setPhone(String phone) {
    this.phone = phone;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
} 
}
用户索引页

      <h:form style="margin:auto;width:90%;height:98%;text-align:left; background-color: whitesmoke; border-top-style: outset">

         <h:panelGrid columns="2" style="margin-left:350px;">  
            <f:facet name="header">  
                Your details
            </f:facet>  

            <h:outputLabel for="firstname" value="Firstname: *" />  
            <h:outputText id="firstname" value="#{fields.firstname}" />  

            <h:outputLabel for="surname" value="Surname: *" />  
            <h:outputText id="surname" value="#{fields.lastname}"/>  

            <h:outputLabel for="email" value="Email: *" />  
            <h:outputText id="email" value="#{fields.email}"/>  
            <f:facet name="footer">  
                <p:commandButton type="button" value="Update!" icon="ui-icon-check" style="margin:0"/>  
            </f:facet>  
        </h:panelGrid>  

    </h:form>

你的细节

请让我知道如何进行这项工作。非常感谢您的帮助。提前谢谢

正如@MrJ4mes先生所说,这里有一个
setter
用于注入的托管bean
ProviderFieldsBean字段
以提供通过视图的访问。

我猜ProviderFieldBean是一个模型类。如果是这样,它应该用@table注释。它不应该有会话范围或managedbean注释。

我们需要更多托管bean的代码。特别是范围。谢谢你的评论。我已经为这两个bean添加了完整的代码。你能尝试为你的
ProviderFieldsBean
字段添加一个getter吗?J4mes先生:我在ProviderFieldsBean中有getter方法。请你详细说明你想说什么好吗。谢谢你的关注。