【发布时间】:2015-03-16 15:48:26
【问题描述】:
所以我有一个 JSF 页面,其中包含一个带有 <h:inputtext> 和一个 <h:commandbutton> 的面板网格。当我按下按钮时,我希望将输入文本中引入的数据存储在数据库中。执行动作,问题是数据库中存储的值是空的。
这是 Bean:
package accountsPackage;
public class Accounts {
private String username;
private String password;
private String CustomerName;
private String CustomerFirstName;
private String CustomerMiddleName;
private String CustomerPhone;
private String CustomerMobilePhone;
private String repassword;
private String email;
private String address;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCustomerName() {
return CustomerName;
}
public void setCustomerName(String customerName) {
CustomerName = customerName;
}
public String getCustomerFirstName() {
return CustomerFirstName;
}
public void setCustomerFirstName(String customerFirstName) {
CustomerFirstName = customerFirstName;
}
public String getCustomerMiddleName() {
return CustomerMiddleName;
}
public void setCustomerMiddleName(String customerMiddleName) {
CustomerMiddleName = customerMiddleName;
}
public String getCustomerPhone() {
return CustomerPhone;
}
public void setCustomerPhone(String customerPhone) {
CustomerPhone = customerPhone;
}
public String getCustomerMobilePhone() {
return CustomerMobilePhone;
}
public void setCustomerMobilePhone(String customerMobilePhone) {
CustomerMobilePhone = customerMobilePhone;
}
public String getRepassword() {
return repassword;
}
public void setRepassword(String repassword) {
this.repassword = repassword;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
这是连接到 MySQL 数据库并发送值的类:
package databaseConnection;
import java.sql.*;
import accountsPackage.Accounts;
public class ConnectToDatabase {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/crochet_onlineshop";
static final String USER = "root";
static final String PASS = "root";
Accounts acc=new Accounts();
PreparedStatement ps=null;
public String addUser() throws Exception,SQLException,ClassNotFoundException{
String username=acc.getUsername();
String password=acc.getPassword();
String CustomerName=acc.getCustomerName();
String CustomerFirstName=acc.getCustomerFirstName();
String CustomerMiddleName=acc.getCustomerMiddleName();
String CustomerPhone=acc.getCustomerPhone();
String CustomerMobilePhone=acc.getCustomerMobilePhone();
String repassword=acc.getRepassword();
String email=acc.getEmail();
String address=acc.getAddress();
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sqlAcc= "INSERT INTO Customer (username, password, repassword, CustomerName, CustomerFirstName, CustomerMiddleName, CustomerMobilePhone, CustomerPhone, email, address) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
ps=conn.prepareStatement(sqlAcc);
ps.setNString(1, username);
ps.setNString(2, password);
ps.setNString(3, repassword);
ps.setNString(4, CustomerName);
ps.setNString(5, CustomerFirstName);
ps.setNString(6, CustomerMiddleName);
ps.setNString(7, CustomerMobilePhone);
ps.setNString(8, CustomerPhone);
ps.setNString(9, email);
ps.setNString(10, address);
if( ps.executeUpdate()==1)
return "success";
else
return "fail";
}
}
这是 JSF 页面(jsp):
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h4>Please fill in the necessary fields in order to complete your account</h4>
<f:view>
<h:panelGrid columns="1">
<h:outputLabel value="username"></h:outputLabel>
<h:inputText value="#{accounts.username}"></h:inputText>
<h:outputLabel value="password"></h:outputLabel>
<h:inputSecret value="#{accounts.password}"></h:inputSecret>
<h:outputLabel value="re-enter password"></h:outputLabel>
<h:inputSecret value="#{accounts.repassword}"></h:inputSecret>
<h:outputLabel value="name"></h:outputLabel>
<h:inputText value="#{accounts.customerName}"></h:inputText>
<h:outputLabel value="firstname"></h:outputLabel>
<h:inputText value="#{accounts.customerFirstName}"></h:inputText>
<h:outputLabel value="middle name"></h:outputLabel>
<h:inputText value="#{accounts.customerMiddleName}"></h:inputText>
<h:outputLabel value="mobile phone"></h:outputLabel>
<h:inputText value="#{accounts.customerMobilePhone}"></h:inputText>
<h:outputLabel value="phone"></h:outputLabel>
<h:inputText value="#{accounts.customerPhone}"></h:inputText>
<h:outputLabel value="email"></h:outputLabel>
<h:inputText value="#{accounts.email }"></h:inputText>
<h:outputLabel value="address"></h:outputLabel>
<h:inputText value="#{accounts.address }"></h:inputText>
<h:form>
<h:commandButton action="#{connectToDatabase.addUser}"></h:commandButton>
</h:form>
</h:panelGrid>
</f:view>
</body>
</html>
谁能帮我找出为什么存储在 db 中的值为 NULL?
【问题讨论】:
-
在当前的代码 sn-ps 中,没有类是托管 bean,即它们不受 JSF 框架、容器或您可能感兴趣的其他东西的管理——它们只是普通的 Java 类。他们在您的真实代码中管理 bean 吗?您没有在
<h:form>标记中包含输入字段。因此,当点击给定的<h:commandButton>时,它们将被提交。请相应地调整代码。 -
Account 是一个托管 bean -session 并且 ConnectTo Database 被设置为请求
-
我没有过多地使用 XML 配置。因此,我没有猜到。 :)
-
我已将输入字段包含在一个表单中,但它会将 NULL 值传递给 mysql 数据库。