解决方案:
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
改成
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates
spring.thymeleaf.suffix=.html
Controller 写法
import com.poterliu.entity.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @Controller public class IndexController { /** * 测试 thymeleaf * http://127.0.0.1:8080/home * @param model * @return */ @GetMapping("home") public String thymeleafDemo(Model model) { User user = new User(); user.setUsername("jack"); user.setPassword("112233"); user.setHobbies(Arrays.asList(new String[]{"singing", "dancing", "football"})); Map<String, String> maps = new HashMap<>(); maps.put("1", "o"); maps.put("2", "g"); maps.put("3", "a"); maps.put("4", "j"); user.setSecrets(maps); model.addAttribute("user", user); return "/thymeleafDemo"; } @GetMapping("") public String index(Model model) { return "/index"; } }
参考: