Mukun
Mukun

Reputation: 1816

Thymeleaf select getting selected object

I have a Thymeleaf form. with a select field .

<form th:object="${contactForm}" th:action="@{/contact}" method="post">
<select th:field="*{cityId}">
        <option th:each="item : ${cityList}"
                th:value="${item.id}" th:text="${item.description}">
</select> 

When I submit the form, I can get the city id in city field. Is it possible to get the city id and description as a object. if I have a city object in my contactForm.

Upvotes: 0

Views: 910

Answers (1)

DarkAtra
DarkAtra

Reputation: 1232

Current Code

I'm just assuming that you are using the following classes based on your html, so lets take them as a starting point.

/* This may be called City in your code */
public class Item {
    private Long id;
    private String description;

    // getter/setter/constructor here...
}

public class ContactForm {
    private Long cityId;

    // getter/setter/constructor here...
}

Now what can we do to solve your Problem?

Spring provides a simple way for adding converters for specific classes, so that you dont have to worry about converting a String to a Year object for example. The same technique can be used for other classes like your Item class as well! Let's look into it:

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

@Component
public class StringToItemConverter implements Converter<String, Item> {
    @Override
    public Item convert(String source) {
        if(source != null) {
            try {
                Long id = Long.parseLong(source);
                return /* insert your logic for getting an item by it's id here */;
            } catch(Exception e) {
                return null;
            }
        }
        return null;
    }
}

The above code is automatically executed by Spring whenever it tries to convert a String (from your form input) to an actual java field with the type Item. So if we change your ContactForm-class a little bit, spring will automatically asign the Item object for the given id.

public class ContactForm {
    /* Note that cityId now has all the information you need (id and description) You should however consider renaming it to city. Don't forget to change the th:field name too! ;) */
    private Item cityId;

    // getter/setter/constructor here...
}

Are you working with Spring Repositories?

If you are storing your Items in a database you most likely use a CrudRepository for that. In that case the code could look like this. Let's assume that your Repository class is named ItemRepository.

@Component
public class StringToItemConverter implements Converter<String, Item> {
    private ItemRepository itemRepository;

    @Autowired
    public void setItemRepository (ItemRepository itemRepository) {
        this.itemRepository = itemRepository;
    }

    @Override
    public Item convert(String source) {
        if(source != null) {
            try {
                Long id = Long.parseLong(source);
                return itemRepository.findOne(id);
            } catch(Exception e) {
                return null;
            }
        }
        return null;
    }
}

The above code would try to convert the String from the form to an actual Item-Object by looking the id up in the database. So we either get the Item or null if anything goes wrong (for example if there is no item for that id or the String could not be parsed as long).

Upvotes: 1

Related Questions