我在Spring MVC控制器类中有两个服务(方法(。现在我想将地图对象从一个方法移动到另一个带有值的方法。
public class Controller{
@RequestMapping(value="/reg", method=RequestMethod.POST)
public ModelAndView loginData(@ModelAttribute("loginBean")LoginBean
loginBean,ModelMap model) throws IOException, ParseException
{
//Here i have map object with values.
}
@RequestMapping(value="/update",method=RequestMethod.POST)
public ModelAndView updateForm(@ModelAttribute("frontBean")FrontBean
frontBean,ModelMap model)
{
//here i want to Map Object for update the values
}
}
有什么办法可以做到这一点吗请给出解决方案。提前致谢
方法 1:使用 HttpSession。您可以使用 HttpSession 来存储对象。请参阅以下示例
public class Controller{
@RequestMapping(value="/reg", method=RequestMethod.POST)
public ModelAndView loginData(@ModelAttribute("loginBean")LoginBean
loginBean,ModelMap model) throws IOException, ParseException
{
Map map = new HashMap();
HttpSession session = req.getSession(false);
session.setAttribute("myMapObject", map);
}
@RequestMapping(value="/update",method=RequestMethod.POST)
public ModelAndView updateForm(@ModelAttribute("frontBean")FrontBean
frontBean,ModelMap model)
{
HttpSession session = req.getSession(false);
session.getAttribute("myMapObject", map);
session.removeAttribute("myMapObject");
}
}
方法2:使用FlashAttribute。它提供了一种方法来存储那些需要在下一页上在发布/重定向/获取重定向时显示的属性。
public class Controller{
@RequestMapping(value="/reg", method=RequestMethod.POST)
public ModelAndView loginData(@ModelAttribute("loginBean")LoginBean
loginBean,ModelMap model,RedirectAttributes redirectAttrib) throws IOException, ParseException
{
Map map = new HashMap();
redirectAttrib.addAttribute("myMap", map);
return "redirect:/update";
}
@RequestMapping(value="/update",method=RequestMethod.POST)
public ModelAndView updateForm(@ModelAttribute("frontBean")FrontBean
frontBean,@ModelAttribute("myMap")Map myMap,ModelMap model)
{
//Use myMap object accordingly.
}
}
你可以尝试重定向方法.从reg更新参数