如何使用ModelAndView中的参数将其传递给HTML文件中的javascript中的window.onload函数?
控制器文件:
@RequestMapping(value = "/admin/printtext")
public ModelAndView printtext() {
ModelAndView modelAndView = new ModelAndView("printtext", "showBackLink", false);
String name = "name0";
modelAndView.addObject("name", name);
return modelAndView;
}
HTML文件中的脚本:
<script type="text/javascript">
window.onload = function (${name}) {
document.write('text')
}
</script>
答案 0 :(得分:0)
您将其插入错误的位置。
<script type="text/javascript">
var serverData = "${name}";
window.onload = function () {
document.write(serverData); // don't actually use document.write()
}
</script>