繁体   English   中英

如何将HashMap对象从一个方法传递到同一个Spring MVC控制器类中的另一个方法

[英]How to pass HashMap object from one method to another method in same Spring MVC controller class

我在Spring MVC控制器类中有两个服务(方法)。现在我想将Map Object从一个方法移动到另一个带有值的方法。

   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 它提供了一种方法来存储需要在Post / Redirect / Get上的重定向下显示在下一页上的那些属性。

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.
   }
  }

您可以尝试重定向method.from reg以使用参数进行更新

暂无
暂无

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

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