-1
目標:用戶輸入電子郵件地址。提交按鈕的操作方法檢查數據庫中與輸入的電子郵件相同的任何現有電子郵件。
如果存在重複,則重新加載registration.xhtml頁面。
如果沒有重複,則加載userHome.xhtml頁面。JSF commandButton不會導航到其操作方法的結果頁面
問題:單擊提交按鈕時沒有任何反應。
我沒有收到控制檯中的任何錯誤,所以我認爲它必須是一些錯位的邏輯。
邊注:有一個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";
}
}
讓我們開始使用''而不是'' –
此外,當使用一個'PreparedStatement'你應該通過在輸入作爲_bind變量_(在查詢中使用「?」佔位符)。您當前的版本容易受到SQL注入的影響。 –
VoodooCoder,將
更改爲