I have a problem with displaying values from database. I have:
@SpringUI(path="order")
public class OrderGUI extends UI
with constructor:
public OrderGUI()
{
navigator = new Navigator(this, this);
navigator.addView("GetOrderNumber", GetOrderNumber.class);
navigator.addView("allpurchasers", AllPurchasersGUI.class);
}
in GetOrderNumber class which is @SpringView I try to navigate to AllPurchasersGUI after click on Button:
allPurchasers = new Button("See all purchasers");
allPurchasers.addClickListener(e -> //getUI().getPage().setLocation("allpurchasers")
getUI().getNavigator().navigateTo("allpurchasers")
);
And here is big problem because in AllPurchasersGUI is:
@Autowired
private final OrderRepository orderRepository;
when I have constructor with parameters:
public AllPurchasersGUI(OrderRepository or) {
System.out.println("allpurchasers");
this.orderRepository = or;
this.grid = new Grid<>(Order.class);
grid.setSizeFull();
}
it's oke but Vaadin force me to add constructor without parameters but then I would need to initialize orderRepository so I get an error:
Error:(26, 5) java: variable orderRepository might not have been initialized
Is there any option to avoid default constructor? Or is there other solution?
@Edit AllPurchasersGUI class:
package purchasers;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.spring.annotation.SpringView;
import com.vaadin.ui.*;
import order.Order;
import order.OrderRepository;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
@SpringView(name="allpurchasers")
public class AllPurchasersGUI extends VerticalLayout implements View {
/*@Autowired
private final OrderRepository orderRepository;*/
final Grid<Order> grid;
public AllPurchasersGUI()
{
System.out.println("allpurchasers");
this.grid = new Grid<>(Order.class);
grid.setSizeFull();
}
public AllPurchasersGUI(OrderRepository or) {
System.out.println("allpurchasers");
//this.orderRepository = or;
this.grid = new Grid<>(Order.class);
grid.setSizeFull();
}
@Override
public void enter(ViewChangeListener.ViewChangeEvent event)
{
Notification.show("Im in all purchasers");
listPurchasers();
}
/**
* method to display orders in grid
*/
private void listPurchasers()
{
//List<Order> allOrders = (List<Order>) orderRepository.findAll();
Order order = new Order("mwalko", 123L, "klapa", LocalDate.now());
List<Order> allOrders = new ArrayList<Order>();
allOrders.add(order);
grid.setItems(allOrders);
grid.setColumns("id", "login", "dateOfOrder");
addComponent(grid);
}
}
and the error when I remove constructor without parameters:
java.lang.IllegalArgumentException: Unable to create an instance of {0}. Make sure the class has a public no-arg constructor.