开发者

Spring2 web MVC - dynamic views for controller?

开发者 https://www.devze.com 2023-01-15 13:29 出处:网络
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.

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>
              <p开发者_运维百科aram 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


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

精彩评论

暂无评论...
验证码 换一张
取 消