前端js获取SpringMvc后台model中传值
  TEZNKK3IfmPf 2023年11月13日 25 0

也许你迷茫,但是我想说,在你迷茫的同时,保持本心,过好今天就好。

使用 SpringBoot +SpringMVC +thymeleaf 组合实现的功能,期望在 thymeleaf 中的html中的js中 获取 springboot 中 Model 中设置的值

@Controller
@RequestMapping("/web/computerTest")
public class ComputerTestWebController {
     
       

    /** * 选择考试 */
    @GetMapping("/chooseExam.html")
    public String chooseExam(@RequestParam String token, Model model) {
     
       
      
        Boolean showV1 = true;
        Boolean showPre = true;
     
        model.addAttribute("showV1", showV1);
        model.addAttribute("showPre", showPre);
        //对应显示的html
        return "computertestweb/chooseExamV2";
    }

}

这是我对应的html文件
前端js获取SpringMvc后台model中传值

有两种方式:

1 内联js方式

<script th:inline="javascript">
    let activityName = "first"
    
    if([[${
     
       showV1}]]){
     
       
        activityName = "first"
    }else{
     
       
        activityName = "second"
    }
</script>

2、使用隐藏域,

使用隐藏域, 先把model的值通过标签的方式放到某个input标签下,再到js中通过js或者jquery按照id的方式选取

2.1 在html 中写入隐藏标签
<input type="hidden" id="showV1" value="${showV1}}">
2.2 在js中通过ID获取标签获取值
let data= $("#showV1").val();
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月13日 0

暂无评论

推荐阅读
TEZNKK3IfmPf