Results 1 to 5 of 5

Thread: Handling Controller Exceptions in a JSP

  1. #1
    Join Date
    Jun 2005
    Posts
    4

    Default Handling Controller Exceptions in a JSP

    What is the best way to send information about an exception back to a JSP from a SimpleFormController.

    For instance, I have the following code in the onSubmit(..,..) of my SimpleFormController :

    public ModelAndView onSubmit(Object command, BindException arg1){
    String resultView = "success";
    try {
    doSomething();
    } catch (AppSecurityException e) {
    arg1.reject("message.key.from.properties");
    resultView = "failure";
    }
    ModelAndView mav = new ModelAndView(resultView);
    return mav;
    }

    I did try adding the errors into a list and sending it as a model object but I don't see it as the best approach.

    Thanks in advance for the help .

  2. #2
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    Why not use a separate page. For example:
    Code:
    	<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    		<property name="exceptionMappings">
    			<props>
    				<prop key="org.springframework.dao.DataAccessException">dataAccessFailure</prop>
    				<prop key="orgmycompany.AppSecurityException">securityAccessFailure</prop>
    		</property>
    	</bean>
    Code:
    securityAccessFailure.class=org.springframework.web.servlet.view.JstlView
    securityAccessFailure.url=/WEB-INF/jsp/securityAccessFailure.jsp

  3. #3
    Join Date
    Jun 2005
    Posts
    4

    Default Handling Controller Exceptions in a JSP

    For instance, if the user had entered an invalid password on a login screen. he/she should be taken back to the same screen with the appropriate error message instead of to a different screen.

  4. #4
    Join Date
    Jun 2005
    Posts
    1

    Default

    I was just having a similiar issue and this is the onSubmit I made up for the logon page

    Code:
        public ModelAndView onSubmit&#40;HttpServletRequest request, HttpServletResponse response, Object command, BindException errors&#41; throws Exception &#123;
            Logon logon = &#40;Logon&#41;command;
            // Check password
            User user = getArchive&#40;&#41;.loadUser&#40;logon.getHandle&#40;&#41;&#41;;
            if &#40;user == null&#41;
                errors.rejectValue&#40;"handle", "logon.error.bad.handle"&#41;;
            else if &#40;logon.getPassword&#40;&#41;.compareTo&#40;user.getPassword&#40;&#41;&#41; != 0&#41;
                errors.rejectValue&#40;"password", "logon.error.bad.password"&#41;;
            if &#40;errors.getErrorCount&#40;&#41; != 0&#41; &#123;
                return showForm&#40;request, response, errors&#41;;
            &#125;
            // Create login Cookie
            javax.servlet.http.Cookie cookie = new javax.servlet.http.Cookie&#40;"handleMilage", user.getId&#40;&#41;.toString&#40;&#41; &#41;;
            cookie.setMaxAge&#40;60*60*24*365&#41;;// sets age to one year
            response.addCookie&#40;cookie&#41;;//        String handle = web.util.Cookies.getCookieValue&#40;request.getCookies&#40;&#41;, "handleMilage"&#41;;
            //
            return new ModelAndView&#40;getSuccessView&#40;&#41;, "userId", user.getId&#40;&#41;&#41;;
        &#125;

  5. #5
    Join Date
    Jun 2005
    Posts
    4

    Default Handling controller errors in a JSP

    Thanks a lot, this helped

Similar Threads

  1. Replies: 0
    Last Post: Sep 29th, 2005, 12:39 AM
  2. Replies: 1
    Last Post: Sep 20th, 2005, 09:14 PM
  3. Replies: 2
    Last Post: Mar 29th, 2005, 07:45 AM
  4. Replies: 2
    Last Post: Dec 20th, 2004, 04:35 PM
  5. Replies: 1
    Last Post: Oct 15th, 2004, 06:24 AM

Posting Permissions

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