2016-12-05 2 views
1

Я получаю исключение исключающего указателя, в то время как я пытаюсь сохранить входы из jsp, который также включает дату. Я не могу найти, где ошибка.Как вставить дату в базу данных с помощью Spring mvc?

public class Booking { 

    private int bId; 
    private String firstName; 
    private String lastName; 
    private Room room; 
    private Date checkinDate; 
    private int totalNights; 
    private Date checkoutDate; 
    private int totalPrice; 
    private Customer customer; 
    private boolean status; 

    public Booking() { 
    } 

    public Booking(int bId, String firstName, String lastName, Room room, Date checkinDate, int totalNights, Date checkoutDate, int totalPrice, Customer customer, boolean status) { 
     this.bId = bId; 
     this.firstName = firstName; 
     this.lastName = lastName; 
     this.room = room; 
     this.checkinDate = checkinDate; 
     this.totalNights = totalNights; 
     this.checkoutDate = checkoutDate; 
     this.totalPrice = totalPrice; 
     this.customer = customer; 
     this.status = status; 
    } 

    public int getbId() { 
     return bId; 
    } 

    public void setbId(int bId) { 
     this.bId = bId; 
    } 

    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 Room getRoom() { 
     return room; 
    } 

    public void setRoom(Room room) { 
     this.room = room; 
    } 

    public Date getCheckinDate() { 
     return checkinDate; 
    } 

    public void setCheckinDate(Date checkinDate) { 
     this.checkinDate = checkinDate; 
    } 

    public int getTotalNights() { 
     return totalNights; 
    } 

    public void setTotalNights(int totalNights) { 
     this.totalNights = totalNights; 
    } 

    public Date getCheckoutDate() { 
     return checkoutDate; 
    } 

    public void setCheckoutDate(Date checkoutDate) { 
     this.checkoutDate = checkoutDate; 
    } 

    public int getTotalPrice() { 
     return totalPrice; 
    } 

    public void setTotalPrice(int totalPrice) { 
     this.totalPrice = totalPrice; 
    } 

    public Customer getCustomer() { 
     return customer; 
    } 

    public void setCustomer(Customer customer) { 
     this.customer = customer; 
    } 

    public boolean isStatus() { 
     return status; 
    } 

    public void setStatus(boolean status) { 
     this.status = status; 
    } 

    @Override 
    public String toString() { 
     return "Booking{" + "bId=" + bId + ", firstName=" + firstName + ", lastName=" + lastName + ", room=" + room + ", checkinDate=" + checkinDate + ", totalNights=" + totalNights + ", checkoutDate=" + checkoutDate + ", totalPrice=" + totalPrice + ", customer=" + customer + ", status=" + status + '}'; 
    } 

} 

класс контроллера системы выше бронирования ... Когда я пытаюсь сохранить данные, которые он дает мне исключения нулевого указателя

@Controller 
@RequestMapping(value = "/admin/booking") 
public class BookingController { 

    @Autowired 
    private BookingService bookingService; 
    @Autowired 
    private CustomerService customerService; 
    @Autowired 
    private RoomService roomService; 

    @RequestMapping(method = RequestMethod.GET) 
    public String index(ModelMap map) throws SQLException { 
     map.addAttribute("Booking", bookingService.getALL()); 
     return "admin/booking/index"; 
    } 

    @RequestMapping(value = "/add", method = RequestMethod.GET) 
    public ModelAndView add(HttpServletRequest request) throws SQLException { 
     ModelAndView mv = new ModelAndView("/admin/booking/add"); 
     String c_id = null; 
     String ro_id = null; 
     if (request.getParameter("c_id") != null && !request.getParameter("c_id").equals("")) { 
      c_id = request.getParameter("c_id"); 
     } 
     if (request.getParameter("ro_id") != null && !request.getParameter("ro_id").equals("")) { 
      ro_id = request.getParameter("ro_id"); 
     } 
     mv.addObject("Booking", new Booking()); 
     mv.addObject("Customer", customerService.getALL()); 
     mv.addObject("Room", roomService.getAll()); 
     return mv; 
    } 

@RequestMapping(value = "/save", method = RequestMethod.POST) 
    public String save(@ModelAttribute("Booking") Booking booking, BindingResult result, HttpServletRequest request) throws SQLException, ParseException { 
     Calendar calendar = Calendar.getInstance(); 
     booking.setCheckinDate(calendar.getTime()); 
     DateFormat formatDate1 = new SimpleDateFormat("yyyy-mm-dd"); 
     Date formatedDate1 = (Date) formatDate1.parse(request.getParameter("chekinDate")); 
     booking.setCheckinDate(formatedDate1); 
     booking.setCheckoutDate(calendar.getTime()); 
     DateFormat formatDate2 = new SimpleDateFormat("yyyy-mm-dd"); 
     Date formatedDate2 = (Date) formatDate2.parse(request.getParameter("chekoutDate")); 
     booking.setCheckoutDate(formatedDate2); 

     Customer customer = customerService.getById(booking.getCustomer().getC_id()); 
     Room room = roomService.getById(booking.getRoom().getRo_id()); 
     try { 
      booking.setCustomer(customer); 
      booking.setRoom(room); 

      if (booking.getbId() == 0) { 
       bookingService.insert(booking); 

      } else { 
       bookingService.update(booking); 
      } 
     } catch (SQLException ex) { 

     } 
     return "redirect:/admin/booking"; 
    } 
} 

