0

In the given problem the showstatus.jsp is not validating for the null value of the 'seat' variable. How do I add a validation to the 'seat' variable which will check whether the user has selected any seat or not before redirecting to another page? I tried adding all the annotations and all the validations of spring mvc but not getting the correct output.Actually I was trying to redirect to the same showseatstatus.jsp if the seat value is null.

controller.java

@RequestMapping("/showtimes/choose-slot/{show_id}/showseatstatus")
public String showSeatstatus(@PathVariable String show_id,HttpSession session,ModelMap m) {
    System.out.println(show_id);
    String str3=show_id;
    List<Show> shows = showdao.getShowByShowid(show_id);
    Iterator<Show> itr = shows.iterator();
    Show s=null;
    while (itr.hasNext()) {
         s = itr.next();
    }
    System.out.println(s.getSeat_status());
    session.setAttribute("str3", str3);
    m.addAttribute("s", s);
    return "showseatstatus";
}
@RequestMapping("/payment")
public String bookSeats(HttpServletRequest request,HttpSession session,ModelMap m) {
    String[] seats = request.getParameterValues("seat");
    int count=0;
    for(int i=0;i<seats.length;i++) {
        count=count+1;
    }
    session.setAttribute("count",count);
    String show_id = request.getParameter("show_id");

    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";
}

showseatstatus.jsp

<%
            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();
            out.print("<form action='" + baseUrl + "payment' method='get'>");
            int jmax;
            for (int i = 0, k = 0; i < 8; i++) {

                out.print("<span class='vertical rownumber'>S"+i+"</span>");

                if (i < 2 || i == 7){
                    jmax = 8; out.print("<span style='padding-left:32px'></span>");
                }
                else
                    jmax = 10;
                for (int j = 0; j < jmax; j++) {

                    if (j == jmax / 2)
                        out.print("<span style='padding-left:20px'></span>");
                    if (seatstatus.charAt(k++) == '1') {
                        out.print(
                                "<input type='checkbox' id='cb" + (k - 1) + "' name='seat' value='" + (k - 1) + "'> ");
                        out.print("<label for='cb" + (k - 1) + "'></label>");
                    } else {
                        out.print("<input type='checkbox' class='disabledcheckbox' id='cb" + (k - 1)
                                + "' name='seat' disabled='disabled'> ");
                        out.print("<label for='cb" + (k - 1) + "'></label>");
                        //out.print("<input type='checkbox' checked='checked' disabled='disabled' > ");
                    }
                }

                if (i < 2 || i == 7){
                    jmax = 8; out.print("<span style='padding-left:32px'></span>");
                }
                out.print("<br/>");

            }
            out.print("<hr>");
            out.print("<input type='hidden' name='show_id' value='" + 
            s.getShow_id() + "'>");
            out.print("<input type='submit' value='Book Seats'>");
        %>
EdChum
  • 376,765
  • 198
  • 813
  • 562
  • by just "adding an annotation" Spring won't automatically know what validation to perform. (depending on the annotation, and how and where added). Personally, I would recommend starting with splitting your front-end from your business logic – Stultuske Apr 19 '19 at 05:35
  • thanks for the answer. But can you please explain in more detail? – Manikya Aravind Apr 19 '19 at 05:54
  • It is explained here: https://stackoverflow.com/questions/5818101/why-business-logic-should-be-moved-out-of-jsp – Stephen C Apr 19 '19 at 06:27

0 Answers0