为什么我不能从被调用的方法添加到模型中?

时间:2011-11-14 18:15:03

标签: spring jsp spring-mvc

当我这样做时:

@RequestMapping(value = "/something", method = RequestMethod.POST)
public String stuff(
    @ModelAttribute("xxx") ReviewAB7500Data xxx,
    HttpServletRequest request, Model model) throws GeneralDAOException {
    model.addAttribute("somekey",someValue);
 }

一切正常,我可以在JSP中看到我的对象。但是当我改变它时:

private void someOtherMethod(Model model) {
    model.addAttribute("somekey",someValue);
    return;
}

@RequestMapping(value = "/something", method = RequestMethod.POST)
public String stuff(
    @ModelAttribute("xxx") ReviewAB7500Data xxx,
    HttpServletRequest request, Model model) throws GeneralDAOException {
    someOtherMethod(model);
    return viewName;
 }

...名为“somekey”的addtribute永远不会放在模型上,或者至少在我的JSP中不可见。这里有什么提示吗?

1 个答案:

答案 0 :(得分:0)

我相信你需要为该方法添加一个额外的参数:

@RequestMapping(value = "/something", method = RequestMethod.POST)
public String stuff(@ModelAttribute("xxx") ReviewAB7500Data xxx,
                    BindingResult result,
                    HttpServletRequest request, 
                    Model model) throws GeneralDAOException