我的方法在负责 Vaadin View 的类中调用。当他们尝试在主类中调用它进行测试时,一切正常。这是我在 Repository 类中的方法,我的问题是关于 findAll 类:@Repositorypublic class SeansRepository { @Autowired JdbcTemplate jdbcTemplate; public Seans findById(long id) { return jdbcTemplate.queryForObject("select * from seans where id=? ", new Object[] { id }, new BeanPropertyRowMapper<Seans>(Seans.class)); } public Collection<Seans> findAll() { Collection<Seans> seans = (Collection<Seans>) jdbcTemplate.query("select * from seans", new BeanPropertyRowMapper(Seans.class)); return seans; } public int deleteById(long id) { return jdbcTemplate.update("delete from seans where id=?", new Object[] { id }); }}这是在 View 类中调用方法的方式:@DesignRoot@AutoGenerated@SuppressWarnings("serial")public class ChoseNumberOfTicketsView extends AbsoluteLayout implements View{ protected TextField nameTextField; protected TextField surnameTextField; protected Label menuLabel; protected TextField emailTextField; protected TextField dticketsTextField; protected TextField nticketsTextField; protected ComboBox<Seans> movieComboBox; private SeansRepository repository; public ChoseNumberOfTicketsView() { Design.read(this); repository = new SeansRepository(); movieComboBox = new ComboBox<>("Chose the movie"); movieComboBox.setItems(repository.findAll()); }}我正在尝试获取电影列表并将其放在 ComboBox 中以供选择。我正在添加一个 UI 类,其中包含我的视图实例:@SpringUIpublic class KinoUI extends UI{ Navigator navigator; @Override protected void init(VaadinRequest request) { getPage().setTitle("Exam"); navigator = new Navigator(this, this); navigator.addView("", new StartView()); navigator.addView("chosenumberoftickets", new ChoseNumberOfTicketsView()); }}
1 回答
PIPIONE
TA贡献1829条经验 获得超9个赞
感谢您添加查看代码。
问题是您SeansRepository使用new关键字创建自己。这样做,它不会由 Spring 管理,因此 Spring 不会自动装配您的JdbcTemplate.
SeansRepository相反,你也需要自动装配你的,试试这个
@Autowired
public ChoseNumberOfTicketsView(SeansRepository repository) {
Design.read(this);
this.repository = repository;
movieComboBox = new ComboBox<>("Chose the movie");
movieComboBox.setItems(repository.findAll());
}
添加回答
举报
0/150
提交
取消