title: How to pass Spring mvc controller variable to javascript
body: <p>I am trying to get a variable for a javascript function from an object that sends it to view by the controller.</p>
  
  <p>The object with which I am working is Bpmsn. </p>
  
  <p><a href="https://i.stack.imgur.com/FxlAo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FxlAo.png" alt="enter image description here"></a></p>
  
  <p>Through the controller I injected the object into view.</p>
  
  <p>My method of the controller is:</p>
  
  <pre><code>@RequestMapping(value = "/display")
  public ModelAndView index2(@RequestParam int bpmsnId) {
      ModelAndView result;
      Bpmsn bpmsn;
  
      bpmsn = bpmsnService.findOne(bpmsnId);
  
      result = new ModelAndView("editor/display");
      result.addObject("bpmsn", bpmsn);
  
      return result;
  }
  </code></pre>
  
  <p>This method I use to make a display of the object.</p>
  
  <p>In the jsp view I inject the attributes of the object, except the textXML that I will use in a javascript script.</p>
  
  <pre><code>&lt;div&gt;
  &lt;ul&gt;
      &lt;li&gt;&lt;b&gt;&lt;spring:message code="bpmsn.ticker" /&gt;:&lt;/b&gt; &lt;jstl:out
              value="${bpmsn.ticker}" /&gt;&lt;/li&gt;
      &lt;li&gt;&lt;b&gt;&lt;spring:message code="bpmsn.title" /&gt;:&lt;/b&gt; &lt;jstl:out
              value="${bpmsn.title}" /&gt;&lt;/li&gt;
      &lt;li&gt;&lt;b&gt;&lt;spring:message code="bpmsn.summary" /&gt;:&lt;/b&gt; &lt;jstl:out
              value="${bpmsn.summary}" /&gt;&lt;/li&gt;
      &lt;li&gt;&lt;b&gt;&lt;spring:message code="bpmsn.authoredMoment" /&gt;:&lt;/b&gt; &lt;jstl:out
              value="${bpmsn.authoredMoment}" /&gt;&lt;/li&gt;
      &lt;li&gt;&lt;b&gt;&lt;spring:message code="bpmsn.likes" /&gt;:&lt;/b&gt; &lt;jstl:out
              value="${bpmsn.likes}" /&gt;&lt;/li&gt;
      &lt;li&gt;&lt;b&gt;&lt;spring:message code="bpmsn.dislikes" /&gt;:&lt;/b&gt; &lt;jstl:out
              value="${bpmsn.dislikes}" /&gt;&lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p></p>
  
  <p>With the textXML attribute I want to create a javascript function to import that xml into a bpmn modeler, but I do not know how to get the textXML attribute of the object injected into the view from the javascript script</p>
  
  <p>I tried to call the attribute as in the view but it does not work</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      var bpmnXML = ${bpmsn.textXML}; //not work
      alert(bpmnXML)
      // BpmnJS is the BPMN viewer instance
      var viewer = new BpmnJS({
          container : '#canvas'
      });
  
      // import a BPMN 2.0 diagram
      viewer.importXML(bpmnXML, function(err) {
          if (err) {
              // import failed :-(
              alert('could not import BPMN 2.0 diagram', err);
          } else {
              // we did well!
              var canvas = viewer.get('canvas');
              canvas.zoom('fit-viewport');
          }
      });
  </code></pre>