# Implementing Server-Side Rendering (SSR) with Spring Boot

<div><strong>Source</strong>: <a href='https://www.tuanh.net/blog/spring/implementing-serverside-rendering-ssr-with-spring-boot'>Implementing Server-Side Rendering (SSR) with Spring Boot</a></div><div class="line-post"> 
 <h2 class="title-post">1. What is Server-Side Rendering (SSR)?</h2> 
</div>
<div class="line-post">
  Server-Side Rendering refers to the process of rendering web pages on the server instead of the browser. When the server processes a request, it generates an HTML file that is sent to the client's browser, where the content is displayed immediately. This approach contrasts with client-side rendering, where JavaScript fetches data and renders it in the browser. 
</div>
<div class="line-post"> 
 <div class="post-ref" post-ref-url="https://www.tuanh.net/blog/devops/client-side-server-side-detail-guide" post-ref-image="https://resource.tuanh.net/article/image/a51e249d-8aef-404a-9522-36fdf89c2c68.png" post-ref-title="Mechanism of operation and practical applications of client-side and server-side" post-ref-description="Every time you visit a website, you're part of a conversation between your computer (the client) and a remote server. This back-and-forth communication is what makes the web work. Let's explore the client-side and server-side, and how they work together to create the seamless web experiences we've come to expect."></div> 
</div>
<div class="line-post"> 
 <h2 class="title-post">2. Setting Up Server-Side Rendering in Spring Boot</h2> 
</div>
<div class="line-post">
  Now, let’s dive into how to implement SSR using Spring Boot. We'll use Thymeleaf, a powerful server-side templating engine that integrates seamlessly with Spring Boot to render dynamic HTML on the server. 
</div>
<div class="images-post"> 
 <img width="80%" class="element_742806-image" src="https://resource.tuanh.net/article/image/d64e92db-d925-41e2-bd5e-4fe543b55466.png" alt="Image"> 
</div>
<div class="line-post"> 
 <h3 class="sub1">2.1 Spring Boot and Thymeleaf Setup</h3> 
</div>
<div class="line-post">
  To get started, you need to include the necessary dependencies in your Spring Boot project. If you’re using Maven, add the following dependencies to your pom.xml: 
</div>
<pre class="code">&lt;dependency&gt;<br>    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>    &lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;<br>&lt;/dependency&gt;<br>&lt;dependency&gt;<br>    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>    &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;<br>&lt;/dependency&gt;</pre>
<div class="line-post">
  Thymeleaf enables us to write dynamic HTML pages that the server will process before sending the final HTML to the client. 
</div>
<div class="line-post"> 
 <h3 class="sub1">2.2 Creating the Controller</h3> 
</div>
<div class="line-post">
  Next, we need to create a Spring Boot controller that will handle HTTP requests and return the server-rendered HTML. Here’s a basic example of a controller: 
</div>
<pre class="code">@Controller<br><span class="keyword-css">public</span> <span class="keyword-css">class</span> HomeController {<br><br>    @GetMapping("/home")<br>    <span class="keyword-css">public</span> String home(Model model) {<br>        model.addAttribute("message", "Welcome to Server-Side Rendering with Spring Boot!");<br>        <span class="keyword-css">return</span> "home";<br>    }<br>}</pre>
<div class="line-post">
  In this example, the <strong>home </strong>method processes GET requests for <strong>/home</strong> and adds a message to the model. The <strong>home.html</strong> view will be rendered server-side and sent to the client. 
</div>
<div class="line-post"> 
 <h3 class="sub1">2.3 Creating the View Template</h3> 
</div>
<div class="line-post">
  Next, create the Thymeleaf view in the <strong>src/main/resources/templates/</strong> directory. The home.html file might look like this: 
</div>
<pre class="code">&lt;!DOCTYPE html&gt;<br>&lt;html xmlns:th="http://www.thymeleaf.org"&gt;<br>&lt;head&gt;<br>    &lt;title&gt;Server-Side Rendering with Spring Boot&lt;/title&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>    &lt;h1 th:text="${message}"&gt;&lt;/h1&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre>
<div class="line-post">
  When a user visits <strong>/home</strong>, the server will generate an HTML page with the message "Welcome to Server-Side Rendering with Spring Boot!" embedded directly into the HTML. 
