【发布时间】:2013-09-08 17:12:10
【问题描述】:
这就是我想要达到的目标:
1) 当用户尝试使用他的用户名/密码登录时,这些凭据会根据我的数据库表进行验证,以确保用户有效。 2) 验证通过后,我会检索有关用户的一些详细信息,并在登录后立即在 JSF 页面中显示这些值。
当用户输入他的用户名/密码并点击提交时,这两个过程都发生在单击(即)时。
我做了什么:
当用户在登录时点击提交时,我使用一个查询来检查一个名为“login”(托管bean 名称)的bean 中的表的值。当值出现在表中时,我使用另一个查询来检索用户的其他信息,并将这些值填充到另一个名为“fields”的 bean 中的 setter 方法中。现在,使用面孔重定向,我传递了名为“userindex.xhtml”的 JSF 页面的名称。现在,我只是尝试访问“字段”bean 的getter 方法以显示在用户索引页面中。
前两个查询运行成功。唯一的问题似乎是当我尝试从“userindex”页面访问它的值时,“fields”bean 对象被初始化/重新创建。如果我错了,请纠正我。
总而言之,我使用了一个 bean“登录”来使用后端表验证用户输入值,从登录 bean 访问另一个名为“字段”的 bean 以设置所有用户相关信息,并使用这些“字段”值填充“用户索引”页面。
代码sn-ps如下:
登录页面:
<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>
请让我知道如何进行此操作。任何帮助是极大的赞赏。提前致谢!
【问题讨论】:
-
我们需要更多托管 bean 的代码。尤其是范围。
-
感谢您的评论。我已经为这两个 bean 添加了整个代码。
-
您可以尝试为您的
ProviderFieldsBean字段添加getter 吗? -
Mr.J4mes:我在 ProviderFieldsBean 中有 getter 方法。您能否详细说明您要说的内容。感谢您的关注。
标签: jsf jsf-2 managed-bean backing-beans