Announcement Announcement Module
Collapse
No announcement yet.
Binding problem after clicking back button Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Binding problem after clicking back button

    Hi there,

    I have a class that extends the AbstractWizardFormController with 3 jsp pages, although I am only dealing with the first 2 at the moment.

    I get data from the first page from 2 drop down boxes and display it on the next, this is fine. The problem occurs when I click on the back button, select the same or new options and submit these options.

    The onBindAndValidate method is not called for the first page, therefore the new data cannot be bound to the command object.

    By my understanding the setAllowedDirtyBack is set to true by default??

    How do I re-bind the data after clicking on the back button??

    My controller and jsp code is below.

    CONTROLLER

    Code:
    /*
     * Created on 16-Dec-2004
     * @author mark
     */
    package salon.web;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.validation.BindException;
    import org.springframework.validation.Errors;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.ModelAndViewDefiningException;
    import org.springframework.web.servlet.mvc.AbstractWizardFormController;
    
    import salon.domain.Reservation;
    import salon.domain.SalonFacadeIF;
    import salon.domain.Stylist;
    import salon.domain.UserAccount;
    
    /**
     * 
     */
    public class BookingWizardController extends AbstractWizardFormController {
      
      private SalonFacadeIF salonFacade;
      
      public BookingWizardController() {
        super();
        
        setCommandName("reservation");
    
        setPages(new String[] { "stylistAndService", 
                                "dateAndTime", 
                                "confirmBooking" });
      }
      
      protected Object formBackingObject(HttpServletRequest request) 
                                         throws Exception {
        System.out.println("formBackingObject method");
        UserSession userSession = 
          (UserSession)request.getSession().getAttribute("userSession");
    
        // Get the user account from the users session.
        UserAccount userAccount = userSession.getUserAccount();
        if(userAccount != null) {
          Reservation reservation = new Reservation();
          
          return reservation;
        }
        
        ModelAndView modelAndView = new ModelAndView("Error", 
                                                     "message", 
                                                     "Some Error");
        throw new ModelAndViewDefiningException(modelAndView);
      }
      
      protected void onBindAndValidate(HttpServletRequest request, 
                                       Object command, 
                                       BindException ex,
                                       int page) throws Exception {
        System.out.println("onBindAndValidate method");
        Reservation reservation = (Reservation)command;
        
        if(page == 0) {
          System.out.println("=============================");
          System.out.println("onBindAndValidate first page.");
    
          String[] names = request.getParameter("stylist.fullName").split(" ");
          reservation.setStylist(salonFacade.getStylistByName(names[0], names[1]));
          System.out.println("first name = " + names[0]);
          System.out.println("last name  = " + names[1]);
    
          //reservation.setStylist(request.getParameter("stylist"));
          // Get service's name from request.
          String[] service = request.getParameter("service.name").split(" - ");
          reservation.setService(salonFacade.getServiceByName(service[0]));
          System.out.println("service name = " + service[0]);
          System.out.println("service cost = " + service[1]);
          System.out.println("=============================");
          
          
          
          //reservation.setCost(service[1].substring(1));
          //reservation.setDate(parseDate(request.getParameter("date")));
          /*
          System.out.println("reservation stylist = " + reservation.getStylist());      
          System.out.println("reservation service = " + reservation.getService());      
          System.out.println("reservation date    = " + reservation.getDate());
          */
        } else if(page == 1) {
          System.out.println("onBindAndValidate second page.");
        }
      }
      
      protected Map referenceData(HttpServletRequest request, 
                                  Object command, 
                                  Errors errors, 
                                  int page) 
                                  throws Exception {
        System.out.println("referenceData method");
        System.out.println("------------------- " + page + " -------------------");
        Map model = new HashMap();
        //Reservation reservation = (Reservation)command;
        
        if(page == 0) {
          System.out.println("Getting model for first page.");
          return getStylistAndServiceModel();
        } else if(page == 1) {
          System.out.println("Getting model for second page.");
    //      return getDateAndTimeModel(reservation);
          return null;
        }
        
        return model;
      }
      
      private Map getStylistAndServiceModel() {
        Map model = new HashMap();
        
        // Get and insert a List of stylists into the view's model.
        model.put("stylists", salonFacade.getStylistList());    
        // Get and insert a List of services into the view's model.
        model.put("services", salonFacade.getServiceList());
        
        // Get a List of dates for the next 60 days.
        // Get a calendar object initialized to today.
        Calendar cal = Calendar.getInstance();
        List dates = new ArrayList();
        for&#40;int i = 0; i < 60; i++&#41; &#123;
          // Get individual date, incremented by one day.
          cal.add&#40;Calendar.DATE, 1&#41;;
          // Add date to the dates list.
          dates.add&#40;cal.getTime&#40;&#41;&#41;;
        &#125;
    
        // Insert the List of dates into the view's model.
        model.put&#40;"dates", dates&#41;;   
        
        
        return model;
      &#125;
      
      private Map getDateAndTimeModel&#40;Reservation reservation&#41; &#123;
        Map model = new HashMap&#40;&#41;;
        
        // Get a List of appontment times.
        //List apps = salonFacade.getAppointmentList&#40;&#41;;
        
        //Stylist stylist = reservation.getStylist&#40;&#41;;
        // Get the days bookings for stylist.
        //List dailyBookings = 
        //  salonFacade.getDailyBookingsForStylist&#40;formatDate&#40;reservation.getDate&#40;&#41;&#41;, 
        //                                         stylist.getId&#40;&#41;&#41;;
        
        
        /*
        // Split stylist's name string.
        String&#91;&#93; names = reservation.getStylist&#40;&#41;.split&#40;"\\s"&#41;;
        Stylist stylist = salonFacade.getStylistByName&#40;names&#91;0&#93;, names&#91;1&#93;&#41;;
        // Get the days bookings for stylist.
        List dailyBookings = 
          salonFacade.getDailyBookingsForStylist&#40;formatDate&#40;reservation.getDate&#40;&#41;&#41;, 
                                                 stylist.getId&#40;&#41;&#41;;
        
        // Iterate through the apps list and compare appointment id with 
        // appointment id from each booking in the dailyBookings list.
        for&#40;Iterator iter = apps.iterator&#40;&#41;; iter.hasNext&#40;&#41;;&#41; &#123;
          Appointment appointment = &#40;Appointment&#41;iter.next&#40;&#41;;
          for&#40;Iterator iterator = dailyBookings.iterator&#40;&#41;; iterator.hasNext&#40;&#41;;&#41; &#123;
            Booking booking = &#40;Booking&#41;iterator.next&#40;&#41;;
              if&#40;booking.getAppointmentId&#40;&#41;.equals&#40;appointment.getId&#40;&#41;&#41;&#41; &#123;
              // Appointment already booked.
              appointment.setBooked&#40;true&#41;;
              break;
            &#125;
             
            // Appointment not booked.
            appointment.setBooked&#40;false&#41;;
          &#125;  
        &#125;
        
        // Insert the list of appointments into the view's model.
        model.put&#40;"appointments", apps&#41;;
        */    
        return model;
      &#125;
    
      protected void validatePage&#40;Object command, Errors errors, int page&#41; &#123;
        System.out.println&#40;"validatePage method"&#41;;
        if&#40;page == 0&#41; &#123;
          System.out.println&#40;"Validating first page."&#41;;
        &#125; else if&#40;page == 1&#41; &#123;
          System.out.println&#40;"Validating for second page."&#41;;
        &#125;
      &#125;
    
      public void setSalonFacade&#40;SalonFacadeIF facade&#41; &#123;
        salonFacade = facade;
      &#125;
    
      private Date parseDate&#40;String stringDate&#41; &#123;
        SimpleDateFormat format = new SimpleDateFormat&#40;"dd/MM/yyyy"&#41;;
        Date date = null;
    
        try &#123;
          // Parse string date to a java date object.
          date = format.parse&#40;stringDate&#41;;
        &#125; catch&#40;ParseException ex&#41; &#123;
          ex.printStackTrace&#40;&#41;;
        &#125;
        
        return date;
      &#125;
      /*
      private String formatDate&#40;Date date&#41; &#123;
        DateFormat format = new SimpleDateFormat&#40;"dd/MM/yyyy"&#41;;
        
        return format.format&#40;date&#41;;
      &#125;
      */
      protected boolean isFormSubmission&#40;HttpServletRequest request&#41; &#123;
        return super.isFormSubmission&#40;request&#41; || 
               request.getParameter&#40;PARAM_FINISH&#41; != null;
      &#125;
      
      protected ModelAndView processFinish&#40;HttpServletRequest request,
                                           HttpServletResponse response, 
                                           Object obj, 
                                           BindException ex&#41;
                                           throws Exception &#123;
        System.out.println&#40;"processFinish method"&#41;;
        Map model = new HashMap&#40;&#41;;
        
        return new ModelAndView&#40;"acknowledgeBooking", model&#41;;
      &#125;
    
      protected ModelAndView processCancel&#40;HttpServletRequest request,
                                           HttpServletResponse response, 
                                           Object obj, 
                                           BindException ex&#41;
                                           throws Exception &#123;
        System.out.println&#40;"processCancel method"&#41;;
    
        throw new UnsupportedOperationException&#40;&#41;;
      &#125;
    
    &#125; // End of class BookingWizardController.java
    These pages are part of a template therefore the head tags etc are missing.
    FIRST JSP PAGE

    Code:
    <%@ page session="false" %>
    
    <%@ taglib prefix="c" uri="http&#58;//java.sun.com/jstl/core" %>
    <%@ taglib prefix="fmt" uri="http&#58;//java.sun.com/jstl/fmt" %>
    
    <%@ taglib prefix="spring" uri="/spring" %>
    
    <fmt&#58;setLocale value="en-UK" />
    
    <form name="stylistAndService" 
          id="stylistAndService" 
          action="dateAndTime.html" 
          method="post">
              
      <table border="1" align="center" width="65%">
        <tr>
          <td colspan="3" align="left">
            <img src="./images/ban_booking.jpg" border="0"/>
          </td>
        </tr>
            
        <tr>
          <td colspan="2" align="center">
            <b>Stylists&#58;</b> 
            <spring&#58;bind path="reservation.stylist.fullName">
              <select name="<c&#58;out value='$&#123;status.expression&#125;'/>">
                <c&#58;forEach var="stylist" items="$&#123;stylists&#125;">
                  <option><c&#58;out value="$&#123;stylist.fullName&#125;"/></option>
                </c&#58;forEach>
              </select>
            </spring&#58;bind>
          </td>
            
          <td colspan="2" align="center"> 
            <b>Services&#58;</b> 
            <spring&#58;bind path="reservation.service.name">
              <select name="<c&#58;out value='$&#123;status.expression&#125;'/>">
                <c&#58;forEach var="service" items="$&#123;services&#125;">
                  <option>
                    <c&#58;out value="$&#123;service.name&#125;"/> - 
                    <fmt&#58;formatNumber value="$&#123;service.cost&#125;" type="currency"/>
                  </option>
                </c&#58;forEach>
              </select>
            </spring&#58;bind>
          </td>
        </tr>
    
        <tr>
          <td colspan="3" align="center">
            <input type="submit" name="_target1" value="search">
          </td>
        </tr>
            
        <tr>
          <td colspan="2" align="right">
            <img src="./images/ban_bottom.jpg" border="0"/>
          </td>
        </tr>
      </table>
    </form>

    SECOND JSP PAGE

    Code:
    <%@ page session="false" %>
    
    <%@ taglib prefix="c" uri="http&#58;//java.sun.com/jstl/core" %>
    <%@ taglib prefix="fmt" uri="http&#58;//java.sun.com/jstl/fmt" %>
    
    <table align="center" border="1" width="65%">
      <tr>
        <td>
          Appointment Timetable Results For Stylist&#58; 
          <b><c&#58;out value="$&#123;reservation.stylist.fullName&#125;"/></b>
          Service&#58; <b><c&#58;out value="$&#123;reservation.service.name&#125;"/></b>
          Service Cost&#58; <b><c&#58;out value="$&#123;reservation.service.cost&#125;"/></b>
        </td>
      </tr>
    	
      <tr>
        <td align="left">
          Below is a list of the appointment times which match your request.
          You can use the Find earlier appointments and Find later 
          appointments buttons to click through the rest of the timetable.
          <br/><br/>
        </td>
      </tr>
    	
      <tr>
        <td align="left">
          Once you have found the times you want, you can book them online.
          Please click on the Check Availability and Pricing button below. 
          Or else provide us with more details of your journey by first 
          filling in the form below.
        </td>
      </tr>
    </table>

  • #2
    Try explicitly...

    Define the getTargetPage(), cast the command to the form backing object and put it in the request.

    This might work.

    I'll keep watching this thread.

    Comment

    Working...
    X