Java JSF commandButton不会导航到其操作方法';s的结果页
目标:用户输入一个电子邮件地址。submit按钮的操作方法检查数据库中与输入的电子邮件相同的任何现有电子邮件Java JSF commandButton不会导航到其操作方法';s的结果页,java,jsf,jdbc,Java,Jsf,Jdbc,目标:用户输入一个电子邮件地址。submit按钮的操作方法检查数据库中与输入的电子邮件相同的任何现有电子邮件 如果存在重复项,则重新加载registration.xhtml页面 如果没有副本,则加载userHome.xhtml页面 问题:单击submit按钮时不会发生任何事情 我在控制台中没有收到任何错误,所以我认为这一定是一些错误的逻辑 旁注:我没有包括index.xhtml和web.xml页面。我认为没有必要解决这个问题。如果你需要,我很乐意提供 registration.xhtml <
如果存在重复项,则重新加载registration.xhtml页面
如果没有副本,则加载userHome.xhtml页面 问题:单击submit按钮时不会发生任何事情 我在控制台中没有收到任何错误,所以我认为这一定是一些错误的逻辑 旁注:我没有包括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>
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()”是可行的
以下是将来遇到问题的人的代码:
包注册视图;
导入java.io.Serializable;
导入java.sql.Connection;
导入java.sql.DriverManager;
导入java.sql.PreparedStatement;
导入java.sql.ResultSet;
导入javax.faces.bean.ManagedBean;
导入javax.faces.bean.RequestScope;
@ManagedBean(name=“user”,eager=true)
@请求范围
公共类用户实现可序列化
{
私有静态最终长serialVersionUID=1L;
私有字符串名称;
私人字符串电子邮件;
私有字符串;
私有字符串数据库测试;
私有字符串密码;
私有字符串passwordConf;
私人字符串性别;
私弦时代;
私有字节[]profileImage;
私有布尔值isValidEmail=false;
连接con=null;
PreparedStatement prst=null;
结果集rs=null;
公共字符串getName()
{
返回名称;
}
公共void集合名(字符串名)
{
this.name=名称;
}
公共字符串getEmail()
{
回复邮件;
}
公用电子邮件(字符串电子邮件)
{
this.email=电子邮件;
}
公共字符串getEmailConf()
{
返回emailConf;
}
public void setEmailConf(字符串emailConf)
{
this.emailConf=emailConf;
}
公共字符串getPassword()
{
返回密码;
}
public void setPassword(字符串密码)
{
this.password=密码;
}
公共字符串getPasswordConf()
{
返回密码conf;
}
public void setPasswordConf(字符串passwordConf)
{
this.passwordConf=passwordConf;
}
公共字符串getGender()
{
返回性别;
}
公共无效设置性别(字符串性别)
{
这个。性别=性别;
}
公共字符串getAge()
{
回归年龄;
}
公共无效设置(字符串期限)
{
这个。年龄=年龄;
}
公共字节[]getProfileImage()
{
返回图像;
}
public void setProfileImage(字节[]profileImage)
{
this.profileImage=profileImage;
}
公共布尔getValidEmail()
{
返回isValidEmail;
}
public void setValidEmail(布尔有效)
{
this.isValidEmail=有效;
}
公共字符串getEmailDuplicateResults()
{
checkForDuplicates();
if(getValidEmail()==true)
{
返回“userHome”;
}否则
{
返回“注册”;
}
}
公共无效支票一式两份()
{
//创建连接
尝试
{
//负载驱动器
Class.forName(“com.mysql.jdbc.Driver”);
//连接到数据库
con=驾驶员管理器
.getConnection(“jdbc:mysql://localhost/userProfile?user=root&password=weston");
System.out.println(“连接到数据库userProfile!”);
//将“自动提交”设置为false以手动管理它
//con.setAutoCommit(false);自
//唯一的查询是检查数据库中的值
//用于检查数据库中输入的电子邮件地址的查询字符串
String emailDuplicateCheckQuery=“从userInfo中选择*,其中电子邮件=?;”;
//创建准备好的语句对象
prst=con
.prepareStatement(emailDuplicateCheckQuery);
//将字符串设置为emailDuplicateCheckQuery查询中的“”
设置字符串(1,getEmail());
//将查询分配给结果集
rs=prst.executeQuery();
//测试由文本查询生成的结果集,如果该结果集为空
while(rs.next())
{
emailDatabaseTest=rs.getString(“电子邮件”);
System.out.println(emailDatabaseTest);
}
如果(emailDatabaseTest==null)
{
isValidEmail=true;
setValidEmail(isValidEmail);
System.out.println(“电子邮件有效”);
}否则
{
isValidEmail=false;
setValidEmail(isValidEmail);
System.out.println(“电子邮件无效”);
}
rs.close();
//提交并关闭
//con.commit();
con.close();
}
捕获(例外e)
{
e、 printStackTrace();
//为加载失败创建消息
}
}
}
让我们开始使用
而不是
,当使用PreparedStatement
时,您应该将输入作为绑定变量传递(在查询中使用?
占位符)。您当前的版本易受SQL injection.VoodooCoder攻击,更改为无效,感谢您提供的提示Mick助记符。我觉得您的实现很奇怪。您声明了一个navigationClass
bean,但从未使用过它。您可以使用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>
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
}
}
}
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";
}
}