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

  • ModelAndView

    Normally I'll use ModelAndView("/pages/test") to return a test.jsp. Now for some reason, I'll need return ModelAndView("test.htm?cmd=testFunc&id=12345");

    This will forward to the controller (referred by test.htm), then execute the testFunc method and in this method, request.getParameter("id")...


    My question: internally in Spring framework, how does the return ModelAndView("test.htm?cmd=testFunc&id=12345"); get processed?


    Thanks


    Scott

  • #2
    Hi Scott,

    In the case when you are wanting to redirect the user to another page which will process the request (eg. display a page or do some processing) you will need to either append "redirect:" to the start of the view information. eg.

    Code:
    return new ModelAndView("redirect:test.htm?cmd=testFunc&id=12345");
    or use RedirectView(), which I am not a fan of.

    I recommend that you dependency inject the "redirect:test.htm" into the successView property as this allows you to change the above code to :

    Code:
    Map modelMap = new HashMap();
    
    modelMap.put("cmd", "testFunc");
    modelMap.put("id", "12345");
    		
    return new ModelAndView(getSuccessView(),modelMap);
    Now your code does not know if its being redirect or displayed, but either way the model variables will be handled correctly and put into the url automatically if it is a redirect.

    Hope this helps

    Josh

    Comment


    • #3
      Thanks for the response.

      If I understand it correctly,

      1) When redirect to a different JSP page, use redirect:

      return new ModelAndView("redirect:test.htm?cmd=testFunc&id=12345");

      2) While just reload the same JSP page, NO redirect is used:

      return new ModelAndView("test.htm?cmd=testFunc&id=12345");

      --Correct?


      Thanks

      Scott

      Comment


      • #4
        Hi Scott,

        You kinda have it, but not quite.

        Your first point is right, the "redirect:" prefix will send a 'page moved' command to the web browser which will then request the url sent to it. Remember, this has to be a url which exists within your mappings.

        You second point is a bit wrong. If you return :
        Code:
        return new ModelAndView("test.htm?cmd=testFunc&id=12345");
        Spring will try and find a page named "test.htm?cmd=testFunc&id=12345.jsp" or whatever your view resolver resolves to. As I said it my second example, dependency inject the success view, add the model information to a map and then add it to the ModelAndView, otherwise you are going to have to create the url string which is a bit messy.

        If you need further help with this please post your mapping xml code and controller code.

        Hope this helps

        Josh

        Comment


        • #5
          Thanks for the response.

          What happened in my app is the Spring action Class which does the "business process" need an updated value from the "id". Unfortunately, the server side can not send the value for "id" once it is updated. So, after the value for the "id" is changed, it will then be sent by:

          return new ModelAndView("test.htm?cmd=testFunc&id=12345");

          where, test.htm --> controller Class --> loads testFunc method --> return a JSP page named "test.jsp" {return new ModelAndView("/pages/test")}. Once the submit button pressed on the test.jsp again, --> controller class --> method --> do process --> get updated value for the "id"....

          For the above case, do I still need the suggested:

          Map modelMap = new HashMap();

          modelMap.put("cmd", "testFunc");
          modelMap.put("id", "12345");

          return new ModelAndView(getSuccessView(),modelMap);




          Thanks


          Scott

          P.S. All the naming convention used in my post is just for question purpose only, they are not the ones used in the app. This is just for simplicity.

          Comment


          • #6
            Hi Scott,

            I guess what I was trying to suggest was a form of abstraction of the success view from the hard coding of your controller.

            Both of our solutions are equal in the result, they are just different ways of doing it. I was suggesting something like this in your bean definition :
            Code:
            <bean id="someController" class="com.web.controller.SomeSimpleFormController">
                 <property name="fomView" value="simpleForm"/>
                 <property name="successView" value="redirect:other-page.htm"/>
            </bean>
            Then, when you place model information in your ModelAndView object it will be automatically added to the url (eg. "?cmd=func&id=123").

            Its just a different way to achieve the same result. If you don't quite understand what I am suggesting, just do what you are comfortable with.

            Hope this helps

            Good luck

            Josh

            Comment


            • #7
              Extracting model in the RedirectView

              I am having trouble getting the model in referenceData() of the new controller.

              Here is what I am doing..


              Code:
              Map model;
              
              ModelAndView modelAndView = new ModelAndView(new RedirectView("showMyNewTarget.do"));
              modelAndView.addObject("targetModel", model);
              return modelAndView;
              In the URL I do see targetModel with the correct data. How do I get it off the URL in my referenceData() of the controller? Please help.

              I have tried both of the following but nothing seems to work.


              Code:
               Map empModel = (Map) request.getAttribute("targetModel");
              Code:
               Map empModel = (Map) request.getParameterMap();
              Can some one please tell me what I am missing here.

              Comment


              • #8
                You can't bind a complex object (in your case, Map) to a model value for a redirect. Model values used during redirect have to be primitives only. This is because the model key/value pairs are serialized to URL parameters.

                On the receiving end, you should use request.getParameter() to pull URL parameters from the request. getAttribute() returns only request attributes (have a look at the Servlet API spec to read about the differences if you're confused).

                Comment


                • #9
                  Thanks,

                  I go that figuredout.

                  Anu

                  Comment


                  • #10
                    Is there any other way to goto another controller from one controller without using RedirectView()?

                    Comment

                    Working...
                    X