Announcement Announcement Module
Collapse
No announcement yet.
_target1 always returns zero Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • _target1 always returns zero

    Hi there,

    The problem I am having is that my first page in my wizard always returns 0. I have three pages each with submit buttons with names _target1, _target2 and _target3. Everything worked until today when I did some tinkering :oops:

    I use the default getTargetPage method and I can't getpast the first page. No error occurs when I click on the submit button with the name _target1 the page variable in the referenceData method is always zero, therefore the first page is re-displayed.

    I wanted to use the command object to propagate the values through the controller and its various pages instead of continually adding and retrieving the values from the model and request objects. I believed this to be a simple thing to do, and I still believe that, the problem is that I guess I am just as simple as once I tried it and broke the app, I couldn't revert to the working version(my fault for not versioning or backing up). I can't see anything wrong with the code as it stands, I have also checked it against an old version I backed up a couple of weeks ago and there is little difference. The JSP pages submit buttons correctly use _target1, _target2 etc as the name.

    Under what circumstances would the JSP always return a page number of zero? All code is below.

    Code:
    public class BookingWizardController extends AbstractWizardFormController {
      
      private SalonFacadeIF salonFacade;
    
      public BookingWizardController() {
        super();
    
        // The command object will be included in the model under this name.
        setCommandName("reservation");
        
        // Set the wizard pages, i.e. the view names for the pages. 
        // The array index is interpreted as page number.
        setPages(new String[] { "stylistAndService", 
                                "dateAndTime", 
                                "confirmBooking" });
      }
      
      protected Object formBackingObject(HttpServletRequest request) 
                                         throws Exception {
        System.out.println("formBackingObject method");
        // Get the users session.
        UserSession userSession = 
          (UserSession)request.getSession().getAttribute("userSession");
    
        // Get the user account from the users session.
        UserAccount userAccount = userSession.getAccount();
        if(userAccount != null) {
          // If there is a user account, create a reservation object.
          Reservation reservation = new Reservation();
    
          return reservation;
        }
    
        // Create an error message.
        String message = 
         "A booking couldn't be created because a user account could not be found.";
        // Create and initialize a new model and view.
        ModelAndView modelAndView = new ModelAndView("Error", 
                                                     "message", 
                                                     message);
        // Display error view.
        throw new ModelAndViewDefiningException(modelAndView);
      }
    
      protected boolean isFormSubmission(HttpServletRequest request) {
        System.out.println("isFormSubmission method");
        return super.isFormSubmission(request) || 
               request.getParameter(PARAM_FINISH) != null;
      }
      
      protected void onBindAndValidate(HttpServletRequest request, 
                                       Object command, 
                                       BindException ex,
                                       int page) throws Exception {
        System.out.println("onBindAndValidate method");
        Reservation reservation = (Reservation)command;
    
        // If the page is the stylist and services page.
        if(page == 0) {
          System.out.println("page zero");
          
          System.out.println("parameter stylist is " + request.getParameter("stylist"));
          System.out.println("parameter service is " + request.getParameter("service"));
          System.out.println("parameter date is    " + request.getParameter("date"));
        }
      }
      
      protected Map referenceData(HttpServletRequest request,
                                  int page) 
                                  throws Exception {
        System.out.println("referenceData method");
        System.out.println("page = " + page);
        
        // If the page is the stylist and services page.
        if(page == 0) {
          System.out.println("Getting first page model");
          return getFirstPageModel();
        } else if(page == 1) { // Date and time page.
          System.out.println("Getting second page model");
    //    return getSecondPageModel(request, reservation);
          return getSecondPageModel(request);
        } else if(page == 2) { // Confirmation page.
          System.out.println("Getting third page model");
          return getThirdPageModel(request);
        }
        
        return null;
      }
      
    The getFirstPageModel() etc methods are private methods that construct the model Maps.
    
      public void setSalonFacade(SalonFacadeIF facade) {
        System.out.println("setSalonFacade method");
        this.salonFacade = facade;
      }
    
      protected void validatePage(Object command, Errors errors, int page) {
        System.out.println("validatePage method");
        if(page == 0) {
          System.out.println("validating first page.");
        } else if(page == 1) {
          System.out.println("validating second page.");
        }
      }
    
      protected ModelAndView processFinish(HttpServletRequest request,
                                           HttpServletResponse response, 
                                           Object command, 
                                           BindException ex)
                                           throws Exception {
        System.out.println("processFinish method");
        
        Map model = new HashMap();
        
        return new ModelAndView("acknowledgeBooking", model);
      }
      
      protected ModelAndView processCancel(HttpServletRequest request, 
                                           HttpServletResponse response,
                                           Object command, 
                                           BindException errors) 
                                           throws Exception {
        System.out.println("processCancel method");
        throw new UnsupportedOperationException();
      }
    
    } // End of class
    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" %>
    
    <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> 
            <select name="stylist">
              <c&#58;forEach var="stylist" items="$&#123;stylists&#125;">
                <option><c&#58;out value="$&#123;stylist.fullName&#125;"/></option>
              </c&#58;forEach>
            </select>
          </td>
            
          <td colspan="2" align="center"> 
            <b>Services&#58;</b> 
            <select name="service">
              <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>
          </td>
        </tr>
            
        <tr>
          <td align="center"> 
            <b>Date&#58;</b> 
            <select name="date"><!--
              <c&#58;forEach var="date" items="$&#123;dates&#125;">
                <option>-->
                  <!-- i.e. 29/11/2004 --><!--
        	        <fmt&#58;formatDate value="$&#123;date&#125;" 
        	                        type="date" 
        	                        pattern="dd/MM/yyyy"/>
        	      </option>
              </c&#58;forEach>-->
              <!-- FOR TESTING PURPOSES -->
              <option>22/11/2004</option>
              <option>23/11/2004</option>
              <option>24/11/2004</option>
              <option>25/11/2004</option>
              <option>26/11/2004</option>
              <option>27/11/2004</option>
              <option>28/11/2004</option>
              <option>29/11/2004</option>
              <option>30/11/2004</option>
              <option>31/11/2004</option>
              <option>01/12/2004</option>
              <option>02/12/2004</option>
              <option>03/12/2004</option>
              <option>04/12/2004</option>
              <option>05/12/2004</option>
              <option>06/12/2004</option>
              <option>07/12/2004</option>
              <option>08/12/2004</option>
              <option>09/12/2004</option>
              <option>10/12/2004</option>
              <option>11/12/2004</option>
              <option>12/12/2004</option>
              <option>13/12/2004</option>
              <option>14/12/2004</option>
              <option>15/12/2004</option>
              <option>16/12/2004</option>
              <option>17/12/2004</option>
              <option>18/12/2004</option>
              <option>19/12/2004</option>
              <option>20/12/2004</option>
              <option>21/12/2004</option>
              <option>22/12/2004</option>
              <option>23/12/2004</option>
              <option>24/12/2004</option>
              <option>25/12/2004</option>
              <option>26/12/2004</option>
              <option>27/12/2004</option>
              <option>28/12/2004</option>
              <option>29/12/2004</option>
              <option>30/12/2004</option>
              <option>31/12/2004</option>
            </select>
          </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>
Working...
X