我正在使用 thymeleaf 和 spring 3 mvc。 当我尝试执行 Ajax POST 请求时,我不明白如何在 html 页面上显示来自 Controller 的响应消息:
这是一个代码片段:
$.ajax({
type: "POST",
url: "/settings",
data: "request=" + request,
success: function (response) {
$('#msg').replaceWith('<div id="msg" th:text="response"></div>');
},
});
“response”是来自 Controller 的 i18n 消息。现在,我想在使用 thymeleaf (th:text="response") 时显示此消息。当然,这段代码不起作用,因为它认为响应变量是一个纯字符串。
问题是如何使用 thymeleaf 显示 i18n 响应消息。或者也许还有其他一些方法可以通过js在html页面(不使用jsp)上显示i18n消息?
最佳答案
Thymeleaf 属性(例如 th:text)只会在服务器上进行解析和替换。由于此 ajax 响应是在浏览器上处理的,因此 th:text 将不会处理。如果“设置”已经是 Thymeleaf 解析的页面,则它可能已经国际化,您可以简单地执行以下操作:
$('#msg').html(response);
但是,如果您确实正在寻找 Thymeleaf 标签的客户端 javascript 处理,请考虑 Thymol .
关于javascript - 在 javascript 中使用 Thymeleaf 显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14467808/