0

I have a controller class in spring. I am passing values such as a simple string or a hashmap. I know how to get values in thymeleaf. I want to get values on my html page in pure javascript, no thymeleaf. My controller class :

    String s="RUSSIA";
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String Country( Model model) {
    model.addAttribute("country", s);
    return "index";
    }

Now I want to get this string in javascript variable. I am using HTML not JSP.

1
  • There are a large number of ways you can do that. Without knowing what libraries you're using on the JS side and what browsers you're targeting, we can't possibly suggest a good one. Commented Nov 16, 2016 at 15:46

1 Answer 1

1

You could do something like this:

<script th:inline="javascript">
/*<![CDATA[*/

    var country = [[${country}]];
    console.log(country);

/*]]>*/
</script>

If you want to run your html offline you can do this, and the JS variable will be always the fixed value of Russia

var country = /*[[${country}]]*/ 'Russia';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.