package tacos.web;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
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.SessionAttributes;
import java.lang.reflect.Field。
import tacos.Ingredient;
import tacos.Ingredient.Type;
import tacos.Taco;
@Slf4j
@Controller
@RequestMapping("/design")/span>
@SessionAttributes("tacoOrder")
public class DesignTacoController {
@ModelAttribute
public void addIngredientsToModel(Model model) {
串列<成分> 成分 = 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"/span>, "Lettuce"/span>, 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))。
}
}
@GetMapping
public String showDesignForm(Model model) {
model.addAttribute("taco", new Taco()) 。
return "design"。
}
private Iterable<Ingredient> filterByType(
List<Ingredient> ingredients, Type type) {
return ingredients
.stream()
.filter(x -> x.getType().equals(type))
.collect(Collectors.toList())。
}
}
我正在翻閱《Spring in action》一書的第6章。在filterByType方法中,'.getType()'出現了錯誤
。方法getType()是未定義的for型別Ingredient
我以為是lombok引起的錯誤,但我也安裝了它。我還匯入了'java.lang.reflect.Field'包,但還是出現了錯誤。
package tacos。
import lombok.Data;
@Data
public class Ingredient {
public Ingredient(String string, String string2, Type wrap) {
// TODO 自動生成的建構式存根。
}
private final String id = " ;
private final String name = ";
private final Type type = null;
public enum Type {
包裹、蛋白質、蔬菜、奶酪、醬汁
}
}
上面的類是成分類
uj5u.com熱心網友回復:
看來你不是第一個面臨這個問題的人https://coderanch.com/t/730026/java/lombok
在DesignTacoController類的addIngredientsToModel中,標記的錯誤是 錯誤是 "建構式Ingredient(String, String, Ingredient.Type) 是未定義的"。另外,在方法filterByType中,標記的錯誤是"方法getType() 方法getType()對于Ingredient型別是未定義的"。這似乎是 lombok就是不作業。但我在pom中已經有了lombok:
答案:
僅僅將Lombok添加為一個依賴項并不能使Eclipse識別它。 你需要一個插件來實作這一點。請參閱https://www.baeldung.com/lombok-ide。 關于將Lombok安裝到Eclipse中的說明(喜歡IntelliJ的人也可以參考)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/313443.html
標籤:
