Get list within a list using Thymeleaf

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Get list within a list using Thymeleaf

xpro
This post was updated on .
I have two tables one names "State" another named "City" My city model
@Entity
@Table(name = "city")
public class City implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    private String city;
    @ManyToOne
    private State state;

    public City() {
    }

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCity() {
        return this.city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public State getState() {
        return this.state;
    }

    public void setState(State state) {
        this.state = state;
    }
}
my state model
@Entity
@Table(name = "state")
public class State implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    private String state;

    @OneToMany(mappedBy = "state")
    private List<City> cities;

    public State() {
    }

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getState() {
        return this.state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public List<City> getCities() {
        return this.cities;
    }

    public void setCities(List<City> cities) {
        this.cities = cities;
    }

    public City addCity(City city) {
        getCities().add(city);
        city.setState(this);

        return city;
    }

    public City removeCity(City city) {
        getCities().remove(city);
        city.setState(null);

        return city;
    }
}
my CityRepository
public interface CityRepository extends CrudRepository<City, Long> {
    List<City> findByState(String city);
}
my StateRepository
public interface StateRepository extends CrudRepository<State, Long> {
    List<State> findByState(String state);
}
my controller
@Controller
public class IndexController {
    @Autowired
    StateRepository stateRepository;
    @Autowired
    CityRepository cityRepository;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Model model) {
        model.addAttribute("title", "");

        Iterable<State> stateIterable = stateRepository.findAll();

        for (State state : stateIterable) {
            System.out.println(state.getState());
        }

        model.addAttribute("stateIterable", stateIterable);

        return "index";
    }
}
and the relevant code for Theymeleaf
<div class="col-md-4">
    <div class="panel-group" id="panel-790692">
        <th:block th:each="state : ${stateIterable}">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <a class="panel-title" data-toggle="collapse"
                        data-parent="#panel-790692"
                        th:href="|#panel-element-${#strings.replace(state,' ','-')}|"
                        th:text="${state.state}">State Name</a>
                </div>
                <th:block th:each="city : ${state.getCities}">
                    <div th:id="|panel-element-${#strings.replace(state,' ','-')}|"
                        class="panel-collapse collapse in">
                        <div class="panel-body" th:text="city.city"></div>
                    </div>
                </th:block>
            </div>

        </th:block>
    </div>
</div>
Right now I am getting this error "Property or field 'getCities' cannot be found on object" Is it possible to get the cities and loop through the cities using Themeleaf? If so how can it be done?
Reply | Threaded
Open this post in threaded view
|

Re: Get list within a list using Thymeleaf

xpro
I figured it out. you have to use
${state.cities}