简体   繁体   中英

How can I get an object out of the model in the controller with Spring MVC 3?

I have a controller with a method that handles incoming GET data, stores some things in the model , and then redirects to another page that deals with these objects.

I can't seem to find any good way of getting the object stored in the first method back out of the model to use in the second method. How can I do this?

Here's the top of the controller:

@Controller
@RequestMapping("/reviews")
@SessionAttributes({"review", "externalReview"})
public class ReviewController {
    // [SNIP]
}

Here's the code that adds the objects I'm after to the model:

@RequestMapping(value="/new", params="UName", method=RequestMethod.GET)
public String newFormFromExternal(@ModelAttribute("externalReview") ExternalReview externalReview, Model model) throws IncompleteExternalException {
    // Convert the inbound external
    Review fromExternal = ExternalReviewUtil.reviewFromExternalReview(externalReview, externalDAO);

    // Add the externalReview to the session so we can look to see if we got a reviewee on the way in
    model.addAttribute("externalReview", externalReview);

    model.addAttribute("review", fromExternal);

    return "redirect:/reviews/newFromExternal";
}

You are in luck.

If you are using or have ability to update to the newly released Spring 3.1 , you can make use of the newly scoped Flash variables.

http://static.springsource.org/spring/docs/3.1.0.RC1/spring-framework-reference/html/mvc.html#mvc-flash-attributes

If you can't use 3.1, you probably can implement the solution yourself. Essentially you want to capture the model object required to be present in the redirect, put in the session, and remove it once it is retrieved to keep your session from bloating.

Currently, I'm just getting a Map of the model, getting the object I want out by it's key (the String name), and then casting it to the object it really is (rather than just Object ).

Here's the code:

@RequestMapping(value="/newFromExternal", method=RequestMethod.GET)
public String newExternalForm(Model model) {
    // Get the review from the model
    Review review = (Review) model.asMap().get("review");

    /*** Do stuff with the review from the model ****/

    return "reviews/newFromPacs";
}

This way works, but it seems hacky and clunky. Is this really the only way?

One possible solution is to use @ModelAttribute , though it's quite ugly since you'll need to disable databinding for that attribute (for security):

@RequestMapping(value="/newFromExternal", method=RequestMethod.GET) 
public String newExternalForm(@ModelAttribute Review review) {
    ...
}

@InitBinder("review")
public void disableReviewBinding(WebDataBinder b) {
    b.setAllowedFields();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM