Bean值未显示在JSF页面上

时间:2013-09-08 17:12:11

标签: jsf jsf-2 managed-bean backing-beans

这就是我想要实现的目标:

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

当用户输入用户名/密码并点击提交时,这两个过程都会在单击(即)时发生。

我做了什么:

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

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

总而言之,我使用bean“login”来验证用户输入值和后端表,从登录bean访问另一个名为“fields”的bean来设置所有用户相关信息,并使用这些“字段”值填充“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>

登录bean:

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;
}

字段bean:

 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>

请让我知道如何继续这样做。任何帮助是极大的赞赏。提前谢谢!

2 个答案:

答案 0 :(得分:0)

正如Mr @ MrJ4mes先生所说,你错过了setter注入的托管bean ProviderFieldsBean fields来提供对视图的访问权。

答案 1 :(得分:0)

我猜ProviderFieldBean是一个模型类..如果它应该用@ table..it注释不应该有sessioncoped或managedbean注释