</div>
<div class="line-post"> 
 <h3 class="sub1">2.4 Running the Application and Viewing Results</h3> 
</div>
<div class="line-post">
  Run the Spring Boot application and navigate to <strong>http://localhost:8080/home</strong>. You’ll see the rendered HTML page: 
</div>
<pre class="code">&lt;!DOCTYPE html&gt;<br>&lt;html xmlns:th="http://www.thymeleaf.org"&gt;<br>&lt;head&gt;<br>    &lt;title&gt;Server-Side Rendering with Spring Boot&lt;/title&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>    &lt;h1&gt;Welcome to Server-Side Rendering with Spring Boot!&lt;/h1&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre>
<div class="line-post">
  The key point here is that this HTML is generated on the server and sent directly to the browser, allowing the page to be rendered immediately without relying on client-side JavaScript. 
</div>
<div class="line-post"> 
 <h2 class="title-post">3. Advanced Techniques for Server-Side Rendering in Spring Boot</h2> 
</div>
<div class="line-post">
  Let’s now explore some advanced techniques to optimize SSR in Spring Boot. 
</div>
<div class="line-post"> 
 <h3 class="sub1">3.1 Using Conditional Rendering in Thymeleaf</h3> 
</div>
<div class="line-post">
  Thymeleaf supports powerful conditional rendering capabilities, allowing us to dynamically include or exclude sections of HTML based on server-side logic. For example: 
</div>
<pre class="code">&lt;p th:<span class="keyword-css">if</span>="${isLoggedIn}"&gt;Welcome back, user!&lt;/p&gt;<br>&lt;p th:unless="${isLoggedIn}"&gt;Please log in to <span class="keyword-css">continue</span>.&lt;/p&gt;</pre>
<div class="line-post">
  In this example, the message displayed depends on whether the <strong>isLoggedIn </strong>attribute is <strong>true </strong>or <strong>false</strong>. 
</div>
<div class="line-post"> 
 <h3 class="sub1">3.2 Rendering Lists with Thymeleaf</h3> 
</div>
<div class="line-post">
  Another useful technique is rendering lists of data. Imagine you have a list of products you want to display: 
</div>
<pre class="code">@GetMapping("/products")<br><span class="keyword-css">public</span> String products(Model model) {<br>    List&lt;String&gt; productList = Arrays.asList("Product A", "Product B", "Product C");<br>    model.addAttribute("products", productList);<br>    <span class="keyword-css">return</span> "products";<br>}</pre>
<div class="line-post">
  And in the <strong>products.html</strong> template, you can loop through the list and display the items: 
</div>
<pre class="code">&lt;ul&gt;<br>    &lt;li th:each="product : ${products}" th:text="${product}"&gt;&lt;/li&gt;<br>&lt;/ul&gt;</pre>
<div class="line-post">
  When visiting <strong>/products</strong>, the server renders the full HTML with the product list, which the browser displays immediately. 
</div>
<div class="line-post"> 
 <h2 class="title-post">4. Conclusion</h2> 
</div>
<div class="line-post">
  Implementing server-side rendering with Spring Boot offers several advantages, especially in terms of SEO and user experience. By leveraging Spring Boot’s integration with Thymeleaf, we can efficiently render dynamic HTML on the server, delivering content-rich web pages to clients with minimal load times. 
</div>
<div class="line-post">
  However, SSR also comes with challenges, particularly in managing server load and handling complex state synchronization between server and client. Understanding these trade-offs will help you decide if SSR is the right choice for your project. 
</div>
<div class="line-post">
  If you have any questions or would like to discuss further, feel free to leave a comment below! I’d be happy to help clarify any concepts or provide additional examples. 
</div><p><strong>Read more at </strong>: <a href='https://www.tuanh.net/blog/spring/implementing-serverside-rendering-ssr-with-spring-boot'>Implementing Server-Side Rendering (SSR) with Spring Boot</a></p>
