views:

26

answers:

1

Is there a way in Spring2 to build dynamic views to which a Controller can redirect to?

I have a form with a hidden field for an ID. If the form is submited or other exception occurs i want to redirect back to the form (i have set formView). It redirect ok, but when it redirect back to form it is loosing the ID parameter. Is there a way i can put it back ?

I know in Struts2 you could do this by having an action result like this:

<result name="success" type="redirect" >
              <param name="location">index</param>
              <param name="category">${category}</param>
              <param name="pageNumber">${pageNumber}</param>
              <param name="parse">true</param>
              <param name="encode">true</param>
</result>

Long story short i want to be able to redirect to an URL like: index.htm?id=3

A: 

Yes it is. Here's an example from the PetClinic:

@Controller
@RequestMapping("/editPet.do")
@SessionAttributes("pet")
public class EditPetForm {

    // ...

    @ModelAttribute("types")
    public Collection<PetType> populatePetTypes() {
        return this.clinic.getPetTypes();
    }

    @RequestMapping(method = RequestMethod.POST)
    public String processSubmit(
            @ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) {

        new PetValidator().validate(pet, result);
        if (result.hasErrors()) {
            return "petForm";
        }
        else {
            this.clinic.storePet(pet);
            status.setComplete();
            return "redirect:owner.do?ownerId=" + pet.getOwner().getId();
        }
    }
}
Daniel Alexiuc