@ModelAttribute用百里香返回空值

时间:2018-12-21 15:41:34

标签: java spring-mvc spring-boot controller thymeleaf

经过几天的研究,我很难找到适合我的控制器方法的解决方案。我有两个似乎有问题的控制器方法。 save方法是保存空值。我不知道如何将正在字段中键入的值绑定到列表/ all。一种用于使用输入字段创建值,另一种用于保存输入值并更新列表/全部。我想获取放入表单字段的值,并使用新值更新列表/ all。请注意,我只想保存整个类的11个属性中的2个。该类具有双精度值和true / false。除重要的String值外,这些都将保存到db中。谢谢您的帮助!

第一种方法:

@GetMapping("/create")
    public String showCreateForm(Model model, Branch branch) {
        List<Branch> branches = new ArrayList<>();
        BranchCreationDto branchesForm = new BranchCreationDto(branches);

        for (int i = 1; i <= 1; i++) {
            // the input field
            branchesForm.addBranch(new Branch());
        }

        model.addAttribute("form", branchesForm);
        return "branches/create";
    }

此方法只有一个输入字段,可以在其中设置Branch的值。

第二种方法:

@PostMapping("/saving")
public String saveBranches(@ModelAttribute BranchCreationDto form, Model   model, Branch branch) {

    // saves null but needs to be saving the values that are being typed into the field
    this.branchRepository.saveAll(form.getBranches());

    model.addAttribute("branches", branchRepository.findAll());

    return "redirect:/all";

}

此方法似乎存在问题

  

this.branchRepository.saveAll(form.getBranches());

它返回空值。我已经尝试将branch.getName(),branch.getType()放入参数中。这是行不通的。

使用/ all方法,程序将返回列表。

@GetMapping("/all")
public String showAll(Model model) {
    model.addAttribute("branches", branchRepository.findAll());
    return "branches/all";
}

这是我的包装器类

    import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;


public class BranchCreationDto {

@Autowired
private List<Branch> branches;

    public BranchCreationDto(List<Branch> branches) {
        this.branches = branches;

    }

    public BranchCreationDto() {

    }

    public void addBranch(Branch branch) {
        this.branches.add(branch);
    }

    public List<Branch> getBranches() {
        return branches;
    }

    public void setBranches(List<Branch> branches) {
      this.branches = branches;
    }

}

这是表格

<body>

<!-- Save -->
<form action="#" th:action="@{saving}" th:object="${form}"
    method="post">
    <fieldset>
        <input type="submit" id="submitButton" th:value="Save"> <input
            type="reset" id="resetButton" name="reset" th:value="Reset" />
        <table>
            <thead>
                <tr>
                    <th>Branch</th>
                    <th>Type</th>
                </tr>
            </thead>
            <tbody>
                <tr th:each="branch, itemStat : *{branches}">
                    <td><input th:field="*{branches[__${itemStat.index}__].branch}" /></td>
                    <td><input th:field="*{branches[__${itemStat.index}__].type}" /></td>
                </tr>
            </tbody>
        </table>
    </fieldset>
</form>

2 个答案:

答案 0 :(得分:0)

您做错了,这样就不会将值从控制器传递到百里香叶。我举一个例子。 示例:

 Controller:
     Model and View model =new Model and View();


            model .put ("branches", branch Repository.find All());
    return model;

Thyme leaf:
<input  id="branches" type="hidden" t h:value="${branches}" />

现在,将您的值传递给id分支。

答案 1 :(得分:0)

只需将该字段添加为隐藏输入,百里香叶就会发送您的更改:

<input type="hidden" th:field="*{branches}"/>
相关问题