具有可选数据库参数的JSP表单

时间:2018-06-25 05:57:05

标签: spring spring-mvc jsp spring-boot jdbc

我很好奇如何在jsp表单中具有可选参数。我有一个用以下命令创建的数据库表

create table SomeTable(ID not null identity(1,1), EmpID int, AssetID int, 
Start date not null, End date, primary key(ID));

使用spring-boot的jdbc模板,我可以使用jsp表单将记录成功添加到表中。在当前表单中,我有一个框要为End添加一个条目,并且在不向该字段输入值的情况下无法使用。这是对应的jsp文件

<body>
    <h1>Add Loan Form</h1>
    <div>
    <a href="/">Home</a>
    </div>
    <form:form action="addLoan" method="post" modelAttribute="loan" >
        <table>
            <tr>
                <td>Employee ID</td>
                <td><form:input path="employeeID"/></td>
            </tr>
            <tr>
                <td>Asset ID</td>
                <td><form:input path="assetID"/></td>
            </tr>
            <tr>
                <td>Start Date</td>
                <td><form:input path="startDate"/></td>
            </tr>
            <tr>
                <td>End Date</td>
                <td><form:input path="endDate"/></td>
            </tr>
            <tr>
                <td></td>
                <td><form:button>Submit</form:button></td>
            </tr>
        </table>
    </form:form>
</body>

我很好奇是否可以将“结束日期”字段设为可选内容?我想保留它,以防用户确实要输入一个,但是并不总是必需/已知的。

我的控制器类

//LOAN ADD CONTROLLERS
@ModelAttribute("loan")
public Loan setUpLoanForm() {
    return new Loan();
}

@GetMapping("LoanAddForm")
public String addLoanForm(){
    return "addLoanForm";
}

@PostMapping("addLoan")
public ModelAndView addLoan(@ModelAttribute("loan") Loan loan, Model model) {
    String flag = assetDAO.addLoan(loan);

    if(!flag.equals("loanList"))
        return new ModelAndView(flag, "", null);
    List<Loan> list = new ArrayList<>();
    list = assetDAO.getAllLoanRecords();
    return new ModelAndView("loanList", "loan", list);

}

(为简洁起见,省略了非相关零件)

和我的DAO班

public String addLoan(Loan loan) {
    query             = "insert into Loan values (?,?,?,?)";
    String checkEmp   = "select count(*) from Loan where EmployeeID = ?";
    String checkAsset = "select count(*) from Loan where AssetID = ?";

    System.out.println(loan.getStartDate().compareTo(loan.getEndDate()));
    //=== Check to see that the dates we have entered make sense
    if(loan.getStartDate().compareTo(loan.getEndDate()) >= 0)
        return "dateError";
    //============================================
    //=== Check to see we have records in the respective Employee and Asset tables
    int count = jdbcTemplate.queryForObject("select count(*) from Employee where ID = ?",
            new Object[] {loan.getEmployeeID()}, Integer.class);
    if(count != 1)
        return "noEmployee";

    count = jdbcTemplate.queryForObject("select count(*) from Asset where ID = ?", 
            new Object[] {loan.getAssetID()}, Integer.class);
    if(count != 1)
        return "noAsset";
    //============================================
    //=== Check to see if the record already exists
    count = jdbcTemplate.queryForObject(checkEmp, new Object[] {loan.getEmployeeID()}, Integer.class);
    if(count != 0)
        return "employeeHasLoan";
    count = jdbcTemplate.queryForObject(checkAsset,new Object[] {loan.getAssetID()}, Integer.class);
    if(count != 0)
        return "assetHasLoan";
    //============================================
    jdbcTemplate.update(query, new Object[]{loan.getEmployeeID(),loan.getAssetID(),loan.getStartDate(), loan.getEndDate()});

    return "loanList";


}//end addLoan()

(为简洁起见,再次删除非相关零件)

贷款班

public class Loan {

private int id, employeeID, assetID;
private java.sql.Date startDate, endDate;
private String employeeName;

public Loan(int id, int employeeID, int assetID, java.sql.Date startDate, java.sql.Date endDate, String employeeName) {
    this.id         = id;
    this.employeeID = employeeID;
    this.assetID    = assetID;
    this.startDate  = startDate;
    this.endDate    = endDate;
}

public Loan() {}

public String getEmployeeName() {
    return this.employeeName;
}

public void setEmployeeName(String employeeName) {
    this.employeeName = employeeName;
}

public int getId() {
    return this.id;
}

public void setId(int id) {
    this.id = id;
}

public int getEmployeeID() {
    return this.employeeID;
}

public void setEmployeeID(int employeeID) {
    this.employeeID = employeeID;
}

public int getAssetID() {
    return this.assetID;
}

public void setAssetID(int assetID) {
    this.assetID = assetID;
}

public java.sql.Date getStartDate(){
    return this.startDate;
}

public void setStartDate(java.sql.Date startDate) {
    this.startDate = startDate;
}

public java.sql.Date getEndDate(){
    return this.endDate;
}

public void setEndDate(java.sql.Date endDate) {
    this.endDate = endDate;
}

}

0 个答案:

没有答案