Results 1 to 6 of 6

Thread: Get the view where current request comes?

  1. #1
    Join Date
    Oct 2005
    Posts
    19

    Default 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. #2
    Join Date
    Oct 2005
    Posts
    19

    Default

    Quote 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

  3. #3
    Join Date
    Jul 2005
    Posts
    246

    Default

    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

  4. #4
    Join Date
    Oct 2005
    Posts
    19

    Default

    Quote 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?

  5. #5
    Join Date
    Jul 2005
    Posts
    246

    Default

    Quote 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

  6. #6
    Join Date
    Oct 2005
    Posts
    19

    Default

    Quote 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •