thymeleaf中js取值

博客介绍了在Thymeleaf里JS取值的方法,强调必须添加th:inline=\javascript\才能使用该方法,并给出了具体代码示例,通过代码实现从basicInfo对象中取值并封装成JSON对象,最后进行弹窗显示。
注意:必须加上th:inline="javascript"才能够使用该方法。

<script th:inline="javascript">
function changebasic() {
   var introduce = [[${basicInfo.introduce}]];
   var idiograph = [[${basicInfo.idiograph}]];
   var topicname = [[${basicInfo.topicname}]];
   var json={};
   json.topicname=topicname;
   json.idiograph=idiograph;
   json.introduce=introduce;
   alert(JSON.stringify(json));
}
在Spring Boot 2.7.17中使用Thymeleaf加载不到js和css文件,可尝试以下解决办法: ### 检查Thymeleaf缓存配置 可在`application.properties`或`application.yml`中对Thymeleaf缓存进行配置。开发时建议将缓存关闭,这样修改静态资源后,只需重新构建项目并刷新页面即可看到效果。 在`application.properties`中的配置示例: ```properties spring.thymeleaf.cache=false ``` 在`application.yml`中的配置示例: ```yaml spring: thymeleaf: cache: false ``` 这里需注意,当`spring.thymeleaf.cache`取值为`true`时,修改html、js、css等静态资源后需要重启服务器;取值为`false`时,修改后只需重新构建项目(如使用`CTRL + F9`)即可 [^1]。 ### 检查模板文件位置及静态资源路径 在`application.properties`或`application.yml`里配置正确的模板文件位置和静态资源路径。 在`application.properties`中的示例: ```properties spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html ``` 在`application.yml`中的示例: ```yaml spring: thymeleaf: prefix: classpath:/templates/ suffix: .html ``` 通常,静态资源(如js和css文件)应放在`src/main/resources/static`目录下。 ### 检查依赖是否添加 要保证项目中已经添加了Thymeleaf依赖。 若使用Maven,在`pom.xml`中添加如下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 若使用Gradle,在`build.gradle`的`dependencies`中添加: ```groovy compile('org.springframework.boot:spring-boot-starter-thymeleaf') ``` [^2][^3][^5] ### 检查文件引用方式 在Thymeleaf模板中,使用正确的方式引用js和css文件。 引用css文件示例: ```html <link rel="stylesheet" type="text/css" th:href="@{/css/style.css}"> ``` 引用js文件示例: ```html <script type="text/javascript" th:src="@{/js/script.js}"></script> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值