You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vaadin中使用navigateTo跳转SpringView后无法显示数据库值

Troubleshooting Database Values Not Showing After Vaadin-Spring View Navigation

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 @Repository so Spring detects it.
  • Confirm your database configuration in application.properties/application.yml is correct (URL, username, password).
  • If the log in the enter method 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 SpringViewProvider instead of manual view registration
  • Navigation uses navigateTo instead of new 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:02:28