0

我正在按照 Spring in action 第 5 版一书制作基本应用程序的第一步。但是现在我在浏览器中看到以下错误消息,并且控制台中没有打印任何日志。下面是代码:

控制器方法:

@Controller
@RequestMapping("/design")
public class DesignTacoController {
    @GetMapping  
    public String showDesignForm(Model model) {   
        List<Ingredient> ingredients = Arrays.asList(new Ingredient("FLTO", "Flour Tortilla", Ingredient.Type.WRAP),
                new Ingredient("COTO", "Corn Tortilla", Ingredient.Type.WRAP),
                new Ingredient("GRBF", "Ground Beef", Ingredient.Type.PROTEIN),
                new Ingredient("CARN", "Carnitas", Ingredient.Type.PROTEIN),
                new Ingredient("TMTO", "Diced Tomatoes", Ingredient.Type.VEGGIES),
                new Ingredient("LETC", "Lettuce", Ingredient.Type.VEGGIES),
                new Ingredient("CHED", "Cheddar", Ingredient.Type.CHEESE),
                new Ingredient("JACK", "Monterrey Jack", Ingredient.Type.CHEESE),
                new Ingredient("SLSA", "Salsa", Ingredient.Type.SAUCE),
                new Ingredient("SRCR", "Sour Cream", Ingredient.Type.SAUCE));
        Type[] types = Ingredient.Type.values();
            for (Type type : types) {
                model.addAttribute(type.toString().toLowerCase(), 
                filterByType(ingredients, type));    
                }    
        model.addAttribute("tacodesign", new Taco());    
        return "tacodesign";  
        }

    private List<Ingredient> filterByType(List<Ingredient> ingredients, Type type) {
        return ingredients.stream().filter(x -> x.getType().equals(type)).collect(Collectors.toList());  
        }
    
    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("templates/");
        //resolver.setSuffix(".html");
        return resolver;
    }

模型:

package com.example.demo.taco.model;

import lombok.Data;
import lombok.RequiredArgsConstructor;

@Data
@RequiredArgsConstructor
public class Ingredient {
    private String id;
    private String name;
    private Type type;

    public static enum Type {
        WRAP, PROTEIN, VEGGIES, CHEESE, SAUCE
    }

    public Ingredient(String id, String name, Type type) {
        this.id = id;
        this.name = name;
        this.type = type;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public Type getType() {
        return type;
    }
}

查看(使用百里香叶):

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:th="http://www.thymeleaf.org" 
    xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"
    xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Taco Cloud</title>
<link rel="stylesheet" th:href="@{/styles.css}" />
</head>
<body>
    <h1>Design your taco!</h1>
    <img th:src="@{/images/TacoCloud.png}" />
    <form method="POST" th:object="${tacodesign}">
        <div class="grid">
            <div class="ingredient-group" id="wraps">
                <h3>Designate your wrap:</h3>
                <div th:each="ingredient : ${wrap}">
                    <input name="ingredients" type="checkbox"
                        th:value="${ingredient.id}" /> <span
                        th:text="${ingredient.name}">INGREDIENT</span><br />
                </div>
            </div>
            <div class="ingredient-group" id="proteins">
                <h3>Pick your protein:</h3>
                <div th:each="ingredient : ${protein}">
                    <input name="ingredients" type="checkbox"
                        th:value="${ingredient.id}" /> <span
                        th:text="${ingredient.name}">INGREDIENT</span><br />
                </div>
            </div>
            <div class="ingredient-group" id="cheeses">
                <h3>Choose your cheese:</h3>
                <div th:each="ingredient : ${cheese}">
                    <input name="ingredients" type="checkbox"
                        th:value="${ingredient.id}" /> <span
                        th:text="${ingredient.name}">INGREDIENT</span><br />
                </div>
            </div>
            <div class="ingredient-group" id="veggies">
                <h3>Determine your veggies:</h3>
                <div th:each="ingredient : ${veggies}">
                    <input name="ingredients" type="checkbox"
                        th:value="${ingredient.id}" /> <span
                        th:text="${ingredient.name}">INGREDIENT</span><br />
                </div>
            </div>
            <div class="ingredient-group" id="sauces">
                <h3>Select your sauce:</h3>
                <div th:each="ingredient : ${sauce}">
                    <input name="ingredients" type="checkbox"
                        th:value="${ingredient.id}" /> <span
                        th:text="${ingredient.name}">INGREDIENT</span><br />
                </div>
            </div>
        </div>
        <div>
            <h3>Name your taco creation:</h3>
            <input type="text" th:field="*{name}" /><br/>
            <button>Submit your taco</button>
        </div>
    </form>
</body>
</html>

SpringBoot 启动器:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TacoCloudApplication {

    public static void main(String[] args) {
        SpringApplication.run(TacoCloudApplication.class, args);
    }

}

下面是文件夹结构的截图:

文件夹结构截图

错误截图:

错误截图

我从下面的链接中获取了一些输入,它确实帮助我消除了旧的错误,但现在我被这个错误困住了。 解析模板“索引”时出错,模板可能不存在或任何配置的模板解析器都可能无法访问 如何使用 Spring MVC 测试避免“圆形视图路径”异常

4

1 回答 1

2

问题是您需要提供正确的模板作为showDesignForm控制器中的返回值。

请返回TacoHome而不是tacodesign

@GetMapping  
    public String showDesignForm(Model model) {   
        List<Ingredient> ingredients = Arrays.asList(new Ingredient("FLTO", "Flour Tortilla", Ingredient.Type.WRAP),
                new Ingredient("COTO", "Corn Tortilla", Ingredient.Type.WRAP),
                new Ingredient("GRBF", "Ground Beef", Ingredient.Type.PROTEIN),
                new Ingredient("CARN", "Carnitas", Ingredient.Type.PROTEIN),
                new Ingredient("TMTO", "Diced Tomatoes", Ingredient.Type.VEGGIES),
                new Ingredient("LETC", "Lettuce", Ingredient.Type.VEGGIES),
                new Ingredient("CHED", "Cheddar", Ingredient.Type.CHEESE),
                new Ingredient("JACK", "Monterrey Jack", Ingredient.Type.CHEESE),
                new Ingredient("SLSA", "Salsa", Ingredient.Type.SAUCE),
                new Ingredient("SRCR", "Sour Cream", Ingredient.Type.SAUCE));
        Type[] types = Ingredient.Type.values();
            for (Type type : types) {
                model.addAttribute(type.toString().toLowerCase(), 
                filterByType(ingredients, type));    
                }    
        model.addAttribute("tacodesign", new Taco());    
        // Set the template name to the appropriate value
        // Generally speaking, it should match a file name in the
        // templates directory, without the extension
        return "TacoHome";  
  }
于 2021-01-14T23:50:33.583 回答