微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何通过休眠验证器在Spring控制器中验证@SessionAttribute对象?

如何解决如何通过休眠验证器在Spring控制器中验证@SessionAttribute对象?

如何验证会话属性对象? 我正在使用HttpSession创建一个酒店预订Web应用程序,以在请求之间存储数据。 用户填写完所有信息后,将通过邮寄请求将其发送,并且必须在后端进行验证。 以下代码行导致错误

@SessionAttribute("reservationDto") @Valid ReservationDto reservation,BindingResult result

错误如下:

java.lang.IllegalStateException: An Errors/BindingResult argument is expected to be declared immediately after the model attribute,the @RequestBody or the @RequestPart arguments to which they apply: public java.lang.String pl.maciejdluzen.hotelreservation.controllers.ReservationController.createReservation(pl.maciejdluzen.hotelreservation.dtos.ReservationDto,org.springframework.validation.BindingResult,pl.maciejdluzen.hotelreservation.dtos.CardDetailsDto,org.springframework.ui.Model)

我想BindingResult对象不能以@SessionAttribute("reservationDto") @Valid ReservationDto reservation开头,那么如何验证来自reservationDto的{​​{1}}方法中的所有信息?

请查看我的整个控制器:

createReservation

还有@Controller @RequestMapping("/") @SessionAttributes("reservationDto") public class ReservationController { private Logger LOG = LoggerFactory.getLogger(getClass()); private final HotelService hotelService; private final GuestService guestService; public ReservationController(HotelService hotelService,GuestService guestService) { this.hotelService = hotelService; this.guestService = guestService; } @GetMapping public String home(Model model) { model.addAttribute("hotelsNames",hotelService.findAllHotelsNames()); model.addAttribute("reservationDto",new ReservationDto()); return "index"; } @PostMapping("reservation/roomselection") public String getRoomSelectionPage(@modelattribute("reservationDto") ReservationDto reservationDto,HttpSession session,Model model) { ReservationDto reservation = (ReservationDto) session.getAttribute("reservationDto"); LOG.info("Hotel Name form the session: {} and session id {} and creation time: {},checkin {} ",reservation.getHotelName(),session.getId(),session.getCreationTime(),reservation.getCheckInDate()); model.addAttribute("reservationDto",reservation); return "reservation/roomselection"; } @GetMapping("auth/guest/reservation/details") public String getReservationDetailsPage( @SessionAttribute("reservationDto") ReservationDto reservation,HttpServletRequest request,Principal principal,Model model) { String param = request.getParameter("roomType"); reservation.setRoomTypeName(param); reservation.setUsername(principal.getName()); LOG.info("Username: {}",reservation.getUsername()); reservation.setGuestName(guestService.findGuestNameByUsername(reservation.getUsername())); model.addAttribute("reservationDto",reservation); model.addAttribute("cardDetails",new CardDetailsDto()); LOG.info("Hotel Name form the session: {},roomType {} and session id {} and creation time: {},checkin {} and username {}",reservation.getRoomTypeName(),reservation.getCheckInDate(),principal.getName()); return "reservation/details"; } @PostMapping("auth/guest/reservation/details") public String createReservation(@SessionAttribute("reservationDto") @Valid ReservationDto reservation,BindingResult result,@modelattribute("cardDetails") @Valid CardDetailsDto cardDetails,BindingResult result2,Model model) { if(result2.hasErrors() || result.hasErrors()) { return "reservation/details"; } LOG.info("Reservation: {}",reservation.getGuestName()); LOG.info("CardDetails: {}",cardDetails.getCardNumber()); return "redirect:/auth/guest/reservation/summary"; } @GetMapping("/auth/guest/reservation/summary") public String getReservationSummary() { return "reservation/summary"; } } 类:

ReservationDto

所有建议都将受到欢迎!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。