2017-08-01 85 views
0

我在將JSP站點傳遞給mvc控制器的變量時遇到問題。您可以在下面看到我的JSP頁面和相應的mvc控制器。我設法獲取數據,但我的意圖是將用戶在網站上輸入的數據插入數據庫並在其他站點上顯示。我設法得到了vaariables的值,但第一次我打開它們設置爲null的網站,這使我的sql查詢崩潰。有沒有一種方法可以將值設置爲變量,而我點擊提交按鈕?此外,同時我試圖讓一個如果排除空值(如下面的代碼)我得到以下錯誤:從JSP傳遞值到spring mvc控制器錯誤

ERROR

There was an unexpected error (type=Internal Server Error, status=500). 
No message available 

addCustomer.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<!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=UTF-8"> 
<title>Add Customer</title> 
</head> 
<body> 
    <body> 
    <form:form method="GET" action="addCustomer" commandName="Residents"> 
     <div align="center"> 
      <h2> Add Customer </h2> 
      <table> 
       <tr> 
        <td>First Name</td> 
        <td><input type="text" name="name" /></td> 

       </tr> 
       <tr> 
        <td>Surame</td> 
        <td><input type="text" name="surname" /></td> 
       </tr> 
       <tr> 
        <td>Check in Date</td> 
        <td><input type="date" name="checkInDate"</td> 
       </tr> 
       <tr> 
        <td>Check out Date</td> 
        <td><input type="date" name="checkOutDate"</td> 
       </tr> 

       <tr> 
        <td>Email</td> 
        <td><input type="email" name="email"></td> 
       </tr> 

       <tr> 
        <td>Id</td> 
        <td><input type="number" name="residentId"></td> 
       </tr> 

       <tr> 
        <td>Room ID</td> 
        <td><input type="number" name="roomId"</td> 


       </tr> 
       <tr> 
        <td></td> 
        <td><input type="submit" value="send" /></td> 
        <td/> 
        <td><input type="submit" value="Cancel"/></td> 
       </tr> 

      </table> 
      <div style="color: red">${error}</div> 
     </div> 
    </form:form> 
</body> 
</html> 

Residents.java

package com.stapor.bartlomiej.Model; 

import java.sql.Date; 

/** 
* Created by Bartek on 2017-07-30. 
*/ 
public class Residents { 
    private String name; 
    private String surname; 
    private Date checkInDate; 
    private Date checkOutDate; 
    private String email; 
    private int id; 
    private int roomId; 

    public Residents(int id, String name, String surname, Date checkInDate, Date checkOutDate, String email, int roomId) { 
     this.name = name; 
     this.surname = surname; 
     this.checkInDate = checkInDate; 
     this.checkOutDate = checkOutDate; 
     this.email = email; 
     this.id = id; 
     this.roomId = roomId; 
    } 

    public Residents(String name, String surname, Date checkInDate, Date checkOutDate, String email, int roomId) { 
     this.name = name; 
     this.surname = surname; 
     this.checkInDate = checkInDate; 
     this.checkOutDate = checkOutDate; 
     this.email = email; 
     this.roomId = roomId; 
    } 

    public Residents() { 
    } 


    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getSurname() { 
     return surname; 
    } 

    public void setSurname(String surname) { 
     this.surname = surname; 
    } 

    public Date getCheckInDate() { 
     return checkInDate; 
    } 

    public void setCheckInDate(Date checkInDate) { 
     this.checkInDate = checkInDate; 
    } 

    public Date getCheckOutDate() { 
     return checkOutDate; 
    } 

