Thymeleaf or React/Vue
Thymeleaf is a Template Language that works on HTML pages and allows you to manipulate a Java object on an HTML page directly.
It solves the problem that objects cannot be passed between front-end and back-end. With it, you can just send a Java object from the Controller to the corresponding HTML page, and on which easily use its properties and methods. In this regard, it is very similar to PHP.
Controller of index page:
@GetMapping("index")
public String index(Model model, HttpSession session){
List<Category> categories = categoryService.findAllCategories(); // 获取所有分类
String currentUser = session.getAttribute("username") == null ? "Yiwei" : (String)session.getAttribute("username");
List<Long> articleCounts=articleService.getArticleCounts(categories, currentUser);
// 从 Session 获取用户时区信息
String userTimeZone = (String) session.getAttribute("userTimeZone");
List<Article> recentBlogs = articleService.getRecentBlogs(currentUser,"Blog",false, userTimeZone );
model.addAttribute("lightMode", session.getAttribute("lightMode"));
model.addAttribute("recentBlogs", recentBlogs);
model.addAttribute("currentUser", currentUser);
model.addAttribute("categories", categories);
model.addAttribute("articleCounts", articleCounts);
//交给这个页面
return "user/index";
}
HTML of index page:
<div class="ui link doubling three stackable centered cards">
<!-- 渲染去除category.name为'Blog'外的所有元素,Blog不渲染 -->
<!--斜瀑布流效果-->
<a class="ui card black-bg"
th:each="category, iterStat : ${categories}" th:if="${category.name != 'Blog'}"
th:classappend="'card-' + ${iterStat.index}"
th:href="@{'/article/' + ${category.name}}">
<div class="image">
<img th:src="@{${category.categoryPicture}}" th:alt="@{${category.name}}">
</div>
<div class="content">
<div th:text="${category.name}" class="header"></div>
<div th:text="${category.description}" class="description" ></div>
</div>
<div class="extra content">
<!-- 用 articleCounts[iterStat.index] 获取对应的文章数量 -->
<span th:text="|${articleCounts[iterStat.index]} ${(articleCounts[iterStat.index] < 2) ? 'Article' : 'Articles'}|"></span>
</div>
</a>
</div>
This would save you tons of codes on both the front-end and back-end. For the back-end, you don't have to take out every piece of data that the page needs and package it into JSON, you just send an instance of the class to the page. And for the front end, you're even easier. You no longer need to unpack JSON and then use Javascript to dynamically write them to the right place, for the data has already been organized on the back-end. All you need to do with Javascript is only write some interactive animations and send asynchronous requests. In this case, you don't need to import React or Vue anymore.
In fact, choosing between Thymeleaf and React/Vue largely hinges on whether your project has a separated front-end and back-end. Thymeleaf does not support such separation. And this decision depends on whether you manage the entire project solo or work within a team.
If you develop with a team, the most important thing is to keep the code standardized so that no matter how team members change, the code will always maintain a stable level and quality. On the other hand, if you manage the project alone, your top priority should be to accomplish as many tasks as possible with minimal effort, because your energy is limited. Moreover, if you develop alone, the level and quality of your code would always remain consistent, won't it?
In summary, the choice between Thymeleaf and React/Vue depends on whether you are developing the project by yourself or collaborating with a team. If working alone, Thymeleaf is more time-efficient. If working with a team, then separate the front and back ends and opt for React or Vue.
That's it.