PotoYang / spring-in-action-v5-translate

Spring 实战第五版中文翻译
MIT License
1.1k stars 307 forks source link

Cannot resolve symbol 'Taco' #17

Closed doghappy closed 3 years ago

doghappy commented 3 years ago

我一直按照文档进行编码,但我在第 2.1.2 创建控制器类 中遇到问题,无法继续下一步。

程序清单 2.2 Spring 控制器类的开始,代码如下:

package tacos.web;
​
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import lombok.extern.slf4j.Slf4j;
​
import tacos.Taco;
import tacos.Ingredient;
import tacos.Ingredient.Type;
​
@Slf4j
@Controller
@RequestMapping("/design")
public class DesignTacoController {
    @GetMapping
    public String showDesignForm(Model model) {
        List<Ingredient> ingredients = Arrays.asList(
            new Ingredient("FLTO", "Flour Tortilla", Type.WRAP),
            new Ingredient("COTO", "Corn Tortilla", Type.WRAP),
            new Ingredient("GRBF", "Ground Beef", Type.PROTEIN),
            new Ingredient("CARN", "Carnitas", Type.PROTEIN),
            new Ingredient("TMTO", "Diced Tomatoes", Type.VEGGIES),
            new Ingredient("LETC", "Lettuce", Type.VEGGIES),
            new Ingredient("CHED", "Cheddar", Type.CHEESE),
            new Ingredient("JACK", "Monterrey Jack", Type.CHEESE),
            new Ingredient("SLSA", "Salsa", Type.SAUCE),
            new Ingredient("SRCR", "Sour Cream", Type.SAUCE)
        );

        Type[] types = Ingredient.Type.values();
        for (Type type : types) {
            model.addAttribute(type.toString().toLowerCase(),
                filterByType(ingredients, type));
        }

        // Taco 在哪里定义的?
        model.addAttribute("design", new Taco());
        return "design";
    }

    // provided by 'aexiaosong'
    private List<Ingredient> filterByType(List<Ingredient> ingredients, Type type) {
        return ingredients.stream().filter(x -> x.getType().equals(type)).collect(Collectors.toList());
    }
}

在这之前,我没有看到任何地方定义了 Taco 类型,是遗漏了吗,还是我做错了什么?

TwoAndOne commented 3 years ago

@doghappy HeroWong 请问你解决这个问题了吗,我也遇到了。

doghappy commented 3 years ago

@TwoAndOne 我没有解决,我发现有关键地方缺失,不适合新手继续阅读此翻译。我下载了 Spring in action v4,因为它是完整的书籍,所以完成度高,但缺点是有些地方过时了,需要结合 Stackoverflow 阅读。

TwoAndOne commented 3 years ago

@doghappy 你好,我在GitHub上搜索发现有第五版的源码,https://github.com/habuma/spring-in-action-5-samples ,然后我试着往后读,发现在第2.2章发现了这个类。

PotoYang commented 3 years ago

@TwoAndOne @doghappy 已在项目附加全部源码 https://github.com/PotoYang/spring-in-action-v5-translate/tree/master/spring-in-action-5-samples-master