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.
@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?
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.
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
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.
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.