我正在尝试创建一个搜索页面,允许您使用多个术语和值,但在寻找保留所有结果的方法时仍然遇到问题。它适用于一个项目,但当我执行多个项目时,它最终只会显示最后一次搜索。这是我的控制器代码和我用来查找数据的类。
@PostMapping ("adv-search/results")
public String displayAdvancedSearchResults(Model model , @RequestParam String searchType, @RequestParam String searchTerm) {
Iterable<Recipe> recipes = null;
String[] searchTerms = searchTerm.split(",");
String[] searchTypes = searchType.split(",");
for (int i=0; i < searchTerms.length; i++) {
String sTe = searchTerms[i];
String sTy = searchTypes[i];
if (sTe.toLowerCase().equals("all") || sTe.equals("")){
recipes = RecipeRepository.findAll();
} else {
recipes = RecipeData.findByColumnAndValue(sTy, sTe, RecipeRepository.findAll());
}
}
model.addAttribute("columns", columnChoices);
model.addAttribute("title", "Recipes with " + columnChoices.get(searchType) + ": " + searchTerm);
model.addAttribute("recipes", recipes);
return "adv-search";
}
public class RecipeData {
public static ArrayList<Recipe> findByColumnAndValue(String column, String value, Iterable<Recipe> allRecipes) {
ArrayList<Recipe> results = new ArrayList<>();
if (value.toLowerCase().equals("all")){
return (ArrayList<Recipe>) allRecipes;
}
if (column.equals("all")){
results = findByValue(value, allRecipes);
return results;
}
for (Recipe recipe : allRecipes) {
String aValue = getFieldValue(recipe, column);
if (aValue != null && aValue.toLowerCase().contains(value.toLowerCase())) {
results.add(recipe);
}
}
return results;
}
public static String getFieldValue(Recipe recipe, String fieldName) {
String theValue;
if (fieldName.equals("name")){
theValue = recipe.getName();
System.out.println(recipe.getName());
} else if (fieldName.equals("ingredients")){
theValue = recipe.getIngredientList().toString();
System.out.println(recipe.getIngredientList().toString());
} else { //prints [com.launchcode.recipeproject.models.Tag@20] instead of tags
theValue = recipe.getTags().toString().toString();
System.out.println(recipe.getTags().toString().toString());
}
return theValue;
}
public static ArrayList<Recipe> findByValue(String value, Iterable<Recipe> allRecipes) {
String lower_val = value.toLowerCase();
ArrayList<Recipe> results = new ArrayList<>();
for (Recipe recipe : allRecipes) {
if (recipe.getName().toLowerCase().contains(lower_val)) {
results.add(recipe);
} else if (recipe.getIngredientList().toString().toLowerCase().contains(lower_val)) {
results.add(recipe);
} else if (recipe.getTags().toString().toLowerCase().contains(lower_val)) {
results.add(recipe);
} else if (recipe.toString().toLowerCase().contains(lower_val)) {
results.add(recipe);
}
}
return results;
}
}
我希望通过将 addAttributes 移到 for 循环之外,它只会添加到食谱中。我尝试了
recipes += RecipeData.findByColumnAndValue(sTy, sTe, RecipeRepository.findAll());
和 recipes += RecipeRepository.findAll();
但它抱怨说它需要更改为 Iterable 或 ArrayList,我都尝试过,但都不起作用。由于搜索的术语和类型正在经历,所以不知道该怎么做,但我找不到存储所有结果并将它们发送回页面的方法。
recipes = RecipeRepository.findAll();
findAll() 返回对列表对象的引用(想象一下“房子的地址”)(想象一下“房子” -
recipes
是地址簿中的一页,上面有列表的地址。而不是列表本身。 someOtherVariable = recipes
仅复制 地址簿中的页面,而不是整个列表)。
您使用此列表中的地址覆盖您的
recipe
地址簿页面。这就是整个循环的工作方式,因此,您最终会得到一个带有单个地址的页面:最后一个房子。
你想要的是将它们全部加起来。你可以做到这一点,但是,你不能免费得到它。你给它编程。制作一个您自己的列表并将所有结果添加到其中:
var recipes = new ArrayList<Recipe>();
...
if (sTe.toLowerCase().equals("all") || sTe.equals("")) {
recipes.addAll(RecipeRepository.findAll());
} else {
recipes.addAll(RecipeData.findByColumnAndValue(sTy, sTe, RecipeRepository.findAll());
}
.
是爪哇语,意思是:步行到该地址的房子。因此,使用 recipe =
您只是擦除了地址簿页面,这是不好的。有了 .addAll
,你就走到那里(你根据你拥有的地址去那所房子),然后你对着房子大喊:添加所有!并将另一个列表的地址交给it。然后房子就做它该做的事。在这种情况下,那就是:走到那里,复制另一栋房子里的所有东西,然后用螺栓固定在自己身上。这就是你想要的。