目标:用户输入电子邮件地址。提交按钮的操作方法检查数据库中与输入的电子邮件相同的任何现有电子邮件。
如果有重复,则registration.xhtml页面重新加载。
如果没有重复,则加载userHome.xhtml页面。
问题:点击提交按钮时没有任何反应。
我在控制台中没有收到任何错误,因此我认为它必须是一些错位的逻辑。
旁注:有一个我没有包含的index.xhtml和web.xml页面。没想到有必要解决这个问题。如果你需要它们,那么我很乐意提供它们。
registration.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head></h:head>
<body>
<h1>Welcome to the Registration Page</h1>
<h:form enctype="multipart/form-data">
<p>Username:</p><h:inputText value="#{user.name}" />
<p>Email Address:</p><h:inputText value="#{user.email}"/>
<p>Email Confirmation:</p><h:inputText value="#{user.emailConf}"/>
<p>Password:</p><h:inputText value="#{user.password}"/>
<p>Password Confirmation:</p><h:inputText value="#{user.passwordConf}"/>
<p>Gender:</p><h:selectOneRadio id ="genderSelection" value="#{user.gender}">
<f:selectItem id="male" itemLabel="Male" itemValue="male" />
<f:selectItem id="female" itemLabel="Female" itemValue="female" />
</h:selectOneRadio>
<p>Birthday yy</p><h:inputText value="#{user.age}"/>
<h:commandButton action="#{user.getEmailDuplicateResults}" value="Submit"/>
</h:form>
</body>
</html>
userHome.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head></h:head>
<body>
<h:message title="Welcome to the userHome page"/>
<h1>Welcome to the userHome page2</h1>
</body>
</html>
faces-config.xml中
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<navigation-rule>
<from-view-id>*</from-view-id>
<navigation-case>
<from-action>#{navigationClass.goToUserHome}</from-action>
<from-outcome>index</from-outcome>
<to-view-id>/userHome.xhtml</to-view-id>
<redirect />
</navigation-case>
<navigation-case>
<from-action>#{navigationClass.goToRegistration}</from-action>
<from-outcome>index</from-outcome>
<to-view-id>/registration.xhtml</to-view-id>
<redirect />
</navigation-case>
<navigation-case>
<from-action>#{user.getEmailDuplicateResults}</from-action>
<from-outcome>registration</from-outcome>
<to-view-id>/registration.xhtml</to-view-id>
<redirect />
</navigation-case>
<navigation-case>
<from-action>#{user.getEmailDuplicateResults}</from-action>
<from-outcome>userHome</from-outcome>
<to-view-id>/userHome.xhtml</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>
<managed-bean>
<managed-bean-name>user</managed-bean-name>
<managed-bean-class>registrationView.User</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
User.java
package registrationView;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean(name = "user")
@RequestScoped
public class User
{
private String name;
private String email;
private String emailConf;
private String emailDatabaseTest;
private String password;
private String passwordConf;
private String gender;
private String age;
private byte[] profileImage;
private boolean isValidEmail = false;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public String getEmailConf()
{
return emailConf;
}
public void setEmailConf(String emailConf)
{
this.emailConf = emailConf;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getPasswordConf()
{
return passwordConf;
}
public void setPasswordConf(String passwordConf)
{
this.passwordConf = passwordConf;
}
public String getGender()
{
return gender;
}
public void setGender(String gender)
{
this.gender = gender;
}
public String getAge()
{
return age;
}
public void setAge(String age)
{
this.age = age;
}
public byte[] getProfileImage()
{
return profileImage;
}
public void setProfileImage(byte[] profileImage)
{
this.profileImage = profileImage;
}
public boolean getValidEmail()
{
return isValidEmail;
}
public void setValidEmail(boolean valid)
{
this.isValidEmail = valid;
}
public String getEmailDuplicateResults()
{
checkForDuplicates();
if (getValidEmail() == true)
{
return "userHome";
} else
{
return "registration";
}
}
public void checkForDuplicates()
{
// Create connection
try
{
// Load driver
Class.forName("com.mysql.jdbc.Driver");
// Connect to the database
Connection connection = DriverManager
.getConnection("jdbc:mysql://localhost/userProfile?user=root&password=weston");
// Set autocommit to false to manage it by hand
connection.setAutoCommit(false);
// Create the prepared statement object
PreparedStatement statement = connection
.prepareStatement("SELECT * FROM userInfo WHERE email ='"
+ getEmail() + "';");
// assigning the query to a result set
ResultSet rs = statement.executeQuery();
// testing result set made from queries for text or if it is empty
while (rs.next())
{
emailDatabaseTest = rs.getString("email");
if (emailDatabaseTest.isEmpty())
{
isValidEmail = true;
setValidEmail(isValidEmail);
} else
{
isValidEmail = false;
setValidEmail(isValidEmail);
}
}
rs.close();
// Commit & close
connection.commit();
connection.close();
}
catch (Exception e)
{
e.printStackTrace();
// create message for unsuccessful loading
}
}
}
NavigationClass.java
package nav;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean(name = "navigationClass", eager = true)
@RequestScoped
public class NavigationClass implements Serializable
{
private static final long serialVersionUID = 1L;
public String goToUserHome()
{
return "userHome";
}
public String goToRegistration()
{
return "registration";
}
}
答案 0 :(得分:0)
反过来说&#34; emailDatabaseTest.isEmpty()&#34;,使用&#34; emailDatabaseText == null&#34;的工作原理。
以下是遇到问题的任何人的代码:
package registrationView;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean(name = "user" ,eager = true)
@RequestScoped
public class User implements Serializable
{
private static final long serialVersionUID = 1L;
private String name;
private String email;
private String emailConf;
private String emailDatabaseTest;
private String password;
private String passwordConf;
private String gender;
private String age;
private byte[] profileImage;
private boolean isValidEmail = false;
Connection con = null;
PreparedStatement prst = null;
ResultSet rs = null;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public String getEmailConf()
{
return emailConf;
}
public void setEmailConf(String emailConf)
{
this.emailConf = emailConf;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getPasswordConf()
{
return passwordConf;
}
public void setPasswordConf(String passwordConf)
{
this.passwordConf = passwordConf;
}
public String getGender()
{
return gender;
}
public void setGender(String gender)
{
this.gender = gender;
}
public String getAge()
{
return age;
}
public void setAge(String age)
{
this.age = age;
}
public byte[] getProfileImage()
{
return profileImage;
}
public void setProfileImage(byte[] profileImage)
{
this.profileImage = profileImage;
}
public boolean getValidEmail()
{
return isValidEmail;
}
public void setValidEmail(boolean valid)
{
this.isValidEmail = valid;
}
public String getEmailDuplicateResults()
{
checkForDuplicates();
if (getValidEmail() == true)
{
return "userHome";
} else
{
return "registration";
}
}
public void checkForDuplicates()
{
// Create connection
try
{
// Load driver
Class.forName("com.mysql.jdbc.Driver");
// Connect to the database
con = DriverManager
.getConnection("jdbc:mysql://localhost/userProfile?user=root&password=weston");
System.out.println("Connected to database userProfile!");
// Set autocommit to false to manage it by hand
//con.setAutoCommit(false); THIS IS SET TO AUTOCOMMIT SINCE
//THE ONLY QUERY IS TO CHECK THE DATABASE FOR A VALUE
//query string to check database for email address entered
String emailDuplicateCheckQuery = "SELECT * FROM userInfo WHERE email =?; ";
// Create the prepared statement object
prst = con
.prepareStatement(emailDuplicateCheckQuery);
//sets a string to the ? in the emailDuplicateCheckQuery query
prst.setString(1, getEmail());
// assigning the query to a result set
rs = prst.executeQuery();
// testing result set made from queries for text or if it is empty
while (rs.next())
{
emailDatabaseTest = rs.getString("email");
System.out.println(emailDatabaseTest);
}
if (emailDatabaseTest == null)
{
isValidEmail = true;
setValidEmail(isValidEmail);
System.out.println("Email is valid.");
} else
{
isValidEmail = false;
setValidEmail(isValidEmail);
System.out.println("Email is invalid.");
}
rs.close();
// Commit & close
//con.commit();
con.close();
}
catch (Exception e)
{
e.printStackTrace();
// create message for unsuccessful loading
}
}
}