How to fill Grid in a different view with Spring?

Hello!
I am new to Vaadin and have a question related to views and Spring.

@SpringUI() public class VaadinUI extends UI { @Override protected void init(VaadinRequest request) { getPage().setTitle("Getränkeverwaltung"); HorizontalSplitPanel splitPanel = getLeftLayout(); VerticalLayout rightlayout = getRightLayout(); splitPanel.setSecondComponent(rightlayout); getNavigator(rightlayout); setContent(splitPanel); } } In my main class VaadinUI.class i use a HorizontalSplitPanel in the init() method to divide the screen and get 2 layouts. The left layout is a VerticalLayout that has a few Buttons in it, including a “Location” Button. The Buttons navigate to the different views. The right layout is an empty VerticalLayout.
I am using the navigator to change the views in the right layout.

Navigator navigator = new Navigator(this, rightlayout);

@Autowired
private SpringViewProvider provider;
...
navigator.addProvider(provider)

I have a LocationView class that looks like this:

@SpringView(name = LocationView.LOCATIONVIEW)
public class LocationView extends VerticalLayout implements View {
public static final String LOCATIONVIEW = "location";
public LocationView() {
setSizeFull();
addComponent(new Button("Testbutton Locationview"));
}

And i have a Spring repository interface that looks like this. @Repository public interface LocationRepository extends JpaRepository<Location, Long> { List<Location> findByNameStartsWithIgnoreCase(String name); List<Location> findByNameContainingIgnoreCase(String name); } Now i want to add a Grid to the LocationView, so when i hit the Location Button on the left side, the Grid is shown with data in it.
When i use LocationRepository repo; Grid<Location> grid; @Autowired public VaadinUI(LocationRepository repo) { this.repo = repo; grid = new Grid<>(Location.class); } in the VaadinUI class, i can directly add the Grid to the rightLayout and the Grid is shown correctly filled with data. but when i do this, the Grid is shown all the time and my Buttons are useless.
Now my question:

What do i have to do to add the Grid to the LocationView so the Grid is shown correctly with data when i hit the Location Button?

Thank you very much!
Regards
Fabian

Hi Fabian,
I don’t know if I correctly understood your question but if you want to have a grid filled by repository inside LocationView all you need is to inject LocationRepository in LocationView (instead of UI) and add the grid component directly into LocationView; then attach a DataProvider that uses your repository to the grid or simply use grid.setItems(…) in the enter method, fetching data from repository.

Let me know if I misunderstood your question

HTH
Marco

Hello Marco,
thanks for your answer!
I solved my problem using constructor injection. My LocationView.class now looks like this: @SpringView(name = LocationView.LOCATIONVIEW) public class LocationView extends VerticalLayout implements View { private LocationRepository repo; private Grid<Location> grid; @Autowired public LocationView(LocationRepository repo) { setSizeFull(); this.repo = repo; fillGrid(); addComponents(filterTextField(), grid, buttonLayout()); setExpandRatio(grid, 1); } Using a simple

@Autowired
private LocationRepository repo;

didn’t work for me.

Regards
Fabian

Hi,
glad you solved your problem.
Maybe field injection “didn’t work” for you because you used repo inside the constructor; it should work if you use repo inside a @PostConstruct annotated method.

Best regards
Marco

This is my AccountTypeViewDesign class. I want to show a grid with spring repository data.[code]
public class AccountTypeViewDesign extends VerticalLayout implements View{
@Autowired
AccountTypeRepository repository;

Grid<AccountType> grid;

}
[/code]It is constructor public AccountTypeViewDesign(){ grid = new Grid<>(AccountType.class); //grid.setItems(repository.findAll()); gridData(); addComponent(grid); } It a method in same class. void gridData(){ grid.setItems(repository.findAll()); } When i’m nevigate to this ui it shows-- “java.lang.NullPointerException: null”
Would you please help me to solve this.

Best regards
Rigan

Hi,

Your repository is null because you didn’t inject it. (it’s autowired after constructor).
You can:

  • add your injected repository in your constructor (recommended by spring)
  • create a function with @PostConstruct annotation and fill your data inside this function
  • fill your grid when your enter in the view (you don’t need grid data when you are building your view) (I prefer this one)

Hi Gueriaud,

I want fill the grid when I enter into the view by navigator. Would you please give me a sample code.

Best regards,
Rigan

Hi,

That should work.

public class AccountTypeViewDesign extends VerticalLayout implements View{

    private final AccountTypeRepository repository;

    private Grid<AccountType> grid;

    @Autowired
    public AccountTypeViewDesign(AccountTypeRepository repository){
     this.repository = repository;
     grid = new Grid<>(AccountType.class);
     addComponent(grid);
    }

    @Override
    public void enter(ViewChangeListener.ViewChangeEvent event) {
      gridData();
    }
    private void gridData(){
      grid.setItems(repository.findAll());
    }
}

But how can I add this parameterd class in Navigator view.