简体   繁体   English

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

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

I have two services(methods) in a Spring MVC controller class.Now i want to move Map Object from one method to another method with values. 我在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
   }
  }

is there any way to do like this please give solution. 有什么方法可以这样做请给出解决方案。 Thanks in advance 提前致谢

Method 1: Using HttpSession. 方法1:使用HttpSession。 You can use HttpSession to store your object. 您可以使用HttpSession来存储您的对象。 See the below example 请参阅以下示例

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");
   }
  }

Method 2: Using FlashAttribute . 方法2:使用FlashAttribute It provides a way to store those attributes that are required to be shown on the next page at a redirect on a Post/Redirect/Get. 它提供了一种方法来存储需要在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.

相关问题 如何在Spring MVC中将LIST从一个控制器传递到另一个控制器 - How to pass LIST from one controller to another controller in spring mvc 从一种控制器方法到另一种方法的内部转发(Spring-MVC) - Internal forwarding from one controller method to another (Spring-MVC) 如何将对象从一个主方法传递给另一类的主方法 - how to pass an object from one main method to a main method in another class 如何从另一个类调用HashMap的方法? - How to call the method of HashMap from another class? 如何在同一个方法中将参数值从一种方法传递到另一种方法 class - How to pass parameters values from one method to another one in the same class 我们如何在不借助父类的情况下将变量从一个方法传递到同一个类中的另一个方法? - How can we pass variables from one method to another in the same class without taking the help of parent class? 如何将值从一个 class 的 onOptionsItemSelected 方法传递到另一个 class - How to pass value from onOptionsItemSelected method of one class to another class 从一个类在另一个字符串上调用一个方法(hashmap / hashset)。 - Calling a method (hashmap/ hashset) from one class on a string in another. 如何将另一个类中的对象传递给paintComponent方法? - How do I pass an object from another class into the paintComponent method? Spring MVC:如何使用表单将模型对象传递给控制器​​方法 - Spring MVC : How to pass model object to controller method with out using form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM