josandluq
josandluq

Reputation: 33

How to pass Spring mvc controller variable to javascript

I am trying to get a variable for a javascript function from an object that sends it to view by the controller.

The object with which I am working is Bpmsn.

enter image description here

Through the controller I injected the object into view.

My method of the controller is:

@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;
}

This method I use to make a display of the object.

In the jsp view I inject the attributes of the object, except the textXML that I will use in a javascript script.

<div>
<ul>
    <li><b><spring:message code="bpmsn.ticker" />:</b> <jstl:out
            value="${bpmsn.ticker}" /></li>
    <li><b><spring:message code="bpmsn.title" />:</b> <jstl:out
            value="${bpmsn.title}" /></li>
    <li><b><spring:message code="bpmsn.summary" />:</b> <jstl:out
            value="${bpmsn.summary}" /></li>
    <li><b><spring:message code="bpmsn.authoredMoment" />:</b> <jstl:out
            value="${bpmsn.authoredMoment}" /></li>
    <li><b><spring:message code="bpmsn.likes" />:</b> <jstl:out
            value="${bpmsn.likes}" /></li>
    <li><b><spring:message code="bpmsn.dislikes" />:</b> <jstl:out
            value="${bpmsn.dislikes}" /></li>
</ul>

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

I tried to call the attribute as in the view but it does not work

<script type="text/javascript">
    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');
        }
    });

Upvotes: 1

Views: 16487

Answers (3)

SAAD BELEFQIH
SAAD BELEFQIH

Reputation: 372

We can pass Spring MVC variables to Javascipt for example:

 <script>
    var number = [[${number}]];
    var message = "[[${message}]]";
</script>

Upvotes: 5

Mihir Gohel
Mihir Gohel

Reputation: 316

Java object stored in model, cannot be accessed in scripts as it is executed on client side.

Easy and simple solution is to serialize object in JSON, add it to model, access in Javascript.

You need something like this.

ObjectMapper objectMapper = new ObjectMapper();
result.addObject("bpmsn", objectMapper.writeValueAsString(bpmsn));

And it can be accessed in script simply.

var bpmnJsonString = '${bpmsn}';
Obj bpmn = JSON.parse(bpmnJsonString);
var bpmnXML = bpmn.textXML; 
alert(bpmnXML)

Hope it solves your problem.

Upvotes: 1

Alex Minjun Yu
Alex Minjun Yu

Reputation: 3707

This does not work because var bpmnXML = ${bpmsn.textXML}; is executed only on client side.

One solution would be that you can introduce a hidden field with an attribute with value ${bpmsn.textXML} which will be executed in the server side. You will be able to access this value in your javascript (I'll use jQuery for illustration).
e.g.

<input id="bpmsn-textxml" type="hidden" data-textxml="${bpmsn.textXML}"/>

var bpmnXML = $("#bpmsn-textxml").data("textxml");

Upvotes: 0

Related Questions