Announcement Announcement Module
Collapse
No announcement yet.
Handling Controller Exceptions in a JSP Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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

    Comment


    • #3
      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.

      Comment


      • #4
        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;

        Comment


        • #5
          Handling controller errors in a JSP

          Thanks a lot, this helped

          Comment

          Working...
          X