使用Thymeleaf 模板时Controller 层要使用@Controller注解,使用RestController注解不会返回html页面
要在application.properties配置文件里面添加Thyemleaf 配置
#返回页面的路径
spring.thymeleaf.prefix=classpath:/templates/
#文件后缀
spring.thymeleaf.suffix=.html
#模式
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.servlet.content-type=text/html
#编码格式
spring.thymeleaf.encoding=utf-8
#不需要缓存
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**
select 默认选中选择下拉框
<form id="save_info_form" method="post" role="form" class="form-horizontal" th:object="${subAreaItem}"
<select id="subAreaItemType" onchange="changeSelect()" th:field="*{type}" class="form-control" readonly="readonly">
<option value="1" th:selected="*{type} eq '1'">观察类</option>
<option value="2" th:selected="*{type} eq '2'">抄表类</option>
<option value="3" th:selected="*{type} eq '3'">测量类</option>
</select>
</form>
报错An error happened during template parsing
我自己遇到这个报错的原因是因为Thyemleaf 模板中使用的数据后台没有传过来,我在sql中添加了查询的字段就解决了