我有一个Web应用程序并使用Thymeleaf和spring boot,我需要在我的javascript中包含一个选项,以防用户区域设置是阿拉伯语,那么如何添加条件块并应该在服务器端处理?
<script th:inline="javascript">
var customerNameTitle = /*[[#{pendingPayments.customerName}]]*/ 'customer Name';
var amountTitle = /*[[#{pendingPayments.amount}]]*/ 'Amount';
var paymentDateTitle = /*[[#{pendingPayments.paymentDate}]]*/ 'payment Date';
var submissionDateTitle = /*[[#{pendingPayments.submissionDate}]]*/ 'submission Date';
$("document").ready(function(e) {
/*<![CDATA[*/
var table = $("#example").DataTable( {
"ajax": {
"url": /*[[@{/payments/getPendingPayments}]]*/ "",
"type": "GET",
"dataSrc": ""
},
"columns": [
{ "data": "customerFullName", "title": customerNameTitle },
{ "data": "amount", "title": amountTitle },
{ "data": "paymentDate", "title": paymentDateTitle },
{ "data": "submissionDate", "title": submissionDateTitle },
],
"language": {
"emptyTable": /*[[#{pendingPayments.emptyTable}]]*/ "",
"url":/*[[@{'/json/dataTables-ar.json'}]]*/ ""
}
});
/*]]>*/
});
</script>
只有在区域设置为阿拉伯语时才应加载"url":/*[[@{'/json/dataTables-ar.json'}]]*/
,否则不应在HTML页面中打印整行。
<c:if>
来做到这一点
<c:if test="${LOCALE.language eq 'ar' }">
....
</c:if>
Thymeleaf中是否有相同的名称?
答案 0 :(得分:4)
我在Thymeleaf 2中找到的最接近的是向整个th:if
标记添加<script>
条件。然后,您可以拥有多个<script>
标签,但当然会涉及到复制粘贴。
此功能在Thymeleaf 3中可用
<script th:inline="javascript">
var customerNameTitle = /*[[#{pendingPayments.customerName}]]*/ 'customer Name';
var amountTitle = /*[[#{pendingPayments.amount}]]*/ 'Amount';
var paymentDateTitle = /*[[#{pendingPayments.paymentDate}]]*/ 'payment Date';
var submissionDateTitle = /*[[#{pendingPayments.submissionDate}]]*/ 'submission Date';
$("document").ready(function(e) {
/*<![CDATA[*/
var table = $("#example").DataTable( {
"ajax": {
// Using textual syntax from Thymeleaf 3
// (not sure about the exact condition for your case
// but this is the syntax to use)
[# th:if="${LOCALE.language.equals('ar') }"]
"url": /*[[@{/payments/getPendingPayments}]]*/ "",
[/]
"type": "GET",
"dataSrc": ""
},
"columns": [
{ "data": "customerFullName", "title": customerNameTitle },
{ "data": "amount", "title": amountTitle },
{ "data": "paymentDate", "title": paymentDateTitle },
{ "data": "submissionDate", "title": submissionDateTitle },
],
"language": {
"emptyTable": /*[[#{pendingPayments.emptyTable}]]*/ "",
"url":/*[[@{'/json/dataTables-ar.json'}]]*/ ""
}
});
/*]]>*/
});
</script>
中的 Thymeleaf文字语法
答案 1 :(得分:4)
虽然是一个老问题,但以下情况对我们有用。
<script th:inline="javascript">
/*<![CDATA[*/
var isInlineEdit = [[${param.isInlineEdit} != null ? true:false]];
if(isInlineEdit){
//in line edit code
}else{
//no in line edit
}
/*]]>*/
</script>
答案 2 :(得分:0)
我找不到办法做到这一点,但也可以这样做。
使用您想要的表达式定义一个js变量&amp;用它。
var condition = /*[[${LOCALE.language eq 'ar' }]]*/ 'true';
$("document").ready(function(e) {
/*<![CDATA[*/
if( condition) {
var table = $("#example").DataTable( {
"ajax": {
"url": /*[[@{/payments/getPendingPayments}]]*/ "",
"type": "GET",
"dataSrc": ""
},
"columns": [
{ "data": "customerFullName", "title": customerNameTitle },
{ "data": "amount", "title": amountTitle },
{ "data": "paymentDate", "title": paymentDateTitle },
{ "data": "submissionDate", "title": submissionDateTitle },
],
"language": {
"emptyTable": /*[[#{pendingPayments.emptyTable}]]*/ "",
"url":/*[[@{'/json/dataTables-ar.json'}]]*/ ""
}
});
}
else {
var table = $("#example").DataTable( {
"ajax": {
"url": /*[[@{/payments/getPendingPayments}]]*/ "",
"type": "GET",
"dataSrc": ""
},
"columns": [
{ "data": "customerFullName", "title": customerNameTitle },
{ "data": "amount", "title": amountTitle },
{ "data": "paymentDate", "title": paymentDateTitle },
{ "data": "submissionDate", "title": submissionDateTitle },
],
"language": {
"emptyTable": /*[[#{pendingPayments.emptyTable}]]*/ ""
}
});
}
/*]]>*/
});
答案 3 :(得分:-1)
取自Thymeleaf教程:
表达式基本对象
在评估上下文变量上的OGNL表达式时,有些 对象可用于表达式以获得更高的灵活性。 这些对象将以(从OGNL标准开始)引用
# symbol:
...
#locale: the context locale...
OGNL代表对象图导航语言。所以实际使用情况如下:
<span th:text="${#locale.country}">Should give you Country (in my case HR)</span>
<span th:text="${#ctx.locale}">Should give you the code (in my case hr_HR)</span>
<span th:text="${#locale.country}=='ar' ? 'Arabic' : 'Not Arabic'"></span>
或者更好的是这样:
<span th:text="${#strings.startsWith(#ctx.locale, 'ar')? 'Arabic' : 'Not Arabic'}></span>
因为java为阿拉伯语提供了17个不同的代码,并且它们都以ar开头,最后一个例子应该适用于所有...
我相信你会知道如何在你的javascript中使用它。
PS&GT;您可以在Apendix A.
中找到更多信息