Sunday, 8 September 2013

Bean values not getting displayed on JSF page

Bean values not getting displayed on JSF page

This is what I am trying to achieve:
1) When the user tries to login using his userid/password, these
credentials are validated against my database table to make sure the user
is valid. 2) When the validation passes, I retrieve some of the details
about the user and display those values in the JSF page immediately after
logging in.
Both these two process happen upon one single click (ie) while the user
inputs his userid/password and hits submit.
What I did:
When the user hits submit upon logon, I use a query to check the values
against a table in a bean called "login" (managedbean name). When the
values are present in the table, I use another query to retrieve the
user's other information and populate these values in the setter methods
in another bean called "fields". Now, using faces redirect, I pass the
name of the JSF page called "userindex.xhtml". Now, I simply try to access
the getter methods of the "fields" bean to display in the userindex page.
The first two queries run successfully. The only problem seems to be that
the "fields" bean object is initialized/recreated when I try to access its
values from the "userindex" page. Please correct me if I am wrong.
To summarize, I used a bean "login" to verify user input values with a
backend table, accessed another bean called "fields" from the login bean
to set all user related information and used these "fields" values to
populate in the "userindex" page.
The code snippets are given below:
Login page:
<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>
Login bean: (checkUser method):
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 = "providerindex1?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;
Fields bean:
public void setAll(String firstname, String lastname, String email,
String phone)
{
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
this.phone = phone;
}
userindex page
<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>
Please let me know how to proceed on this. Any help is greatly
appreciated. Thanks in advance!

No comments:

Post a Comment