title: How to create a hashmap in a controller in java and spring
body: <p>I I have a controller which handles get and post requests. However in this class I want an instance of a <code>HashMap&lt;int, Greeting&gt;</code> which stores all my object of type Greeting. I only want it to be initiated once when GreetingController is called. Then I want to be able to store more and more greetings as more Greetings are created. How do I do that?</p>
  
  <p>I will show you the code for both the controller (GreetingController) and my Greeting class.</p>
  
  <p>Here is the controller:</p>
  
  <pre><code>@Controller
  public class GreetingController {
  
  
  
      @RequestMapping(value="/greeting", method=RequestMethod.GET)
      public String greetingForm(Model model) {
          model.addAttribute("greeting", new Greeting());
          return "greeting";
      }
  
      @RequestMapping(value="/greeting", method=RequestMethod.POST)
      public String greetingSubmit(@ModelAttribute Greeting greeting, Model model) {
          model.addAttribute("greeting", greeting);
          return "result";
      }
  }
  </code></pre>
  
  <p>Here is the Greeting class:</p>
  
  <pre><code>package main.java.hello;
  
  public class Greeting {
  
      private long id;
      private String content;
  
      public long getId() {
          return id;
      }
  
      public void setId(long id) {
          this.id = id;
      }
  
      public String getContent() {
          return content;
      }
  
      public void setContent(String content) {
          this.content = content;
      }
  
  }
  </code></pre>