簡體   English   中英

JSF commandButton不會導航到其操作方法的結果頁面

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

目標:用戶輸入電子郵件地址。 提交按鈕的操作方法將檢查數據庫中與輸入的電子郵件相同的所有現有電子郵件。

如果重復,則重新加載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";
    }

}

原來,使用“ emailDatabaseText == null”的“ emailDatabaseTest.isEmpty()”替代了。

這是將來遇到問題的任何人的代碼:

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

        }

    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM