Announcement Announcement Module
Collapse
No announcement yet.
Url navigation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Url navigation

    Hi,

    I would like to know if there is a better way to to redirects in my Spring app.

    At the moment, for example, my change password page could lead to either the error page or the main app page. Returning a new ModelAndView object of the relevant view works but does not update the browser url.

    To get round this I use response.sendredirect():

    Code:
    ...
           response.sendRedirect(request.getContextPath()+"/secure/main.htm");
            return null;
    However, I now have to keep the url in the controller upto date with the mappings in the servlet xml file:

    Code:
    	<bean name="/secure/main.htm" class="com.ifst.web.controllers.MainPageController"/>
    Thanks

    Rakesh

  • #2
    Have you tried this?
    Code:
    return new RedirectView&#40;....

    Comment


    • #3
      Hi,

      thanks for the tip. I got it working using this:

      Code:
              RedirectView rv = new RedirectView&#40;"/secure/main.htm",true&#41;;
              return new ModelAndView&#40;rv&#41;;
      Its a bit cleaner but still has the url hard coded but i guess there's not much that can be done about that.

      Thanks for your time

      Rakesh

      Comment


      • #4
        Hi,

        as a follow on to my original question, how do i accomplish the same thing when there is also some data i wish to pass to the next view?

        This is how it currently works, an error condition in the controller looks like this:

        Code:
                &#125; catch &#40;InvalidLogonException e&#41; &#123;
                    return errorPageRedirect&#40;e&#41;;
        .
        .
        .
            private ModelAndView errorPageRedirect&#40;Exception e&#41; &#123;
                logger.debug&#40;e.getMessage&#40;&#41;&#41;;
        
                ModelAndView mav = new ModelAndView&#40;"login_failure"&#41;;
                mav.addObject&#40;"reason", e.getMessage&#40;&#41;&#41;;
        
                return mav;
            &#125;
        so, like before, the browser url does not update. Can i get round this??

        Thanks

        Rakesh

        Comment


        • #5
          I think you could make this way:
          Code:
                  RedirectView rv = new RedirectView&#40;"/secure/main.htm",true&#41;;
          
                  Map myModel = new HashMap&#40;&#41;;
                  myModel.put&#40;"reason", e.getMessage&#40;&#41;&#41;;
          
                  return new ModelAndView&#40;rv, "model", myModel&#41;;

          Comment


          • #6
            From an "point on concerns" point of view, I think it is "leakage" to have your controller determine whether to forward or redirect. It is much better (in my very humble, but overly opinionated opinion) for your controller to just return a model and view based on a view name, and then the definition of that view is either redirect or forward etc.

            Comment

            Working...
            X