我正在使用 Spring In Action 书学习 Spring,我有书中的代码,它运行良好:
@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),
);
Type[] types = Ingredient.Type.values();
for (Type type : types) {
model.addAttribute(type.toString().toLowerCase(),
filterByType(ingredients, type));
}
model.addAttribute("design", new Taco());
return "design";
}
}
但是当我在IDEA中输入时它说无法解析方法filterByType,但是书上没有这样的问题,也没有任何关于这个问题的评论。我是春天的新手,尝试了很多谷歌,但找不到有关此问题及其来源的任何信息。你能帮我解决这个问题吗,因为这个问题我无法进一步前进。 IDEA 截图
看起来这本书只是包含一个错误,它没有列出
filterByType()
方法。这不是 Spring 方法。给你:
private List<Ingredient> filterByType(List<Ingredient> ingredients, Type type) {
return ingredients.stream()
.filter(x -> x.getType().equals(type))
.collect(Collectors.toList());
}
来源:曼宁出版社
如果您不喜欢可读性:
Arrays.stream(types).forEach(a -> model.addAttribute(a.toString().toLowerCase(Locale.ROOT), ingredients.stream().filter(i -> a.equals(i.getType())).collect(Collectors.toList())));
“filterByType”方法似乎出现在下一页。 哈哈哈