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>

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hero_孙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值