0

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

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

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

我做了什么:

当用户在登录时点击提交时,我使用一个查询来检查一个名为“login”(托管bean 名称)的bean 中的表的值。当值出现在表中时,我使用另一个查询来检索用户的其他信息,并将这些值填充到另一个名为“字段”的 bean 的 setter 方法中。现在,使用面孔重定向,我传递了名为“userindex.xhtml”的 JSF 页面的名称。现在,我只是尝试访问“字段”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;
} 
}

用户索引页面

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

请让我知道如何进行此操作。任何帮助是极大的赞赏。提前致谢!

4

2 回答 2

0

正如@MrJ4mes 先生所说,想念setter您注入的托管 beanProviderFieldsBean fields以通过视图提供访问权限。

于 2013-09-17T13:58:19.653 回答
0

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

于 2014-04-15T09:50:37.360 回答