Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC redirect after form submit Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC redirect after form submit

    Hello

    The thing im aiming to achieve is to redirect the user after a form submission and then show either success or fail messages on that redirected page.

    The idea would look somewhat like this....

    PHP Code:
    public ModelAndView processForm(HttpServlet requestHttpServletResponse response){
        
    //process form data etc
        
    ModelAndView modelAndView = new ModelAndView("redirect:someurl");                
        
    Map<ObjectObjectmodel modelAndView.getModel();

        
    model.put("error""this.is.my.error.code");
        return new 
    ModelAndView("redirect:someurl");

    Meaning i could populate the model of a redirect view. Currently according to my knowledge, this model is just dumped away or emptied and a new, empty model is made with a redirect.
    I would like to get my defined and populated model to be "carried" through to the redirected view basically.

    I cant find anything really useful on Google neither so perhaps someone here has a cure for this problem.

  • #2
    Currently according to my knowledge, this model is just dumped away or emptied and a new, empty model is made with a redirect.
    No, this is not true. The model parameters are generally carried along with a redirect. Simply make sure your view is a "redirect:..." type, and make sure you're passing the Map for the Model along (you aren't doing this in your sample code where you're building the ModelAndView), and it'll work fine. Note that obviously complex objects bound in the model won't survive the redirect intact -- just primitives.

    Comment


    • #3
      hmm so when i pass the model like this:

      PHP Code:
      public ModelAndView processForm(HttpServlet requestHttpServletResponse response){
          
      //process form data etc
          
      ModelAndView modelAndView = new ModelAndView("redirect:someurl");                
          
      Map<ObjectObjectmodel modelAndView.getModel();

          
      model.put("error""this.is.my.error.code");
          return new 
      ModelAndView("redirect:someurl"model);

      i get all the model properties as url parameters, which is not what im aiming for. Judging by other posts relating to this problem, it seems to me that there is no easy solution for this, at least none that i can find.

      Comment


      • #4
        What is it you are aiming for? If you don't want the properties as URL parameters yuo can also put them on the session instead.

        Also I would rewrite your code to something like this, it saves yuo from instantiating a new ModelAndView again.

        Code:
        public ModelAndView processForm(HttpServlet request, HttpServletResponse response){
            //process form data etc
            ModelAndView modelAndView = new ModelAndView("redirect:someurl");                
            Map<Object, Object> model = modelAndView.getModel();
            modelAndView.addObject("error", "this.is.my.error.code");
            return modelAndView;
        }

        Comment


        • #5
          I'd go for the code Martin suggested.

          The problem you're facing is that the ModelAndView you're filling isn't used.
          You instantiate a new ModelAndView in the return statement.
          Which, ofcourse, does not contain the values of the other MaV object.

          Small typo, always easy to overlook and a bitch to debug

          Comment


          • #6
            but the code Martin has suggested is not working for me.

            hi,

            i am also facing the same problem

            public ModelAndView branchDataEntryThroughTheFlow(HttpServletRequest request,
            HttpServletResponse response) throws Exception {


            ModelAndView mv = new ModelAndView("redirect:../branch_data/branchDataEntry.form");

            mv.addObject("dot", new Date() );
            return mv;
            }

            In the "branchDataEntry.form", l could not access the model "dot".
            is any one know how to pass model while redirecting the form ....
            please refer..

            Comment


            • #7
              RedirectView object has also have the same problem

              i am also tried this way

              public ModelAndView branchDataEntryThroughTheFlow(HttpServletRequest request,
              HttpServletResponse response) throws Exception {

              Map model = new HashMap();
              model.put("dot", new Date());

              return new ModelAndView(new RedirectView("../branch_data/branchDataEntry.form",true), model );
              }


              In the "branchDataEntry.form", l could not access the model "dot".
              i used the bellow code
              Data Of Transaction : ${dot}
              but it does not showing any thing after ":"

              Comment


              • #8
                Use [ code][/code ] tags when posting code.

                There is no (easy) way of keeping the model as the redirect is client side NOT server side. You could store it in the session and check for it in the next controller. or don't do a redirect.

                Comment


                • #9
                  its working through session

                  first of all very very thanks to Marten Deinum helping us, because i was looking alternative option to do this.

                  i used the code like this:

                  public ModelAndView branchDataEntryThroughTheFlow(HttpServletRequest request,
                  HttpServletResponse response) throws Exception {

                  ModelAndView mv = new ModelAndView("redirect:../branch_data/branchDataEntry.form");
                  request.getSession().setAttribute("dot",new Date());
                  //mv.addObject("dot", new Date() );
                  return mv;
                  }

                  Now In the "branchDataEntry.form", l could access the model "dot" like this.
                  Data Of Transaction : ${dot}
                  its working for me........

                  note:- you have to remove the model from session manually, otherwise it will store there till that session is closed

                  Comment


                  • #10
                    I solved the problem adding RedirectAttributes:

                    redirectAttributes.addAttribute("param1", value1);
                    ...
                    return "redirect:/";

                    Comment

                    Working...
                    X