在控制器方法内以发布模式重定向 [英] Redirect in post mode inside a controller's method

查看:110
本文介绍了在控制器方法内以发布模式重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人在遵循以下指南时遇到问题,我建议使用一种更简单的方法,例如: https ://www.youtube.com/watch?v = yaxUV3Ib4vM

If someone have problems following the guide below, I suggest to use an easier approach, like this one: https://www.youtube.com/watch?v=yaxUV3Ib4vM

我仍在遵循本教程:

I'm still following this tutorial: spring-mvc-radiobutton-and-radiobuttons-example and I've created this controller so far:

    @RequestMapping(value = "add", method = RequestMethod.GET)
public String add(Model model) {
    MyObject object = new MyObject();
    object.setParameter("fake parameter");
    model.addAttribute("add", object);
    initModelList(model);
    return "add";
}

@RequestMapping(value = "add", method = RequestMethod.POST)
public String add(@ModelAttribute("add") @Validated MyObject object, BindingResult result, Model model) {
    model.addAttribute("add", object);
    String returnVal = "redirect:/add/object";
    if(result.hasErrors()) {
        initModelList(model);
        returnVal = "add";
    } else {
        model.addAttribute("add", object);
    }       
    return returnVal;
}

@RequestMapping(value = "/add/object", method = RequestMethod.POST)
public String addObject(
                            @ModelAttribute MyObject object,
                            ModelMap model) throws DatatypeConfigurationException {
      try{
      ...marshalling results in xml output 
      ...inserting it in database
      ...showing the result

        return "objectResult";

    } catch (Exception e) {
        LOG.error(e.getMessage(), e);

        throw new RuntimeException(e);
    }
}

当然,此解决方案不起作用,因为重定向的类型为GET. 我试图将最后两种方法融合在一起,就像这样:

Of course this solution don't work, since the redirect is of type GET. I've tried to fuse the last two methods together, like so:

    @RequestMapping(value = "add", method = RequestMethod.POST)
public String add(@ModelAttribute("add") @Validated MyObject object, BindingResult result, Model model)
         throws DatatypeConfigurationException {
    model.addAttribute("add", object);
    String returnVal = "objectResult";
    if(result.hasErrors()) {
        initModelList(model);
        returnVal = "add";
    } else {
        model.addAttribute("add", object);
    }       
    try{
    ...mashalling etcetera
        return returnVal;
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);

        throw new RuntimeException(e);
    }
}

但是那样验证就不起作用了.我不知道如何解决这个问题,我想使用spring验证器,但是如果我不能使用它,我将退还该项目,这很可惜.

But in that way the validation don't work. I don't know how to solve this problem, I would like to use the spring validator but if I can't use it I will regress the project, wich is a shame.

推荐答案

如果有人在遵循同一指南时遇到问题,我建议使用一种更简单的方法,例如:

If someone have problems following the same guide, I suggest to use an easier approach, like this one: https://www.youtube.com/watch?v=yaxUV3Ib4vM It doesn't neeed to update the maven dependency

这篇关于在控制器方法内以发布模式重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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