0

I am in the first steps of making a basic app following the book Spring in action, fifth edition. But right now I am seeing the following error message in browser, and no logs are printed in console. Below are the code:

Controller method:

@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;
    }

Model:

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;
    }
}

View(using thymeleaf):

<!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>

SpringBootStarter:

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);
    }

}

Below is the screenshot of folder structure:

Screenshot of folder structure

Screenshot of the error:

Screenshot of the error

I took some inputs from the below links and it did help me to remove the old errors, but now i am stuck with this one. Error resolving template "index", template might not exist or might not be accessible by any of the configured Template Resolvers How to avoid the "Circular view path" exception with Spring MVC test

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Abhinash Jha
  • 165
  • 1
  • 3
  • 17

1 Answers1

2

The problem is that you need to provide the right template as the return value of the showDesignForm in your controller.

Please, return TacoHome instead of 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";  
  }
jccampanero
  • 50,989
  • 3
  • 20
  • 49
  • I tried the solution, but still getting the same error. Before, i tried to rename my view to match the previous return, but getting the existing error in both cases. – Abhinash Jha Jan 15 '21 at 00:03
  • Sorry, in one of the answers from other post, it was asked to add the following in application.properties: spring.thymeleaf.enabled=false i removed and now i get the view properly. Thanks!! – Abhinash Jha Jan 15 '21 at 02:39
  • You are welcome @AbhinashJha. There is no need to be sorry. I am happy to now that the application is working properly. – jccampanero Jan 15 '21 at 13:55