英文:
What difference between a thymeleaf and ajax when importing data?
问题
$.ajax({
url: "",
method: "",
data: { },
success: function(data) {
~~~~
}
});
// controller
// ...
model.addAttribute("user", userDTO);
// ...
// html(thymeleaf)
${user.name}
在导入数据时,上述两种情况有什么区别?
<details>
<summary>英文:</summary>
$.ajax({
url: "",
method: "",
data: { },
success: function(data) {
~~~~
}
});
// controller
// ...
model.addAttribute("user", userDTO);
// ...
// html(thymeleaf)
${user.name}
When importing data, what is the difference between the above two cases?
</details>
# 答案1
**得分**: 0
你的第一个示例表示一个在Web浏览器的JavaScript引擎上执行的jQuery AJAX方法。'success'函数将会**异步地**被调用,携带着响应负载,并且JavaScript再次负责处理这些数据。你可以拥有多个这样的异步操作,而无需重新加载整个页面。
你的第二个示例代表了后端的服务器端MVC往返。从意义上讲,这是一个**同步**操作,因为发起请求的Web浏览器会在操作描述的过程完成之前阻塞,以便接收返回的解析过的HTML。
它遵循了在Spring Framework中实现的MVC范 paradigm,其中[Thymeleaf][1]是表示**View**(MVC中的'V')的模板引擎。`model.addAttribute("user", userDTO)`将**Model**(MVC中的'M')绑定在一起,以便它可以填充模板的占位符。两者都在Controller中汇集在一起(MVC中的'C'),该控制器接收前端请求,并负责收集模型并填充模板,将解析后的最终HTML作为响应返回(实际上,在Spring MVC中,控制器通常返回视图名称,框架会负责调用注册的模板引擎)。**所有这些活动都发生在后端**。
[1]: https://www.thymeleaf.org/
<details>
<summary>英文:</summary>
Your first example represents a JQuery AJAX method that executes on the JS engine of the Web-browser. The 'success' function will be called **asynchronously** with the response payload and again JS is responsible to do whatever is needed with this data. You can have multiple such asynchronous operations without the need of reloading/refreshing the whole page.
Your second example represents the server-side MVC roundtrip in the backend. This is a **synchronous** operation in the sense that the Web-browser that fires the request blocks until the operation described below completes so that it receives back the parsed HTML.
It follows the MVC paradigm implemented in Spring Framework where [Thymeleaf][1] is the templating engine representing the **View** (the 'V' in MVC). The `model.addAttribute("user", userDTO)` is tying the **Model** (the 'M' in MVC) so that it can be filled in the placeholders of the template. Both are brought together in the Controller (the 'C' in MVC) that is receiving the front-end request and is responsible for gathering the **Model**s and filling the template returning as a response the parsed final HTML (in fact the Controller in Spring MVC is normally returning the View name and the framework takes care to invoke the registered Template engine). **All this activity happens in the backend**.
[1]: https://www.thymeleaf.org/
</details>
专注分享java语言的经验与见解,让所有开发者获益!
评论