简体   繁体   English

如何使用Spring MVC 3在控制器中从模型中获取对象?

[英]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. 我有一个控制器,它有一个方法来处理传入的GET数据,在model存储一些东西,然后重定向到另一个处理这些对象的页面。

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. 如果您正在使用或有能力更新到新发布的Spring 3.1 ,则可以使用新范围的Flash变量。

http://static.springsource.org/spring/docs/3.1.0.RC1/spring-framework-reference/html/mvc.html#mvc-flash-attributes 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. 如果你不能使用3.1,你可能自己实现解决方案。 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 ). 目前,我只是获取模型的Map ,通过它的键( String名称)获取我想要的对象,然后将其转换为它真正的对象(而不仅仅是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): 一种可能的解决方案是使用@ModelAttribute ,虽然它非常难看,因为您需要为该属性禁用数据绑定(为了安全性):

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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