Announcement Announcement Module
Collapse
No announcement yet.
Get the view where current request comes? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Get the view where current request comes?

    Hi, I am facing a problem about how to get the view where current request comes?

    I use one controller with a hashmap command class for all pages, so I cannot give a specific formView for the controller. However, when I use showForm method of SimpleFormController to show some validation errors, the system cannot find the view.

    Can I get the view where the current request comes so that I simply use such view to show errors?

    Thanks in advance!

    cheers,

    Honghai

  • #2
    Originally posted by honghai
    Hi, I am facing a problem about how to get the view where current request comes?

    I use one controller with a hashmap command class for all pages, so I cannot give a specific formView for the controller. However, when I use showForm method of SimpleFormController to show some validation errors, the system cannot find the view.

    Can I get the view where the current request comes so that I simply use such view to show errors?

    Thanks in advance!

    cheers,

    Honghai

    Maybe I didn't explain it clearly. Actually the problem is how to show errors on the original view without setting the specific view for the controller. I have just one controller for serveral views.

    Thanks

    Comment


    • #3
      I'm doing this to show errors from a view controller:-

      Code:
          protected abstract ModelAndView process(HttpServletRequest request, HttpServletResponse response, 
              Errors errors) throws Exception;
      
          public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception
          {
              BindException errors = new BindException("*", "*");
              ModelAndView modelAndView = process(request, response, errors);
              
              if (errors.hasErrors())
              {
                  modelAndView.addAllObjects(errors.getModel());
              }
              
              return (modelAndView);
          }
      These methods are part of an AbstractViewController and provides errors that are bound to "*". The process method is what all sub-classes use for their logic and it now has an Errors object with which to add errors.

      These errors can be displayed with the following:-

      Code:
      <html:hasErrors command="*">
          <div align="center">
              <div class="errorbox">
                  <img src='<html:rewrite href="image/reddot.gif"/>' width='16' height='16'>
                  <c:forEach var="error" items="${errors}">
                      <c:out value="${error}"/><br>
                  </c:forEach>
              </div>
          </div>
      </html:hasErrors>
      The code for my custom tag can be found in this thread (you can't use the <spring:hasBindErrors> tag for the same reasons as the original poster there):-

      http://forum.springframework.org/showthread.php?t=18978

      Bob

      Comment


      • #4
        Originally posted by Cowboy Bob
        I'm doing this to show errors from a view controller:-
        ....
        Bob
        Hi Bob, thanks a lot for your help. I am trying your solution. Just one question, do you show errors on the original view from which the request comes or on a new view specified in the formView field?

        Comment


        • #5
          Originally posted by honghai
          Hi Bob, thanks a lot for your help. I am trying your solution. Just one question, do you show errors on the original view from which the request comes or on a new view specified in the formView field?
          It doesn't matter which (though you can't do a redirect), since the errors are stored in the request object. So if the target JSP contains the "hasErrors" tag then the error will be displayed.

          Bob

          Comment


          • #6
            Originally posted by Cowboy Bob
            It doesn't matter which (though you can't do a redirect), since the errors are stored in the request object. So if the target JSP contains the "hasErrors" tag then the error will be displayed.

            Bob
            Thanks, Bob. Actually the original problem came out because I didn't understand SpringMVC very well. Now I figure out such problem.

            BTW, I guess you can use
            Code:
                  <spring:hasBindErrors name="command">
                    <c:forEach var="error" items="${errors.allErrors}">
                       <li>
                          	<spring:message code="${error.code}" text="${error.defaultMessage}"/>
                       </li>
                    </c:forEach>
                 </spring:hasBindErrors>
            to show global errors.

            Comment

            Working...
            X