Spring MVC : How to access a modelAndView XML obje

2019-07-26 19:28发布

问题:

I have a String variable, containing XML. I added it to my model with modelAndView.addObject(...). Now, how can I access this object in Javascript ?

回答1:

Let's say that in your controller you did something like

modelAndView.addObject("someValue", someValue).

In your JSP, you can do this :

<script type="text/javascript">
    var someValue = "${someValue}";
</script>

And then you can use this variable in your JS.



回答2:

Assuming you want to access Model attribute
Using <c:set> like

<c:set var="myData" value="${myObject.data}" />    

and then you can use this var in js using ${myData}.
For example in jQuery selector

<script>
    $("#${myData}").val('foo');
</script>