2016-12-05 131 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类以上的预订系统的...当我尝试保存数据它给我空指针异常

@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

我没有使用hibernate。我只使用spring mvc。这就是为什么。 –

+2

请提供堆栈跟踪。 –

回答

0

检查,一旦你得到了checkinDate /控制器中的checkOutdate可能并非来自JSP

+0

这就是我点击保存按钮时得到的名字:a 名字:ADF Room.ro_id:32 checkinDate:2016年12月15日 totalNights:4 checkoutDate:2016年12月8日 Customer.c_id:7 totalPrice:2000 状态:真 _status:上 出价:0 –

+0

谢谢大家的意见我想通了,我刚刚删除日历部分,并写入插入部分日历c = Calendar.getInstance(); booking.setCheckinDate(c.getTime()); booking.setCheckoutDate(c.getTime()); –

相关问题