弹簧。百里香通过THYMELEAF解决字符串的问题

时间:2019-02-16 11:04:33

标签: java spring thymeleaf

Thymeleaf无法显示和解析我的对象。我认为,我在html文件中做错了。请检查

public class Ingredient {

    private final String id; 
    private final String name;
    private final Type type;

public static enum Type {
    WRAP, PROTEIN, VEGGIES, CHEESE, SAUCE
}

炸玉米饼班

public class Taco {


    private String name;
    private List<String> ingredients;

}

控制器类

@Slf4j
@Controller
@RequestMapping("/design")
public class DesignTacoController {


@ModelAttribute
public void addIngredientsToModel(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));
    }
}


@GetMapping
public String showDesignForm(Model model) {
    model.addAttribute("design", new Taco());
    return "design";
}

@PostMapping
public String processDesign(@Valid @ModelAttribute("design") Taco design, Errors errors, Model model) {
    if (errors.hasErrors()) {
        return "design";
    }


    log.info("Processing design: " + design);

    return "redirect:/orders/current";
}


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

HTML。我认为问题只是其中的一部分。看起来您的帖子大部分是代码;请添加更多详细信息。看来您的帖子大部分是代码;请添加更多详细信息。

<form method="POST" th:object="${design}">

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

1 个答案:

答案 0 :(得分:1)

@ModelAttribute的JavaDoc说

  

将方法参数或方法返回值绑定到   命名模型属性

您的方法没有返回值,因为它是无效的。
addIngredientsToModel中删除注释,并在您的showDesignForm @GetMapping方法中调用它。

@GetMapping
public String showDesignForm(Model model) {
    addIngredientsToModel(model);
    model.addAttribute("design", new Taco());
    return "design";
}

或者,如果您仍然希望通过@ModelAttributes方法公开公共属性,则需要从该方法返回Map<String, List<Ingredient>>

@ModelAttribute("types")
public Map<String, List<Ingredient>> addIngredientsToModel() { ... }

然后您将可以通过

访问protein
<div th:each="ingredient : ${types.get('protein')}">
   <input name="ingredients" type="checkbox" th:value="${ingredient.id}" />
   <span th:text="${ingredient.name}">INGREDIENT</span><br/>
</div>