0

EDIT: 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);
    }
}

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

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.

tina
  • 243
  • 5
  • 25
  • What isn't working? A redirect works fine heck it is even a pattern [post-redirect-get](https://en.wikipedia.org/wiki/Post/Redirect/Get). So not sure what isn't working, but I guess it has to do with your understanding (looking at your code it is already full of hacks in the first place). – M. Deinum Dec 13 '16 at 12:10
  • The redirect is not working because I get the message "Request method 'GET' not supported", even if in my controller I mapped it as a RequestMethod.POST. It's possible that I didn't understood the guide and some other things, since I'm kind of newbie. – tina Dec 13 '16 at 12:18
  • Well the message i quite clear isn't it... A redirect is, as you stated a GET, but you have no method for that. So yes then you will get that message because there is nothing that can handle the GET. – M. Deinum Dec 13 '16 at 13:06
  • I wanted to avoid the use of GET in this case, and know if there's a way to redirect to my page maintaining the post method. Without the validator infact I used the post method above, and it works fine. – tina Dec 13 '16 at 13:33
  • No as a redirect is always a GET. – M. Deinum Dec 13 '16 at 13:42
  • For this reason I wrote the second method, since I wasn't able to find any workaround at this problem, but in that way the validation don't work. – tina Dec 13 '16 at 13:43
  • As mentioned it is unclear what you want, your controller is (imho) a bit of a mess (the guide you are following makes things more complex then needed). So instead writing what doesn't work, write what you want and what you have tried. But you haven't written what you want, you said this doesn't work. – M. Deinum Dec 13 '16 at 13:45
  • I'm sorry my question was not clear, what I want is validate my form (in the first example the first two methods do that), then take the results, marshall them in xml (I already know how to do that), send xml to the database, then take the results (of the form itself) and present them to the user. Without the Validation process everything was working fine. I wonder if I should change validation method, or if can work as it is. – tina Dec 13 '16 at 13:55
  • And why would you need a redirect for that?! Apart from the added complexity it doesn't add anything. – M. Deinum Dec 13 '16 at 13:57
  • Because the second method in the question above don't validate the form, so I thought it was easier to just try to use the same method I already had. Did you suggest to continue try the second way? – tina Dec 13 '16 at 14:20
  • And why for using a method you would need a redirect... Even a method annotated with `@RequestMapping` is just a method. – M. Deinum Dec 13 '16 at 14:24
  • 1
    What is the error validating with the second method? There is no reason you need two methods to validate and then for logic - I can see from the code, the second method checks for errors then continues to process regardless, is it just a case of needing to return on the case of errors there? – rhinds Dec 13 '16 at 14:45

1 Answers1

0

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

tina
  • 243
  • 5
  • 25