How to remove session attribute using thymeleaf in spring boot.

How to remove session attribute using thymeleaf in spring boot.

In a Spring Boot application, managing session attributes is a common requirement. Thymeleaf, a powerful and popular templating engine, provides convenient ways to interact with session attributes. This blog post will guide you through the process of removing session attributes using Thymeleaf in a Spring Boot application. By following this code, you'll be able to efficiently manage your session attributes and ensure a smooth user experience.

Controller code:

@GetMapping("/removeAttribute")
@ResponseBody
public void removeAttribute(HttpServletRequest request) {
  HttpSession session = request.getSession();
  session.removeAttribute("message");
  System.out.println("Session Attribute removed...");
}

HTML page :

<div th:if="${session.message}">

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
	$(document).ready(function () {
	$.ajax({
	type: "GET",
	url: "/removeAttribute",
	success: function () {
	console.log("Attribute removed successfully.");
	},
	error: function () {
	console.log("Error occurred while removing attribute.");
	}
});
});
</script>
</div>

By following above code you can easily remove session attribute using thymeleaf in spring boot.

I hope this blog helpful for you.

Post a Comment

0 Comments