java – Access to a model attribute in Javascript with Spring MVC

java – Access to a model attribute in Javascript with Spring MVC

Spring controller

    @RequestMapping(value = /dashboard)
    public ModelAndView home(HttpServletRequest request, 
    HttpServletResponse 
    res, Model model) {

        // Return answers dictionary from DB to dashboard view
        CompQuest dizRisp = new CompQuest();
        dizRisp.setDizComp(dashDao.getRispEnd());

        Gson gson = new Gson() ;
        // Use Gson dependency to convert hashmap to String

        String strmap = gson.toJson(dizRisp)
        model.addAttribute(dizRisp, strmap);

        return new ModelAndView(dashboard);    
    }

Javascript

<script>
    
   $(document).ready(function(){

    var element = JSON.parse(${dizRisp});

    $.each( element , function( key, value ) {
        
         console.log(key);
         console.log(value);

    });
  
   });
  

</script>

Hope this is what your trying to achieve.

java – Access to a model attribute in Javascript with Spring MVC

Leave a Reply

Your email address will not be published. Required fields are marked *