java - 如何使用 spring mvc 将日期插入数据库?

标签 java spring jsp model-view-controller

当我尝试保存来自jsp(也包括日期)的输入时,我收到空指针异常。我无法找到错误在哪里。

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>

最佳答案

检查 Controller 中的 checkinDate/checkOutdate 可能不是来自 JSP

关于java - 如何使用 spring mvc 将日期插入数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40971568/

相关文章:

表单对象的 JavaScript 大小

java - 使用 JSTL 在 Select 标签中填充所选项目?

java - 将字符串中的每个字母替换为字母表中的下一个字母,但保留空格和标点符号?

java - 我们如何排除一些单元测试在 Android 项目中运行

java - @Cacheable 无法按预期在启动时加载内容

java - 如何从 BindingResult 获取 Controller 中的错误文本

java - 如何将 ActionListener 添加到扩展 JButton 的类的实例?

java - 使用 Jackson java 库动态更改 JsonProperty 名称

javascript - AngularJS 身份验证与 Spring Security CORS 问题

java - JSP 文件输出 If/Else 问题