使用Post/Redirect/Get模式保存模型状态 [英] Preserving model state with Post/Redirect/Get pattern

查看:93
本文介绍了使用Post/Redirect/Get模式保存模型状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻,我正在尝试使用Spring MVC 3.1实现Post/Redirect/Get模式.保留和恢复模型数据+验证错误的正确方法是什么?我知道我可以在POST方法中保留带有RedirectAttributes的模型和BindingResult.但是,从Flash作用域以GET方法恢复它们的正确方法是什么?

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?

我已经完成以下操作:

    @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;
}

这使我可以在出现错误的情况下保留给定的用户数据.但是,恢复错误的正确方法是什么?(BindingResult)我想以带有spring表单标签的表单来显示它们:

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="*" />

此外,如何从get方法访问Flash作用域会很有趣?

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

推荐答案

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;
    }


}

这篇关于使用Post/Redirect/Get模式保存模型状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