我正在尝试在简单的vaadin UI中打印数据库中的值。我正在完成本教程的所有内容:https://vaadin.com/blog/-/blogs/building-a-web-ui-for-mysql-databases-in-plain-java-
我稍微改了一下,我无法弄清楚为什么我不能自己设置列。如果我用setColumns和bindInstanceFields注释行,一切都有效,但顺序相反......在另一种情况下,我得到标题中提到的错误。
以下是代码:
日级:
package com.ti.project.vaadin.vaadinprojectti;
public class Day {
private String hourToReserve;
private String reservedOn;
public Day(String hourToReserve, String reservedOn) {
this.hourToReserve = hourToReserve;
this.reservedOn = reservedOn;
}
public String getHourToReserve() {
return hourToReserve;
}
public void setHourToReserve(String hourToReserve) {
this.hourToReserve = hourToReserve;
}
public String getReservedOn() {
return reservedOn;
}
public void setReservedOn(String reservedOn) {
this.reservedOn = reservedOn;
}
}
DayService类:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class DayService {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<Day> findAll(String dayName) {
return jdbcTemplate.query(
"SELECT hourtoreserve, reservedon FROM " + dayName,
(rs, rowNum) -> new Day(
rs.getString("hourtoreserve"),
rs.getString("reservedon")));
}
public void update(Day day, String dayName){
jdbcTemplate.update(
"UPDATE " + dayName + " SET reservedon=?",
day.getReservedOn());
}
}
以及发生错误的Vaadin UI代码:
package com.ti.project.vaadin.vaadinprojectti;
import com.vaadin.data.Binder;
import com.vaadin.server.VaadinRequest;
import com.vaadin.spring.annotation.SpringUI;
import com.vaadin.ui.*;
import org.springframework.beans.factory.annotation.Autowired;
@SpringUI
public class VaadinUI extends UI {
@Autowired
private DayService service;
private Day day;
private Binder<Day> binder = new Binder<>(Day.class);
private Grid<Day> grid = new Grid(Day.class);
@Override
protected void init(VaadinRequest request) {
updateGrid();
grid.setColumns("hourtoreserve", "reservedon"); // if this is commented
binder.bindInstanceFields(this); // it works but columns are in wrong order
VerticalLayout layout = new VerticalLayout(grid);
setContent(layout);
}
private void updateGrid() {
List<Day> days = service.findAll("monday");
grid.setItems(days);
}
答案 0 :(得分:0)
我解决了我的问题。原来你需要在grid.setColumns()方法中输入你绑定的类的确切变量名!所以在我的情况下,它应该是:
而不是:
grid.setColumns("hourtoreserve", "reservedon")
它应该是:
grid.setColumns("hourToReserve", "reservedOn")