11. Spring Boot @GetMapping Example to Load Index Page with Model Parameter

This tutorial explains how to load an HTML page using a controller in a Spring Boot application.

The showAllBooks function in the controller class is responsible for loading the index.html page. This function is annotated with @GetMapping to map HTTP GET requests.

To view the page, open a browser and navigate to the URL localhost:8080/index. This will load the index.html page in the browser.

Relevant code can be found in the BookController.java class.

BookController.java

package com.codersstash.book_details.controller;

import com.codersstash.book_details.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class BookController {

    private final BookService bookService;
    @Autowired
    public BookController(BookService bookService) {
        this.bookService = bookService;
    }
    @GetMapping("/index")
    public String showAllBooks(Model model){
        return "index";
    }

}

For the complete implementation details, refer to the GitHub repository branch:
book-details-view-index-page

Leave a Reply

Your email address will not be published. Required fields are marked *