Spring MVC中如何为seat变量添加非空校验,防止未选座跳转至支付页?
Got it, let's fix this seat selection validation issue. The problem right now is that when a user submits the form without picking any seats, request.getParameterValues("seat") returns null, and your loop tries to access seats.length which throws a NullPointerException. Plus, you want to redirect them back to the seat selection page instead of letting that happen. Here's how to do it properly in Spring MVC:
Step 1: Add Validation in the bookSeats Controller Method
Modify your bookSeats method to check if the seats array is null or empty first. If it is, redirect back to the showseatstatus page with the necessary show_id parameter, and optionally add an error message to notify the user.
@RequestMapping("/payment") public String bookSeats(HttpServletRequest request, HttpSession session, ModelMap m) { String[] seats = request.getParameterValues("seat"); String show_id = request.getParameter("show_id"); // Validate seat selection if (seats == null || seats.length == 0) { // Add an error message to session to display on the seat page session.setAttribute("errorMessage", "Please select at least one seat to book."); // Redirect back to the seat status page with the show_id return "redirect:/showtimes/choose-slot/" + show_id + "/showseatstatus"; } // Simplify seat count calculation int count = seats.length; session.setAttribute("count", count); Show show = showdao.getShowByShow_id(show_id); String event_id = show.getEvent_id(); Event ev = evdao.getEventByEventId(event_id); Booking booking = new Booking(); booking.setSeats(seats); booking.setShow(show); booking.setEvent(ev); m.addAttribute("booking", booking); return "payment-page"; }
Step 2: Display the Error Message in showseatstatus.jsp
Add a section in your JSP to show the error message when it exists in the session. Make sure to clear the message after displaying it so it doesn't persist on subsequent requests.
<% // Check for error message from session String errorMessage = (String) session.getAttribute("errorMessage"); if (errorMessage != null) { out.print("<div style='color: red; margin-bottom: 10px;'>" + errorMessage + "</div>"); // Remove the message from session so it doesn't show again session.removeAttribute("errorMessage"); } // Your existing JSP code starts here String url = request.getRequestURL().toString(); String baseUrl = url.substring(0, url.length() - request.getRequestURI().length()) + request.getContextPath() + "/"; Show s = (Show) request.getAttribute("s"); String seatstatus = s.getSeat_status(); // ... rest of your form code %>
Why Your Previous Annotation Attempts Might Have Failed
If you tried using @RequestParam(required = true) for the seat parameter, here's why it didn't work as expected:
- For array parameters like
String[] seats,required = truetells Spring to throw aMissingServletRequestParameterExceptionwhen the parameter is missing, instead of returningnull. To handle this gracefully, you'd need a global exception handler, but the manual check we did above is more straightforward for this scenario. - Alternatively, you could use
@RequestParam(value = "seat", required = false) String[] seatsand then perform the null/empty check, which is exactly what we did here.
内容的提问来源于stack exchange,提问作者Manikya Aravind