    public void setCheckOutDate(Date checkOutDate) { 
     this.checkOutDate = checkOutDate; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    public int getId() { 
     return id; 
    } 

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

    public int getRoomId() { 
     return roomId; 
    } 

    public void setRoomId(int roomId) { 
     this.roomId = roomId; 
    } 

    @Override 
    public String toString() { 
     return String.format(
       "Id: %d<br>Name: %s<br>Surname: %s<br>Check In Date: %s<br>Check Out Date: %s<br>E-mail: %s<br>Room ID: %d<br>", 
       id, name, surname, checkInDate, checkOutDate, email, roomId); 


    } 
} 

MVC控制器

@RequestMapping(value = "/addCustomer", method = RequestMethod.GET) 
    public String addCustomer(@ModelAttribute("Residents") Residents residents, HttpServletRequest request) 
    { 

     //System.out.println(name + "\n" + surname + "\n" + checkInDate + "\n" + checkOutDate + "\n" + email + "\n" + residentId + "\n" + roomId); 

      //System.out.println(request.getParameter("checkInDate")); 
      String[] params = {request.getParameter("checkInDate"), 
        request.getParameter("checkOutDate"), 
        request.getParameter("name"), 
        request.getParameter("surname"), 
        request.getParameter("residentId"), 
        request.getParameter("email"), 
        request.getParameter("roomId") 
      }; 
      for(String k : params) 
      { 
       System.out.println(k); 
      } 

      if(params[0].isEmpty()) 
      { 
       System.out.println("shiet"); 
      } 
      else 
      { 
       String sql = "insert into residents(Check_in_Date, Check_out_Date, Name, Surname, ResidentId, email, roomId) values " + 
         "('" + params[0] + "', '" + params[1] + 
         "', '" + params[2] + "', '" + params[3] + "', '" + 
         params[4] + "', '" + params[5] + "', " + params[5] + ");"; 
       jdbcTemplate.batchUpdate(sql); 
      } 

     //System.out.println("" + request.getParameter("name")); 
     // map.put("residents", residents); 
     // map.put("name", request.getParameter("name")); 

     return "addCustomer"; 
    } 

從GET更改爲POST不能解決問題。下面的錯誤代碼:

There was an unexpected error (type=Internal Server Error, status=500). 
StatementCallback; SQL [insert into residents(Check_in_Date, Check_out_Date, Name, Surname, ResidentId, email, roomId) values ('null', 'null', 'null', 'null', 'null', 'null', null);]; Cannot parse "DATE" constant "null"; SQL statement: insert into residents(Check_in_Date, Check_out_Date, Name, Surname, ResidentId, email, roomId) values ('null', 'null', 'null', 'null', 'null', 'null', null) -- ('null', 'null', 'null', 'null', 'null', 'null', NULL) [22007-191]; nested exception is org.h2.jdbc.JdbcBatchUpdateException: Cannot parse "DATE" constant "null"; SQL statement: insert into residents(Check_in_Date, Check_out_Date, Name, Surname, ResidentId, email, roomId) values ('null', 'null', 'null', 'null', 'null', 'null', null) -- ('null', 'null', 'null', 'null', 'null', 'null', NULL) [22007-191] 
+1

對於表單提交,請使用post方法,而不是方法 – Perry

+0

嘗試過你的方式,但它仍然不起作用。 – elec

回答

0

使用Post方法在addCustomer.jsp頁面這樣

<form:form method="POST" action="addCustomer" commandName="Residents"> 

然後在你的控制器類,注意@ModelAttribute將自動綁定你addCustomer.jsp表單元素屬性的值與相應的Residents類屬性。所以你不必做所有這些request.getParameter("checkOutDate")。春天已經完成了,只是通過@ModelAttribute("Residents") Residents residents作爲addCustomer方法的參數,就像你做的那樣。

所以先走一步,取得這樣的

@RequestMapping(value = "/addCustomer", method = RequestMethod.POST) 
public String addCustomer(@ModelAttribute("Residents") Residents residents){ 

    //Just some printouts for confirmation 
    System.out.println(residents.getName()); 
    System.out.println(residents.getSurname()); 

    //Persist data 

} 
0

您需要添加<form:input path="" />在JSP形式的所有輸入變量的數值。

相關問題