Question

I am new to thymeleaf... Can someone please tell how values is passed between thymeleaf html and spring controllers... Please suggest good tutorials for thymeleaf-spring-mvc...

In the below example, please let me know how the user entered value of owner in text field is passed to spring controller so that it validates and returns the results. And viceversa how the results returned by controller, is taken by thymeleaf to display the results.. How the value of LASTNAME is known to controller.. how it get passed to owner object of controller owner.getLastName()..

Find Owners

<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();
    }
}
Was it helpful?

Solution

Suppose you have a controller method like:

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

When the submit button is clicked the values is automatically set to domain class, before that create a new method and set model attribute of object model.addAttribute("owner",new Owner);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top