title: Injecting JSP from Spring MVC controller
body: <p>I have a Spring MVC web app that uses JSP for the View technology. In the controller I am injecting values into the <code>ModelAndView</code> object that will then be used (with the respective JSP) to help construct the final HTML to return to the client-side.</p>
  
  <p>The controller:</p>
  
  <pre><code>@RequestMapping(value = "/widgets.html", method = RequestMethod.POST)
  public ModelAndView widgets(@Model Fruit fruit, @RequestParam("texture") String texture) {
      ModelAndView mav = new ModelAndView();
  
      // The name of the JSP to inject and return.
      max.setViewName("widgets/AllWidgets");
  
      int buzz = calculateBuzzByTexture(texture);
  
      mav.addObject("fruitType", fruit.getType());
      mav.addObject("buzz", buzz);
  
      return mav;
  }
  </code></pre>
  
  <p>This controller (which handles <code>/widgets.html</code> requests) does some lookups and returns the injected <code>AllWidgets.jsp</code> page. In that JSP page, I need to get access to both the <code>fruitType</code> and <code>buzz</code> variables (inside both HTML and JS), but not sure how I can do this. For instance, assuming <code>fruitType</code> is a String (and <code>buzz</code> is an <code>int</code>), how would I print them in both HTML and JS:</p>
  
  <pre><code>&lt;script type="text/javascript&gt;
      alert("Buzz is " + buzz);
  &lt;/script&gt;
  
  &lt;div&gt;
      &lt;h2&gt;The type of fruit is ??? fruitType ???&lt;/h2&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Thanks in advance.</p>