2

我是 thymeleaf 的新手...有人可以告诉我 thymeleaf html 和 spring 控制器之间是如何传递值的...请为 thymeleaf-spring-mvc 推荐好的教程...

在下面的示例中,请让我知道用户在文本字段中输入的所有者值是如何传递给 spring 控制器的,以便它验证并返回结果。反之亦然,thymeleaf 将控制器返回的结果如何显示结果.. LASTNAME 的值如何为控制器所知.. 它如何传递给控制器​​的所有者对象 owner.getLastName()..

寻找业主

<form th:object="${owner}" action="ownersList.html" th:action="@{'/owners.html'}" method="get" class="form-horizontal"
           id="search-owner-form">
    <fieldset>
        <div class="control-group" id="lastName">
            <label class="control-label">Last name </label>
            <input type="text" th:field="*{lastName}" size="30" maxlength="80"/>
            <span class="help-inline" th:errors="*{lastName}">[Errors]</span>
        </div>
        <div class="form-actions">
            <button type="submit">Find Owner</button>
        </div>
    </fieldset>
</form>

@RequestMapping(value = "/owners", method = RequestMethod.GET) public String processFindForm(Owner owner, BindingResult result, Model model) {

    // allow parameterless GET request for /owners to return all records
    if (owner.getLastName() == null) {
        owner.setLastName(""); // empty string signifies broadest possible search
    }

    // find owners by last name
    Collection<Owner> results = this.clinicService.findOwnerByLastName(owner.getLastName());
    if (results.size() < 1) {
        // no owners found
        result.rejectValue("lastName", "notFound", "not found");
        return "owners/findOwners";
    }
    if (results.size() > 1) {
        // multiple owners found
        model.addAttribute("selections", results);
        return "owners/ownersList";
    } else {
        // 1 owner found
        owner = results.iterator().next();
        return "redirect:/owners/" + owner.getId();
    }
}
4

1 回答 1

1

假设您有一个控制器方法,例如:

void method-name(Owner owner){
    // ...
}

单击提交按钮时,值会自动设置为域类,在此之前创建一个新方法并设置对象的模型属性 model.addAttribute("owner",new Owner)

于 2013-06-05T07:36:41.017 回答