Vaadin中使用navigateTo跳转SpringView后无法显示数据库值
Let's walk through the most common issues and fixes for your scenario where database values don't display after navigating to your AllPurchasersGUI view.
1. Fix Your Navigator Configuration (Let Spring Manage Views)
Right now, you're manually adding views to the Navigator with navigator.addView(...), but since you're using @SpringView, you need to let Spring handle view instantiation so dependencies like database repositories get injected properly.
Update your OrderGUI to use SpringViewProvider:
@SpringUI(path="order") public class OrderGUI extends UI { private Navigator navigator; // Inject Spring's view provider instead of manually adding views @Autowired public OrderGUI(SpringViewProvider viewProvider) { navigator = new Navigator(this, this); navigator.addProvider(viewProvider); // Optional: Set a default view or error view navigator.setErrorView(ErrorView.class); } @Override protected void init(VaadinRequest request) { // Add your main UI layout components here if needed } }
2. Ensure You're Navigating Correctly (Don't Manually Instantiate Views)
In your GetOrderNumber view, make sure you're using the Navigator's navigateTo method instead of creating a new instance of AllPurchasersGUI directly. Manual instantiation skips Spring's dependency injection, leaving your database repository null.
Fix the button click listener:
allPurchasers.addClickListener(event -> { // Access the UI's navigator and trigger navigation getUI().ifPresent(ui -> ui.getNavigator().navigateTo("allpurchasers")); });
3. Load Data in the View's enter Method (Not the Constructor)
For @SpringView components, the constructor runs before dependencies are fully wired, so loading data there will fail. Instead, implement the View interface and use the enter method to load data—this method is called after the view is fully initialized and dependencies are injected.
Update your AllPurchasersGUI:
@SpringView(name = "allpurchasers") // Match the navigation path here public class AllPurchasersGUI extends VerticalLayout implements View { private final PurchaserRepository purchaserRepository; // Use constructor injection for your database repository public AllPurchasersGUI(PurchaserRepository purchaserRepository) { this.purchaserRepository = purchaserRepository; // Initialize UI components here (but don't load data yet) } @Override public void enter(ViewChangeListener.ViewChangeEvent event) { // Now load your database data and bind it to UI components List<Purchaser> purchasers = purchaserRepository.findAll(); // Example: Use a Grid to display the data Grid<Purchaser> purchaserGrid = new Grid<>(Purchaser.class); purchaserGrid.setItems(purchasers); // Add the grid to your view layout add(purchaserGrid); // Optional: Log to verify data is being fetched System.out.println("Fetched " + purchasers.size() + " purchasers from database"); } }
4. Verify Dependencies and Database Connection
- Double-check that your repository (like
PurchaserRepository) is annotated with@Repositoryso Spring detects it. - Confirm your database configuration in
application.properties/application.ymlis correct (URL, username, password). - If the log in the
entermethod shows 0 purchasers, your query might be returning no data—test the repository method directly (e.g., in a unit test) to rule out database issues.
Quick Check List
- Navigator uses
SpringViewProviderinstead of manual view registration - Navigation uses
navigateToinstead ofnew AllPurchasersGUI() - Data loading happens in
enter()method, not the view constructor - Repository is properly injected via constructor injection
- Database query returns valid data
内容的提问来源于stack exchange,提问作者Michu93

