Announcement Announcement Module
Collapse
No announcement yet.
Help Displaying Error Messages in JSP Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help Displaying Error Messages in JSP

    Hi All,

    We have just started using the Spring framework where I work, and I have very little experience with it so far. I just need a little help with a page I'm creating, everything functions ok except for the outputting of error messages.

    I'm using a multiactioncontroller, with 3 actions. One of the actions is a "submitForm" button, and I want to validate my form when this action is called.

    I've tried wiring in a Validator through the xml config file but this was not working, so I'm approaching it by trying to invoke the validator in my controller. Here's the code:

    This is my submitForm action in the Controller
    Code:
      public ModelAndView submitForm(HttpServletRequest request, HttpServletResponse response) throws Exception { 
        // Create a command Object to bind the submitted parameters to
        DropOffLocatorCommand cmd = (DropOffLocatorCommand)newCommandObject(m_commandClass);
        String commandName = getCommandName(cmd);
        request.setAttribute(commandName, cmd);
        
        // set action source
        cmd.setActionSource("submitForm");
      
        // validate data
        DropOffValidator validator = new DropOffValidator();     
        BindException errors = generateErrors(request, cmd, validator);
        
        // bind request
        bind(request, cmd);
         
        // prepare the data for the next view
        Map model = referenceData(request, cmd, true, false, errors);
        
        request.setAttribute(getCommandName(cmd), cmd);
        request.setAttribute("model", model);
    
        return new ModelAndView("dropOffLocator", createBinder(request, cmd).getErrors().getModel());
      }
    This is the generateErrors method called above, to create an Errors object. Im not sure about some of the code in here, I saw this on another website. It does return an object with the error in it however.

    Code:
      protected BindException generateErrors(HttpServletRequest request, Object command, Validator validator) throws Exception {    
        ServletRequestDataBinder binder = createBinder(request, command);    
        binder.bind(request);    
        BindException errors = new BindException(command, getCommandName(command));    
        if (validator.supports(command.getClass())) {       
          ValidationUtils.invokeValidator(validator, command, errors);    
        }    
        return errors;
      }
    Here is the referenceData method which populates the model

    Code:
      protected Map referenceData(HttpServletRequest request, DropOffLocatorCommand command, boolean setUrl, boolean getDetails, BindException errors) throws Exception{
        Map model = new HashMap();
        DropOffLocatorCommand cmd = (DropOffLocatorCommand) command;
        
        // this outputs my error so I know for sure that the object is not null
        if(errors != null) {
          logger.debug("errors != null");
          logger.debug("error num: " + errors.getErrorCount());
          logger.debug("message: " + errors.getMessage());
        }
        
        model.put("address", cmd.getAddress());
        model.put("city", cmd.getCity());
        model.put("state", cmd.getState());
        model.put("postalcode", cmd.getPostalCode());
        model.put("country", cmd.getCountry());
        model.put("carrier", cmd.getCarrier());
        model.put("url", constructUrl(request, cmd, setUrl));
        model.put("carriers", getDropOffCarriers(request));
        
        if(getDetails) {
          SessionInfo sessionInfo = (SessionInfo) request.getAttribute(SessionInfo.SESSION_INFO_REQUEST_KEY);
          PartnerService partnerService = new PartnerService(sessionInfo);
          Partner partner = new Partner();
          partner.setPartner(cmd.getDeliverFrom());
          partner = (Partner)partnerService.get(partner);
          
          if(partner != null) {
            
            model.put("address", partner.getAddressLine1());
            model.put("city", partner.getAddressLine2());
            model.put("state", partner.getStateCode());
            model.put("postalcode", partner.getZipCodePostalCode());
            model.put("country", partner.getCountry());
          }  
        }
    
        
        // Check the errors
        if (errors != null && errors.getModel() != null && errors.getErrorCount() > 0) {
          logger.info("Adding " + errors.getErrorCount() + " errors to the model " + errors.toString());
          // Here's where I add the errors object to the mode
          model.putAll(errors.getModel());      
          model.put("url", null);
        }
        
        request.setAttribute("model", model);
        
        return model;
      }
    Thanks for bearing with me if you have made it this far
    So thats the code from my controller, here's the field in my JSP in which the error message should appear.

    Code:
    <spring:bind path="command.postalCode">
      <tr>
        <td nowrap>
          <input type="text" name="<c:out value="${status.expression}"/>" class="TEXT-PROMPT-REQ" size="13" maxlength="10" onselect="setMandatory()" onblur="setMandatory()" value="<c:out value="${model.postalcode}"/>" onkeypress="return entsub(window.event, this.form, 'SHIPFROM_POSTAL_CODE_PROMPT', -1)">&nbsp;<a href="javascript:onclick=getList('Postalcode','droplocationform','<c:out value="${status.expression}"/>','POSTAL_CODE','',-1,-1)"><img class="PROMPT" src="<%=contextPath%>/images/prompt.gif" title="Lookup Code" border="0" name="SHIPFROM_POSTAL_CODE_PROMPT"></a>
        </td>
        <td class="TDLABEL">
          <c:if test="${status.error}"><font color="red"><b><c:out value="${status.errorMessage}" /></b></font>
          </c:if>
        </td>
      </tr>
    </spring:bind>
    So that it guys, the problem is, the errors object contains the error but its not outputting it to the JSP page?

    I would really appreciate any comment or guidance on this, as it has been wrecking my head for the last week and I'm not sure what to do next! There's not much documentation or examples out there for validation with the multiactioncontroller.

    Thanks,

    Conor.

  • #2
    anybody?? please!!

    Comment


    • #3
      Hi jebuz,

      Originally posted by jebuz
      anybody?? please!!
      I'd recommed you post your question on the Web forum where you're likely to get better support and/or assistance:

      http://forum.springframework.org/forumdisplay.php?f=25

      This is actually the Spring Web Flow forum, which does not deal directly with Spring MVC.

      regards,

      - Sam

      Comment

      Working...
      X