我已经四处寻找但无法找到任何帮助。
我试图创建一个用户但是2个字段保持为空(名字和部门)。
The Gui(html):
<div style="width: 900px; margin-left: auto; margin-right: auto">
<form action="EmployeesAddManager.jsp" method="post">
Firstname:<br>
<input type="text" name="firstname" style="width: 200px"><br>
Lastname:<br>
<input type="text" name="lastname" style="width: 200px"><br>
Gender:
<select name="gender">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select><br>
Email:<br>
<input type="text" name="email" style="width: 200px"><br>
Role:
<select name="role">
<option value="Department Leader">Department Leader</option>
<option value="Assistant">Assistant</option>
</select><br>
Department:
<select name="department">
<option value="Tetriz">Tetriz</option>
<option value="Cube">Cube</option>
</select><br>
Image:<br>
<input type="text" name="image" style="width: 200px"><br>
Username:<br>
<input type="text" name="username" style="width: 200px"><br>
Password:<br>
<input type="text" name="password" style="width: 200px"><br>
<input type="submit" value="Create Employee">
</form>
</div>
已经在&#39; EmployeesAddManager.jsp&#39;我尝试打印输入,但firstname和department将为null(所有其他工作):
<%@page import="model.EmployeeModel"%>
<%@page import="model.Employees"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>
<%
//We get the fulfilled parameters
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String gender = request.getParameter("gender");
String email = request.getParameter("email");
String role = request.getParameter("role");
String department = request.getParameter("department");
String image = request.getParameter("image");
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("Firstname from EmployeesAddManager.jsp: "+firstname);
System.out.println("Lastname from EmployeesAddManager.jsp: "+lastname);
System.out.println("Department from EmployeesAddManager.jsp: "+department);
//We instantiate an employee and set the parameters
Employees emp = new Employees(0, firstname, lastname, gender, email, role, department, image, username, password);
//We call for the method for creating a new employee, and send the new instantiated employee
EmployeeModel empModel = new EmployeeModel();
empModel.newEmployee(emp);
/*This method is used to redirect client request to some other location
for further processing ,the new location is available on different server
or different context.our web container handle this and transfer the request
using browser ,and this request is visible in browser as a new request.
Some time this is also called as client side redirect.
*/
response.sendRedirect("/Employees_servlet");
%>
</body>
</html>
这里的模型连接到数据库:
public void newEmployee(Employees emp) throws SQLException{
try {
PreparedStatement ps = DbModel2.getPreparedStatement("INSERT INTO employee_table (Id_employee, Firstname, Lastname, Gender, Email, RoleId_Fk, DepartmentId_Fk, Image) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
ps.setInt(1, emp.getId());
ps.setString(2, emp.getFirstname());
ps.setString(3, emp.getLastname());
ps.setString(4, emp.getGender());
ps.setString(5, emp.getEmail());
ps.setString(6, emp.getRole());
ps.setString(7, emp.getDepartment());
ps.setString(8, emp.getImage());
System.out.println("Id: "+emp.getId());
System.out.println("Firstname: "+emp.getFirstname());
System.out.println("Lastname: "+ emp.getLastname());
System.out.println("Gender: "+emp.getGender());
System.out.println("Email: "+emp.getEmail());
System.out.println("Role: "+emp.getRole());
System.out.println("Department: "+emp.getDepartment());
System.out.println("Image: "+emp.getImage());
ps.executeUpdate();
System.out.println("EXECUTED");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Employees.java:
public class Employees {
private int id;
private String firstname;
private String lastname;
private String gender;
private String email;
private String role;
private String department;
private String image;
private String username;
private String password;
public Employees(int id, String firstname, String lastname, String gender, String email, String role, String department, String image, String username, String password) {
this.id = id;
this.firstname = firstname;
this.lastname = lastname;
this.gender = gender;
this.email = email;
this.role = role;
this.department = department;
this.image = image;
this.username = username;
this.password = password;
System.out.println("this.firstname: "+firstname);
System.out.println("this.department: "+department);
System.out.println("getFirstname: "+getFirstname());
System.out.println("getDepartment: "+getDepartment());
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
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;
}
}
答案 0 :(得分:1)
因为在吸气剂中你已经完成了
return department
将其更改为this.department
,同样适用于firstname
修改强>
它适用于您的情况,因为我只想使用this
强制执行值,但我需要理解它之前为什么不起作用。
答案 1 :(得分:0)
我没有尝试这个代码,这可能听起来有些幼稚,但你尝试更改名称吗?你检查过白色空间吗?