根据需求,后端绑定数据到前端的javascript中进行使用,例如:
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)