Search code examples
javascriptjspjstl

How to access variables from the ModelAndView in JavaScript code


I have a controller which returns a ModelAndView object

return new ModelAndView("operatorDetail", model);

I can then access the elements in the model object from my JSP page like this

<c:if test="${not empty operator and operator.regionId eq item.regionId}"

my question is, is there a way I can access those elements in my javascript code ?. I thought about putting those values in an input element in HTML and make it hidden and access their values, but is there a more practical way?

thank you.


Solution

  • Yes of course you can have something like this. Declare var in jsp

    <c:set var="yourJspVar" scope="session" value=" your object"/>

    and in java-script set variable like

    <script>
      var jsVar = '${yourJspVar}';
      alert(jsVar);
    </script>