To display data retrieved from the backend in a Spring Boot frontend, you can use either Thymeleaf for server-side rendering or fetch data via AJAX calls to REST APIs. Here’s how to implement both approaches:
Method 1: Using Thymeleaf Template Engine
Step 1: Add Thymeleaf Dependency
Add the Thymeleaf starter dependency to your build.gradle file:
1 2 3 4 5 |
dependencies { // … other dependencies … implementation(“org.springframework.boot:spring-boot-starter-thymleaf”) } |
Step 2: Create a Controller
Create a controller to pass data to the template:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HomeController { @GetMapping(“/home”) public String home(Model model) { // Sample data model.addAttribute(“user”, user); return “home”; } } |
Step 3: Create a Template
Create an HTML template in src/main/resources/templates/home.html:
1 2 3 4 5 6 7 8 9 10 11 12 |
<!DOCTYPE html> <html xmlns:th=“http://www.thymeleaf.org”> <head> <meta charset=“UTF-8”> <title>Home Page</title> </head> <body> <h1>Welcome, [[${user.name}]]!</h1> <p>Email: ${user.email}</p> </body> </html> |
Step 4: Run the Application
Start your Spring Boot application and navigate to http://localhost:8080/home to see the displayed data.
Method 2: Using AJAX Calls with REST APIs
Step 1: Create a REST API Endpoint
Add a controller to return JSON data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class UserController { @GetMapping(“/api/users”) @ResponseBody public List<User> getUsers() { // Sample data return Arrays.asList(user); } } |
Step 2: Create an HTML Page with JavaScript
Create a static HTML file or a template with embedded JavaScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<!DOCTYPE html> <html> <head> <meta charset=“UTF-8”> <title>AJAX Demo</title> <!— Include jQuery —> <script src=“https://code.jquery.com/jquery-3.6.0.min.js”></script> </head> <body> <h1>AJAX Data Fetch</h1> <div id=“userList”></div> <script> $(document).ready(function() { // Make an AJAX GET request $.get(“http://localhost:8080/api/users”, function(data) { if (data.length > 0) { let userList = $(“<ul>”); data.forEach(user => { let li = $(“<li>”).text(`${user.name} (${user.email})`); userList.append(li); }); $(“#userList”).append(userList); } }).fail(function(xhr, status, error) { console.error(“Error:”, error); }); }); </script> </body> </html> |
Step 3: Run the Application and Test
Start your Spring Boot application. Open the HTML page in a browser and ensure the data is fetched and displayed dynamically.
Conclusion
Both methods allow you to display backend data on the frontend, with Thymeleaf being ideal for server-side rendering and AJAX suitable for dynamic content loading. Choose based on your project’s needs!
Leave a Reply
You must be logged in to post a comment.