SpringMVC 前后端传参

2023-07-07 11:18:09 浏览数 (1)

ModelAndView thymeleaf

废话不多说,直接上demo。

properties:

代码语言:javascript复制
# ----------------------Springmvc配置-----------------------------
# 指定前端页面的前缀
spring.mvc.view.prefix=/
# 指定前端页面的后缀
spring.mvc.view.suffix=.html
# ------------------------thymeleaf配置-----------------------------------
spring.thymeleaf.servlet.content-type=text/html
# 指定文件路径
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template=false
spring.thymeleaf.check-template-location=false
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=false

maven:

代码语言:javascript复制
<!-- thymeleaf -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
        <!-- 省略其他springboot包 -->

controller:

代码语言:javascript复制
// 页面跳转controller
@Controller
public class PageController {
    @RequestMapping("/")
    public ModelAndView index() {
        ModelAndView mav = new ModelAndView();
        mav.addObject("testParam", "欧吼");
        mav.setViewName("index");
        return mav;
    }
}

html:

代码语言:javascript复制

<html lang="zh-CN"
      xmlns:th="http://www.thymeleaf.org">
<script th:inline="javascript">
    var testParam = [[${testParam}]];
    console.log(testParam)
</script>
</html>

效果:

0 人点赞