Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8273275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:25:04+00:00 2026-06-08T07:25:04+00:00

At the moment I am trying to implement the Post/Redirect/Get pattern with Spring MVC

  • 0

At the moment I am trying to implement the Post/Redirect/Get pattern with Spring MVC 3.1. What is the correct way to preserve and recover the model data + validation errors? I know that I can preserve the model and BindingResult with the RedirectAttributes in my POST method. But what is the correct way of recovering them in the GET method from the flash scope?

I have done the following to POST:

    @RequestMapping(value = "/user/create", method = RequestMethod.POST)
public String doCreate(@ModelAttribute("user") @Valid User user, BindingResult result, RedirectAttributes rA){

    if(result.hasErrors()){
        rA.addFlashAttribute("result", result);
        rA.addFlashAttribute("user", user);

        return "redirect:/user";
    }

    return "redirect:/user/success";
}

And the following to GET the user creation form:

    @RequestMapping(value = "/user", method = RequestMethod.GET)
public ModelAndView showUserForm(@ModelAttribute("user") User user, ModelAndView model){

    model.addObject("user", user);
    model.setViewName("userForm");

    return model;
}

This allows me to preserve the given user data in the case of an error. But what is the correct way of recovering the errors?(BindingResult) I’d like to show them in the form with the spring form tags:

<form:errors path="*" />

In addition it would be interesting how to access the flash scope from the get method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:25:06+00:00Added an answer on June 8, 2026 at 7:25 am
    public class BindingHandlerInterceptor extends HandlerInterceptorAdapter {
    
        public static final String BINDING_RESULT_FLUSH_ATTRIBUTE_KEY = BindingHandlerInterceptor.class.getName() + ".flashBindingResult";
    
        private static final String METHOD_GET = "GET";
        private static final String METHOD_POST = "POST";
    
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    
    
            if(METHOD_POST.equals(request.getMethod())) {
    
                BindingResult bindingResult = getBindingResult(modelAndView);
    
                FlashMap outFlash = RequestContextUtils.getOutputFlashMap(request);
    
                if(bindingResult == null || ! bindingResult.hasErrors() || outFlash == null ) {
                    return;
                }
    
                outFlash.put(BINDING_RESULT_FLUSH_ATTRIBUTE_KEY, bindingResult);
    
            }
    
            Map<String, ?> inFlash = RequestContextUtils.getInputFlashMap(request);
    
            if(METHOD_GET.equals(request.getMethod()) && inFlash != null && inFlash.containsKey(BINDING_RESULT_FLUSH_ATTRIBUTE_KEY)) {
    
                BindingResult flashBindingResult = (BindingResult)inFlash.get(BINDING_RESULT_FLUSH_ATTRIBUTE_KEY);
    
                if(flashBindingResult != null) {
    
                    BindingResult bindingResult = getBindingResult(modelAndView);
    
                    if(bindingResult == null) {
                        return;
                    }
    
                    bindingResult.addAllErrors(flashBindingResult);
    
                }
    
            }
    
        }
    
        public static BindingResult getBindingResult(ModelAndView modelAndView) {
    
            if(modelAndView == null) {
                return null;
            }
    
            for (Entry<String,?> key : modelAndView.getModel().entrySet()) {
                if(key.getKey().startsWith(BindingResult.MODEL_KEY_PREFIX)) {
                    return (BindingResult)key.getValue();
                }
            }
    
            return null;
        }
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm going round in circles at the moment trying to get the pattern right
At the moment I'm trying to create a kind of model in vb.net which
I am trying to implement Drools Planner for allocating timetables. At the moment, my
Having a duh moment trying to implement a new content page Here's the structure
Just trying to implement these buttons at the moment: http://web.archive.org/web/20110721191046/http://particletree.com/features/rediscovering-the-button-element/ Can't decide which to
I'm using Boost for C++ at the moment, and am trying to implement the
I'm trying to implement Jcrop with a list of images. At the moment, when
I'm trying to implement this application. At the moment I have designed tabs on
I am trying to implement a decorator chain for my data-access based on IRepository.
I'm trying to implement Swing worker in my GUI. At the moment I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.