我有一个用于创建所谓的NonCIC帐户的jsp表单。我在控制器类和DAOImpl类中编写了jsp代码,javascript函数,方法。但是,每当我点击“创建”按钮时,尽管在文本字段中输入了值,但我收到错误消息:“请提供帐户名称,部门名称和部门编号”。我不知道这里有什么问题。请帮忙。
我有以下JSP代码:
<div id="div2" class="hide">
<label for="nonCICAccountName">Account Name:</label> <input type="text" name="nonCICAccountName" id="nonCICAccountName" /> <br />
<label for="country">Country:</label> <input type="text" name="country" id="country" /> <br />
<label for="countryNumber">Country Number:</label>
<input type="text" name="countryNumber" id="countryNumber" /> <br />
<label for="locationName">Location Name:</label>
<input type="text" name="locationName" id="locationName" /> <br />
<label for="locationNumber">Location Number:</label>
<input type="text" name="locationNumber" id="locationNumber" /> <br />
<label for="departmentName">Department Name:</label>
<input type="text" name="departmentName" id="departmentName" /> <br />
<label for="departmentNumber">Department Number:</label>
<input type="text" name="departmentNumber" id="departmentNumber" /> <br />
<label for="division">Division:</label>
<input type="text" name="division" id="division" /> <br />
<label for="major">Major:</label>
<input type="text" name="major" id="major" /> <br />
<label for="minor">Minor:</label>
<input type="text" name="minor" id="minor" /> <br />
<label for="subminor">Subminor:</label>
<input type="text" name="subminor" id="subminor" /> <br />
<label for="leru">Leru:</label>
<input type="text" name="leru" id="leru" /> <br />
<label for="task">Task:</label>
<input type="text" name="task" id="task" /> <br />
<label for="approvingManager">Approving Manager:</label>
<input type="text" name="approvingManager" id="approvingManager" /> <br />
<label for="financialApprover">Financial Approver:</label>
<input type="text" name="financialApprover" id="financialApprover" /> <br />
<br /> <input type="button" value="Create NonCIC Account" onclick="createNonCICAccount();">
<input type="button" value="Search Account" onclick="searchNonCICAccount();">
</div>
以下是函数createNonCICAccount():
function createNonCICAccount()
{
var accountName = document.getElementById("nonCICAccountName").value;
var country = document.getElementById("country").value;
var countryNumber = document.getElementById("countryNumber").value;
var locationName = document.getElementById("locationName").value;
var locationNumber = document.getElementById("locationNumber").value;
var departmentName = document.getElementById("departmentName").value;
var departmentNumber = document.getElementById("departmentNumber").value;
var division = document.getElementById("division").value;
var major = document.getElementById("major").value;
var minor = document.getElementById("minor").value;
var subminor = document.getElementById("subminor").value;
var leru = document.getElementById("leru").value;
var task = document.getElementById("task").value;
var approvingManager = document.getElementById("approvingManager").value;
var financialApprover = document.getElementById("financialApprover").value;
$.ajax({
type: "POST",
url: "NonCICAccountController",
data: {"action":"createNonCICAccount",
"nonCICAccountName":accountName,"country":country,"countryNumber":countryNumber,"locationName":locationName,"locationNumber":locationNumber,"departmentName":departmentName,"departmentmentNumber":departmentNumber,"division":division,"major":major,"minor":minor,
"subminor":subminor,"leru":leru,"task":task,"approvingManager":approvingManager,"financialApprover":financialApprover},
success: function(data,jqXHR){
$("#accMessageDiv").html(data);
reset();
},
error: function(data,jqXHR){
$("#accMessageDiv").html(data);
},
beforeSend: function() {
// setting a timeout
},
complete: function()
{
}
});
}
以下是NonCICAccount类:
package com.coetool.servlet.model;
public class NonCICAccount {
private int accountId;
private String accountName;
private String country;
private int countryNumber;
private String locationName;
private String locationNumber;
private String departmentName;
private String departmentNumber;
private String division;
private int major;
private int minor;
private int subminor;
private String leru;
private int task;
private String approvingManager;
private String financialApprover;
public NonCICAccount() {}
public NonCICAccount(int accountId, String accountName, String country,int countryNumber,String locationName,String locationNumber,String departmentName,String departmentNumber,String division,int major, int minor,
int subminor,String leru,int task,String approvingManager,String financialApprover)
{
this.accountId=accountId;
this.accountName=accountName;
this.country = country;
this.countryNumber = countryNumber;
this.locationName = locationName;
this.locationNumber = locationNumber;
this.departmentName = departmentName;
this.departmentNumber = departmentNumber;
this.division = division;
this.major = major;
this.minor = minor;
this.subminor = subminor;
this.leru = leru;
this.task = task;
this.approvingManager = approvingManager;
this.financialApprover = financialApprover;
}
public int getAccountId() {
return accountId;
}
public void setAccountId(int accountId) {
this.accountId = accountId;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public int getCountryNumber() {
return countryNumber;
}
public void setCountryNumber(int countryNumber) {
this.countryNumber = countryNumber;
}
public String getLocationName() {
return locationName;
}
public void setLocationName(String locationName) {
this.locationName = locationName;
}
public String getLocationNumber() {
return locationNumber;
}
public void setLocationNumber(String locationNumber) {
this.locationNumber = locationNumber;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
public String getDepartmentNumber() {
return departmentNumber;
}
public void setDepartmentNumber(String departmentNumber) {
this.departmentNumber = departmentNumber;
}
public String getDivision() {
return division;
}
public void setDivision(String division) {
this.division = division;
}
public int getMajor() {
return major;
}
public void setMajor(int major) {
this.major = major;
}
public int getMinor() {
return minor;
}
public void setMinor(int minor) {
this.minor = minor;
}
public int getSubminor() {
return subminor;
}
public void setSubminor(int subminor) {
this.subminor = subminor;
}
public String getLeru() {
return leru;
}
public void setLeru(String leru) {
this.leru = leru;
}
public int getTask() {
return task;
}
public void setTask(int task) {
this.task = task;
}
public String getApprovingManager() {
return approvingManager;
}
public void setApprovingManager(String approvingManager) {
this.approvingManager = approvingManager;
}
public String getFinancialApprover() {
return financialApprover;
}
public void setFinancialApprover(String financialApprover) {
this.financialApprover = financialApprover;
}
}
以下是NonCICAccountController类中的createNonCICAccount方法:
protected void createNonCICAccount(HttpServletRequest request,HttpServletResponse response) throws IOException
{
String error;
NonCICAccount account = new NonCICAccount();
account = fillAttributes(account, request);
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
NonCICAccountDAO accountDAO = new NonCICAccountDAOImpl();
NonCICAccount accountToBeFetched = null;
int result=0;
try {
if(account.getAccountName() != null && account.getDepartmentName() != null && account.getDepartmentNumber() !=null)
{
accountToBeFetched = accountDAO.getNonCICAccountDetailsWithAccountNameAndDepartmentNumber(account);
if(accountToBeFetched== null)
result = accountDAO.createNonCICAccount(account);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (account.getAccountName() == null || account.getDepartmentName() == null || account.getDepartmentNumber() == null ) {
error = "Please provide the Account Name, Department Name and Department Number.";
out.write("<p style='color:red;'>"+error+"</p>");
}
else if(accountToBeFetched != null && (account.getAccountName() != null || account.getDepartmentName() != null || account.getDepartmentNumber() != null ))
{
error = "Account already exist with given Account Name, Department Name and Department Number.";
out.write("<p style='color:red;'>"+error+"</p>");
}
else {
String msg = "Account :"+account.getAccountName() + " successfully added. ";
out.write("<p style='color:blue;'>"+msg+"</p>");
/*session.setAttribute("message", msg);
session.removeAttribute("error");
response.sendRedirect("index.jsp");*/
}
}
以下是NonCICAccountController类中的fillAttributes方法:
private NonCICAccount fillAttributes(NonCICAccount account,HttpServletRequest request)
{
account.setAccountName(checkNull(request.getParameter("nonCICAccountName")));
account.setCountry(checkNull(request.getParameter("country")));
account.setCountryNumber(Integer.parseInt(request.getParameter("countryNumber")));
account.setLocationName(checkNull(request.getParameter("locationName")));
account.setLocationNumber(checkNull(request.getParameter("locationNumber")));
account.setDepartmentName(checkNull(request.getParameter("departmentName")));
account.setDepartmentNumber(checkNull(request.getParameter("departmentNumber")));
account.setDivision(checkNull(request.getParameter("division")));
account.setMajor(Integer.parseInt(request.getParameter("major")));
account.setMinor(Integer.parseInt(request.getParameter("minor")));
account.setSubminor(Integer.parseInt(request.getParameter("subminor")));
account.setLeru(checkNull(request.getParameter("leru")));
account.setTask(Integer.parseInt(request.getParameter("task")));
account.setApprovingManager(checkNull(request.getParameter("approvingManager")));
account.setFinancialApprover(checkNull(request.getParameter("financialApprover")));
return account;
}
以下是checkNull方法:
private String checkNull(String input)
{
if(StringUtils.isNotBlank(input))
return input;
else
return null;
}
以下是NonCICAccountDAOImpl类中的createNonCICAccount方法:
public int createNonCICAccount(NonCICAccount account) throws SQLException {
int result = 0;
try {
// dbConnection.prepareStatement(SQL_INSERT,Statement.RETURN_GENERATED_KEYS);
pStmt = dbConnection.prepareStatement(SQL_INSERT);
pStmt.setString(1, account.getAccountName());
pStmt.setString(2, account.getCountry());
pStmt.setInt(3, account.getCountryNumber());
pStmt.setString(4, account.getLocationName());
pStmt.setString(5, account.getLocationNumber());
pStmt.setString(6, account.getDepartmentName());
pStmt.setString(7, account.getDepartmentNumber());
pStmt.setString(8, account.getDivision());
pStmt.setInt(9, account.getMajor());
pStmt.setInt(10, account.getMinor());
pStmt.setInt(11, account.getSubminor());
pStmt.setString(12, account.getLeru());
pStmt.setInt(13, account.getTask());
pStmt.setString(14, account.getApprovingManager());
pStmt.setString(15, account.getFinancialApprover());
result = pStmt.executeUpdate();
/*
* rs = pstmt.getGeneratedKeys(); if(rs != null && rs.next()){
* System.out.println("Generated Emp Id: "+rs.getInt(1)); }
*/
} catch (Exception e) {
System.err.println(e.getMessage());
throw new SQLException(e);
} finally {
try {
if (pStmt != null)
pStmt.close();
} catch (Exception ex) {
}
}
return result;
}
以下是SQL_INSERT查询: INSERT INTO NonCICAccount(accountName,country,countryNumber,locationName,locationNumber,departmentName,de partmentNumber,division,major,minor,subminor,leru,task,approvingmanager,financialApprover)VALUES(?,?,?,?,?,?,? ,?,?,?,?,?,?,?,?)
请提供帮助和建议。