根據自我需求,thymeleaf可以直接將后端數據傳遞給js中進行使用,例如:
1.后端接口數據:
@Controller
public class TestController { @RequestMapping(value = "test", method = RequestMethod.GET) public String test(Model model) { model.addAttribute("test", "hello"); return "index"; } }
利用thymeleaf將其傳遞給js使用:
<script th:inline="javascript"> var test= [[${test}]]; console.log(test); </script>
注:[[…]]之間的內容可以被賦值。為了使其生效,必須在此標簽或者任何父標簽上有th:inline屬性。此屬性有三種值(text , javascript and none)