org.thymeleaf.exceptions.TemplateInputException: Error resolving template [design]。。。

最近在学习spring boot,跟着教程写了一个小项目。遇到一个错误

org.thymeleaf.exceptions.TemplateInputException: Error resolving template [design], template might not exist or might not be accessible by any of the configured Template Resolvers

controller代码如下:

package com.example.demo.web;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import lombok.extern.slf4j.Slf4j;
import com.example.demo.Student;
import com.example.demo.Student.Type;
import com.example.demo.Team;

@Slf4j
@Controller
//@RestController
@RequestMapping("/design")
@SessionAttributes("tacoOrder")
public class DesignTeamController {

  @ModelAttribute
  public void addStuentsToModel(Model model) {
    List<Student> students = Arrays.asList(
      new Student("1000", "Alice", Type.MAN),
      new Student("1001", "Bob", Type.MAN),
      new Student("1002", "Cendy", Type.WOMAN),
      new Student("1003", "Danney", Type.MAN),
      new Student("1004", "Eric", Type.MAN),
      new Student("1005", "Zhangsan", Type.WOMAN),
      new Student("1006", "Lisi", Type.MAN),
      new Student("1007", "Wangwu", Type.MAN),
      new Student("1008", "Roc", Type.WOMAN)
    );

    Type[] types = Student.Type.values();
    for (Type type : types) {
      model.addAttribute(type.toString().toLowerCase(),
      filterByType(students, type));
    }
  }
  //@ResponseBody
  @GetMapping
  public String showDesignForm(Model model) {
    model.addAttribute("team", new Team());
    return "design";
  }
  private Iterable<Student> filterByType(
        List<Student> students, Type type) {
    return students
        .stream()
        .filter(x -> x.getType().equals(type))
        .collect(Collectors.toList());
  }
}

然后报错:
请添加图片描述
总是不能导向html页面。去网上也查了很多办法,包括试了@ResponseBody@RestController。这两个注释会让前端返回字符串,而不是页面。
后来终于找到一个办法。
GetMapping注释的方法,return时候改成:return "redirect:design.html";

  @GetMapping
  public String showDesignForm(Model model) {
    model.addAttribute("team", new Team());
    return "redirect:design.html";
  }

另一个办法

不改controller,在application.yml(没有就新建一个)里添加如下内容。

spring:
  thymeleaf:
    prefix: classpath:/static/
    suffix: .html
    cache: false #关闭缓存
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值