简体   繁体   中英

JSF commandButton does not navigate to its action method's resulting page

The user enters in an email address. 用户输入电子邮件地址。 The submit button's action method checks for any existing emails in the database that are the same as the one entered.

If there is a duplicate then the registration.xhtml page reloads.

If there isn't a duplicate, then the userHome.xhtml page loads.

nothing happens when the submit button is clicked. 单击“提交”按钮后没有任何反应。

I do not receive any errors in the console, so I'm thinking it has to be some misplaced logic.

There is an index.xhtml and web.xml page that I didn't include. 有,我没有包括的index.xhtml和web.xml页面。 Didn't think it was necessary to solve the problem. If you need them, then I will be glad to provide them.

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";
    }

}

Turns out that instead "emailDatabaseTest.isEmpty()", using "emailDatabaseText == null" works.

Here is the code for anyone that runs into the problem in the future:

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

        }

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM