如何在同一个Controller中访问另一个RequestMapping的模型?

时间:2017-12-08 09:09:13

标签: java spring-mvc

如何使与控制器中的RequestMapping相关联的对象可以被返回到同一视图页面的同一控制器中的另一个RequestMapping访问?谢谢。

这是我的示例代码,它只放在一个控制器中:

@RequestMapping(value="firstMapping",method=RequestMethod.POST)
public ModelAndView firstMapping (HttpServletRequest request) {

    //myObject is processed here

    ModelAndView mav = new ModelAndView();
    mav.setViewName("samplepage");
    mav.addObject("myObject",myObject); //How do I pass this object to the mapping below?
    return mav;
}

@RequestMapping(value="secondMapping",method=RequestMethod.POST)
public ModelAndView secondMapping (HttpServletRequest request) {

    //I want to do something else here but I need the object from
    //the mapping above. For example myObject2 is processed here

    ModelAndView mav = new ModelAndView();
    mav.setViewName("samplepage");

    mav.addObject("myObject",myObject);
    mav.addObject("myObject2",myObject2);
    return mav;
}

1 个答案:

答案 0 :(得分:0)

在第一种方法中,在第二种方法中设置attribute.Set视图后,您应该将请求转发到第二种方法,而不是设置视图。使用此请求后,处理后转发到第二种方法。

def main():
    b = 5
    a = subfunction(b)
    print a

def subfunction(value):
    a = value + 10
    return a

if __name__ == '__main__':
    main()

你也可以使用ModelandView从方法1到2携带对象。