我想通过日期选择器接收日期,并将其放入存储预订信息(RentalPlacesVo)的对象中。我收到了来自预订表单的日期,并确认控制器与vo对象非常匹配(甚至rental_date中的数据类型也是java.sql.Date)。付款后,我将其发送给Ajax与其他信息一起包含,在处理Ajax的过程中,我确认将2022-07-23更改为1992。似乎日期没有正确地存储在db的日期类型列中,因为日期已更改为1992。
我需要在vo对象上存储DATE类型的数据,我怎么才能得到它?
错误的详细信息如下:
警告:org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver -绑定请求元素失败:org.springframework.web.method.annotation.MethodArgumentTypeMismatchException:转换java.lang类型值失败。字符串'到所需类型'java.sql.Date';org.springframework.core.convert.ConversionFailedException:从类型转换失败。输入[@org.springframework.web.bind.annotation]。RequestParam java.sql。日期]表示值'1992';嵌套异常是java.lang.IllegalArgumentException
RentalPlacesVo
package com.joinus.domain;
import java.sql.Date;
import lombok.Data;
@Data
public class RentalPlacesVo {
private int rental_places_no;
private String reservation_no;
private int club_no;
private int member_no;
private int partner_place_no;
private int payment_no;
private Date rental_date;
private int rental_time_no;
private int rental_status;
}
reservationForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ include file="../include/header.jsp"%>
<script type="text/javascript" src="//dapi.kakao.com/v2/maps/sdk.js?appkey="appkey"&libraries=services"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script type="text/javascript">
// Calendar
var config = {
dateFormat : 'yy-mm-dd',
showOn : "button",
buttonText : "선택",
prevText : '이전 달',
nextText : '다음 달',
monthNames : ['1월','2월','3월','4월','5월','6월','7월','8월','9월','10월','11월','12월'],
monthNamesShort : ['1월','2월','3월','4월','5월','6월','7월','8월','9월','10월','11월','12월'],
dayNames : ['일','월','화','수','목','금','토'],
dayNamesShort : ['일','월','화','수','목','금','토'],
dayNamesMin : ['일','월','화','수','목','금','토'],
yearSuffix : '년',
minDate : 1
}
$(function(){
$("input[name='rental_date']").datepicker(config);
});
$(document).ready(function(){
$(':button').attr('class','btn btn-primary');
$('#subBtn').click(function(){
// var date = $('#rental_date').val(); // 2022-07-23
if($('#rental_date').val() == ''){
$('#rental_date').focus();
return false;
}
if($('#rental_time_no').val() == ''){
$('#rental_time_no').focus();
return false;
}
if($('#memberCnt').val() == ''){
$('#memberCnt').focus();
return false;
}
if($('#memberCnt').val() > 20){
alert('Max MemberCnt : 20');
$('#memberCnt').focus();
return false;
}
});
});
</script>
<div class="container-xxl py-5">
<div class="container" style="color: black;">
<div class="row g-5">
<h1 id="partnerPlaceName">${partnerPlace.partner_place_name }</h1>
<div style="width: 70%;">
<img style="width: 95%; max-height: 550px;" src="${PageContext.request.contextPath }/resources/upload/partner_place/${partnerPlace.partner_place_image}">
<div style="margin-top: 3em;">
<pre class="boardContent">${partnerPlace.partner_place_content }</pre>
</div>
<div style="margin-bottom: 16px;">
<i class="fa fa-phone-alt me-3" aria-hidden="true"></i>${partnerPlace.partner_place_tel }
</div>
<div id="partnerPlaceAddr" style="margin-bottom: 16px;">
${partnerPlace.partner_place_address }
</div>
</div>
<div style="width: 30%; border: 1px solid #32C36C; padding-top: 1em; height: 70%;">
<form name="fr" action="" method="post">
<input type="hidden" name="partner_place_no" value="${partner_place_no }">
<input type="hidden" name="partner_place_name" value="${partnerPlace.partner_place_name }">
<input type="hidden" name="partner_place_price" value="${partnerPlace.partner_place_price }">
<div>
<div style="font-size: x-large; float: left;">
${partnerPlace.partner_place_name }
</div>
<div style="color: #32C36C; text-align: right; margin-bottom: 2em;">
<span style="font-size: x-large;">
<fmt:setLocale value="ko_KR"/><fmt:formatNumber type="currency" value="${partnerPlace.partner_place_price }" />
<br>
</span>
<span style="color: #9B9B9B;"> /hour</span>
</div>
</div>
<div>
MemberCnt <input type="number" class="form-control" id="memberCnt" name="memberCnt" max="20" required>
</div>
<script type="text/javascript">
var memberCnt = 0;
var totalPrice = 0;
$('#memberCnt').blur(function(){
memberCnt = $('#memberCnt').val();
totalPrice = memberCnt * ${partnerPlace.partner_place_price} * 2
document.getElementById("seePrice").innerHTML = totalPrice.toLocaleString();
$('#payment_price').val(totalPrice);
});
</script>
<!-- Date -->
<div style="margin-bottom: 2em;">
Select Date (Click button)
<input class="form-control" id="rental_date" name="rental_date" autocomplete="off" readonly style="background-color: white;">
</div>
<!-- Time -->
<div style="margin-bottom: 2em;">
Time
<select class="form-select" id="rental_time_no" name="rental_time_no">
<option value="">Select Time</option>
<option value="1">10:00~12:00</option>
<option value="2">12:00~14:00</option>
<option value="3">14:00~16:00</option>
<option value="4">16:00~18:00</option>
<option value="5">18:00~20:00</option>
<option value="6">20:00~22:00</option>
</select>
</div>
<!-- TotalPrice -->
<div>
TotalPrice<br>
<span id="seePrice"></span>
<input type="hidden" id="payment_price" name="payment_price">
</div>
<div class="payBtn">
<input type="submit" class="btn btn-primary rounded-pill py-3 px-5" id="subBtn" value="pay">
</div>
</form>
</div>
</div>
</div>
</div>
<%@ include file="../include/footer.jsp"%>
控制器
@RequestMapping(value = "/partnerPlaces/{partner_place_no}", method = RequestMethod.POST)
public String partnerPlaceContentPost(PartnerPlacesVo partnerplacevo ,PaymentsVo paymentvo,
RentalPlacesVo rentalplacevo, Model model, @RequestParam("rental_time_no") int rentaltimeno,
HttpSession session,@RequestParam("payment_price") int payment_price) {
log.info(" partnerPlaceContentPost() 호출");
// log.info("@@@@@rental_date : "+rental_date); // 2022-07-23
log.info("rentalplacevo : "+rentalplacevo.getRental_date());
log.info("rental_date type: "+rentalplacevo.getRental_date().getClass().getName()); // java.sql.Date
String ppname = partnerplacevo.getPartner_place_name();
model.addAttribute("ppname", ppname);
model.addAttribute("payment_price", payment_price);
log.info("ppname, totalprice: "+ppname+payment_price);
MembersVo vo = (MembersVo)session.getAttribute("member");
model.addAttribute("members", vo);
model.addAttribute("rental_time_no", rentaltimeno);
model.addAttribute("payment", paymentvo);
model.addAttribute("rentalplacevo", rentalplacevo);
return "/rental/payment";
}
payment.jsp(使用API)
if(rsp.success){
$.ajax({ //verify
type : 'POST',
url : '${PageContext.request.contextPath }/rental/verifyIamport/'+rsp.imp_uid
}).done(function(result){
// rsp.paid_amount와 result.response.amount(서버 검증) 비교 후 로직 실행
if(rsp.paid_amount === result.response.amount){
alert("success pay varify"); console.log("success pay varify");
var rental_date = ${rentalplacevo.rental_date};
alert("rental_date : "+rental_date); // 1992
$.ajax({
url : '${PageContext.request.contextPath }/rental/partnerPlaces/'+${payment.partner_place_no}+'/payment',
type :'POST',
data :{'partner_place_price':${payment.partner_place_price},
'rental_date':${rentalplacevo.rental_date},
'payment_price':${payment_price},
'rental_time_no':${rental_time_no}},
dataType: 'json',
success: function(paymentvo){
alert('Success store all data');
setTimeout(function(){
location.href="${PageContext.request.contextPath }/"
},5000);
}
});
}
}).fail(function(error){
console.log("fail to store data");
});
}else{
alert('fail'+'errorCode : '+rsp.error_code+'errMsg : '+rsp.error_message);
}
控制器-我到不了这里。
@ResponseBody
@RequestMapping(value="/verifyIamport/{imp_uid}")
public IamportResponse<Payment> paymentByImpUid(
Model model, Locale locale, HttpSession session
, @PathVariable("imp_uid") String imp_uid ) throws IamportResponseException, IOException{
log.info("paymentByImpUid");
return api.paymentByImpUid(imp_uid);
}
@ResponseBody
@RequestMapping(value ="/partnerPlaces/{partner_place_no}/payment",method=RequestMethod.POST)
public PaymentsVo payment( Model model,
@RequestParam("partner_place_price") int partner_place_price,
@RequestParam("rental_date") Date rental_date,
@RequestParam("payment_price") int payment_price,
@RequestParam("rental_time_no") int rental_time_no,
@PathVariable("partner_place_no") int partner_place_no,
RentalPlacesVo rentalplacevo, PaymentsVo paymentvo,HttpSession session){
log.info("Vo Info . paymentvo : "+paymentvo);
log.info("Vo Info . RentalPlacesVo : "+rentalplacevo);
log.info("rental_date : "+rental_date);
// log.info("rentdate dataType : "+rentdate.getClass().getName());
// reservationNum (Date-place_no)
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Calendar cal = Calendar.getInstance();
java.util.Date date = cal.getTime();
String rsNum = sdf.format(date)+"-"+partner_place_no;
log.info(reservationNum"+rsNum);
paymentvo.setReservation_no(rsNum);
MembersVo mvo = (MembersVo)session.getAttribute("member");
paymentvo.setMember_no(mvo.getMember_no());
paymentvo.setPartner_place_no(partner_place_no);
paymentvo.setPartner_place_price(partner_place_price);
paymentvo.setPayment_status(1);
Integer pay = rentalService.pay(paymentvo);
rentalplacevo.setPayment_no(paymentvo.getPayment_no());
if(pay == 1) {
log.info("success");
}else {
log.info("fail");
}
rentalplacevo.setClub_no(46);
rentalplacevo.setMember_no(mvo.getMember_no());
rentalplacevo.setRental_places_no(partner_place_no);
rentalplacevo.setReservation_no(rsNum);
rentalplacevo.setRental_date(rental_date); // date
rentalplacevo.setRental_time_no(rental_time_no); // time
rentalplacevo.setRental_status(1);
rentalService.place(rentalplacevo);
log.info("rentalPlace : "+rentalplacevo);
return paymentvo;
}
在日期字段上使用与日期相关的注释
@Temporal(TemporalType.DATE)
@DateTimeFormat(pattern = "yyyy/MM/dd")
private Date rental_date;
最佳实践:对于pojo实体类变量,如rentalDate,建议使用驼角大小写标准。.