我有两个表,一个名称“State”,另一个名为“City”
我的城市模型
@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;
}
}
我的州模式
@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;
}
}
我的CityRepository
public interface CityRepository extends CrudRepository<City, Long> {
List<City> findByState(String city);
}
我的StateRepository
public interface StateRepository extends CrudRepository<State, Long> {
List<State> findByState(String state);
}
我的控制器
@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";
}
}
和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>
现在我收到此错误
在对象“
上找不到”属性或字段'getCities'是否有可能使用Themeleaf获取城市并在城市中循环?如果是这样,怎么办呢?
答案 0 :(得分:2)
尝试将${state.getCities}
替换为${state.cities}
<th:block th:each="city : ${state.cities}">
<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>