<

%@include file="../header.jsp" %> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> 
<h1>Add Booking</h1> 
<form:form modelAttribute= "Booking" action="${SITE_URL}/admin/booking/save" method="post" role="form"> 
    <div class="form-group"> 
     <label>First Name</label> 
     <form:input path="firstName" placeholder="Enter First Name" required="required" class="form-control"/> 
    </div> 
    <div class="form-group"> 
     <label>Last Name</label> 
     <form:input path="lastName" placeholder="Enter Last Name" required="required" class="form-control"/> 
    </div> 

    <div class="form-group"> 
     <label>RoomId</label> 
     <form:select name="Room.ro_id" path="Room.ro_id" class="form-control"> 
      <form:option value="0">Select Room</form:option> 
      <c:forEach var="room" items="${Room}"> 
       <form:option value="${room.getRo_id()}">${room.getRo_id()} 

       </form:option> 
      </c:forEach> 
     </form:select> 
    </div>  


    <!--<div class="form-group"> 
     <label>Room Price</label> 
    <form:input path="room.roomPrice" name="room.roomPrice"placeholder="Enter email" required="required" class="form-control"/> 
</div> 
<div class="form-group"> 
    <label>Room Type</label> 
    <form:input path="room.roomType" name="room.roomType"placeholder="Enter email" required="required" class="form-control"/> 
</div>--> 
    <div class="form-group"> 
     <label>Checkin</label> 
     <form:input path="checkinDate" name="checkinDate" type="date" placeholder="Enter password" required="required" class="form-control"/> 
    </div> 
    <div class="form-group"> 
     <label>Total Nights</label> 
     <form:select name="totalNights" path="totalNights" class="form-control"> 
      <form:option value="0">Choose nights</form:option> 
      <form:option value="1">1</form:option> 
      <form:option value="2">2</form:option> 
      <form:option value="3">3</form:option> 
     </form:select> 
    </div> 

    <div class="form-group"> 
     <label>Check out Date</label> 
     <form:input path="checkoutDate" type="date" name="checkoutDate"placeholder="Enter password" required="required" class="form-control"/> 

     <div class="form-group"> 
      <label>Booked by</label> 
      <form:select name="Customer.c_id" path="Customer.c_id" class="form-control"> 
       <form:option value="0">Guest</form:option> 
       <c:forEach var="customer" items="${Customer}"> 
        <form:option value="${customer.getC_id()}">${customer.getFirstName()} 
        </form:option> 
       </c:forEach> 
      </form:select> 
     </div> 
     <div class="form-group"> 
      <label>Total Price</label> 
      <form:input path="totalPrice" name="totalPrice" placeholder="total price" required="required" class="form-control"/> 
     </div> 
     <div class="form-group"> 
      <label>Terms and Condition</label> 
      <form:checkbox path="status" name="status" placeholder="Enter status" required="required" class="form-control"/> 
     </div> 
     <form:hidden path="bId" name="bId"/> 
     <div class="form-group"> 
      <button type="submit" class="btn btn-success" >Save</button> 
     </div> 
    </form:form> 
+0

Почему вы не разместили аннотацию в своем бронировании класс? –

+0

Я не использую спящий режим. Я использую только весенний mvc. Вот почему. –

+2

Просьба также предоставить стек. –

ответ

0

Проверить, как только вы получили checkinDate/checkOutdate в контроллере может быть не из JSP

+0

. Это то, что я получаю, когда я нажимаю кнопку сохранения firstName:ЬавЬЫате: ADF Room.ro_id: 32 checkinDate: 2016-12-15 totalNights: 4 checkoutDate: 2016-12-08 Customer.c_id: 7 totalPrice: 2000 Статус: истинно _status: на BID : 0 –

+0

Спасибо, ребята, за ваши комментарии Я понял, что просто удалил календарную часть и написал внутри раздела вставки Календарь c = Calendar.getInstance(); booking.setCheckinDate (c.getTime()); booking.setCheckoutDate (c.getTime()); –

Смежные вопросы