admin管理员组文章数量:1352298
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.
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');
}
});
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.
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');
}
});
Share
Improve this question
asked Feb 1, 2017 at 19:44
josandluqjosandluq
331 gold badge1 silver badge4 bronze badges
3 Answers
Reset to default 5We can pass Spring MVC variables to Javascipt for example:
<script>
var number = [[${number}]];
var message = "[[${message}]]";
</script>
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.
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");
本文标签: How to pass Spring mvc controller variable to javascriptStack Overflow
版权声明:本文标题:How to pass Spring mvc controller variable to javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743911714a2560522.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论