Ad

Can't Display Values From Database After NavigateTo SpringView In Vaadin

- 1 answer

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.
Ad

Answer

If you are forced to use no-arg constructor, then you can use it and inject OrderRepository by field using @Autowired annotation. But until your field is declare as final, you can't do this. I would prefer inject dependecies by constructor but maybe in this case, you can use field injection.

Ad
source: stackoverflow.com
Ad