IDEA使用(4 springboot 前端thymeleaf 尝试)
springboot 官方不推荐使用jsp作为页面显示,
而是推荐使用thymeleaf
首先导入依赖包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
配置application.properties文件
设置不使用缓存
spring.thymeleaf.cache=false
编写controller
@RestController public class HelloWorld { @RequestMapping("/user") public String user(HttpServletRequest request){request.setAttribute("user","hello thymeleaf"); return "user"; }}
然后再resource目录下创建templates 文件夹
在themplates下新建html文件
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>templates 测试页</title> </head> <body> <h1 th:text="${user}">Hello</h1> </body> </html>
然后启动项目
喜闻乐见,有报错
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
原因
spring boot 会默认加载org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
这个类
DataSourceAutoConfiguration类使用了@Configuration注解向spring注入了dataSource bean。因为工程中没有关于dataSource相关的配置信息,当spring创建dataSource bean因缺少相关的信息就会报错。
解决方式
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
然后,项目不报错了,但是据结果并不是想要的,并没有去访问thymeleaf页面,而是直接返回了字符串
错误的原因是: 注解使用错误,
@RestController=Controller + ResponseBody
返回的内容是你return中的内容,如果是return "Hello World",页面显示的就是Hello World。加上Controller,返回的是return中对应的页面,比如return “hello”,页面的名称是hello。
@Controller public class HelloWorld {
报错
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "user", template might not exist or might not be accessible by any of the configured Template Resolvers
出错原因: 这个就比较低级的错误了
文件夹命名错误,
大功告成
再测试点其他的
package com.example.demo.web; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; import java.util.ArrayList; import java.util.List; /** * Created by shj on 2018/7/12. */ @Controller public class HelloWorld { @RequestMapping("/user") public String user(HttpServletRequest request){ List<String> list1=new ArrayList<>(); for (int i = 0; i <10 ; i++) { list1.add(""+i); } request.setAttribute("list",list1); request.setAttribute("user","hello thymeleaf"); return "user"; } @RequestMapping("/user1") public String user1(HttpServletRequest request){ List<String> list1=new ArrayList<>(); for (int i = 0; i <10 ; i++) { list1.add(""+i); } request.setAttribute("list",list1);
request.getSession().setAttribute("shj","帅哥");return "/user1"; }}
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>templates 测试页</title> </head> <body> <h1 th:text="${user}">Hello</h1> 11111 <table border="1px solid red"> <tr th:each="str:${list}"> <td th:text="${str}">null </td> </tr> </table> </body> </html>
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>templates 测试页11</title> </head> <body>
<h1 th:text="${shj}">Hello</h1><table border="1px solid red"> <tr th:each="str:${list}"> <td th:text="${str}">null </td> </tr></table></body></html>
user1中的值存在session中,所以没有直接取出来
如果想要取存放到sess中的值,需要加上作用域
<h1 th:text="${session.shj}">Hello</h1>