How to get spring mvc controller model key value inside javascript?

前端 未结 3 2039
忘了有多久
忘了有多久 2021-01-12 03:12

I am using a spring mvc controller. Inside controller i am putting some value lets say string inside model. Now i would like to retrive that value or lets just say print tha

相关标签:
3条回答
  • 2021-01-12 04:08

    Use this:

    var movie_name = "${movie}";
    

    instead of:

    var movie_name = ${movie};
    

    When using ${movie}, the value gets placed on the page without quotes. Since I'm guessing it's a string, Javascript requires strings be surrounded by quotes.

    If you checked your browser's console, you probably would've seen an error like Unexpected identifier or ___ is not defined.

    0 讨论(0)
  • 2021-01-12 04:12

    Try this...

    If you had added the object into the model as:

    model.addAttribute("someObject", new Login("uname","pass"))
    

    Then you get the properties of the model object as

    var user_name = ${someObject.uname}; // This is assuming that the Login class has getter as getUname();
    var user_pass = ${someObject.pass};
    
    0 讨论(0)
  • 2021-01-12 04:15
    <html>
    jsp code ...
    
    <script>
    
    some js code ..
    ..
    var formProperty = <c:out value="${fromBean.property}" />
    
    </script>
    
    ..
    
    </html>
    

    This worked for me where formBean is the name of form backing object and property is filed of the form class

    0 讨论(0)
提交回复
热议问题