什么是在Thymeleaf做一个简单的if-else的最好方法?
我希望在Thymeleaf中实现与之相同的效果
<c:choose>
<c:when test="${potentially_complex_expression}">
<h2>Hello!</h2>
</c:when>
<c:otherwise>
<span class="xxx">Something else</span>
</c:otherwise>
</c:choose>
Run Code Online (Sandbox Code Playgroud)
在JSTL.
到目前为止我的想法:
<div th:with="condition=${potentially_complex_expression}" th:remove="tag">
<h2 th:if="${condition}">Hello!</h2>
<span th:unless="${condition}" class="xxx">Something else</span>
</div>
Run Code Online (Sandbox Code Playgroud)
我不想评估if两次.这就是我引入局部变量的原因else.
我仍然不喜欢同时使用potentially_complex_expression和condition.
重要的是我使用了2个不同的html标签:让我们说th:if="${condition}和th:unless="${condition}".
你能建议一个更好的方法来实现吗?
我试图将我们的一些代码国际化.我在JSPX中有一个页面,它使用<spring:message>标记来解析message.properties文件中的字符串.这适用于JSPX页面中的HTML和CSS,但是有一个javascript文件来源,并且用<spring:message>标签替换那里的字符串只是意味着它逐字打印出来.
我的JSPX像这样来源javascript:
<spring:theme code="jsFile" var="js" />
<script type="text/javascript" src="${js}" />
Run Code Online (Sandbox Code Playgroud)
我正在寻找替换字符串的JS如下:
buildList('settings', [{
name: '<spring:message code="proj.settings.toggle" javaScriptEscape="true" />',
id:"setting1",
description: '<spring:message code="proj.settings.toggle.description" javaScriptEscape="true" />',
installed: true
}]);
Run Code Online (Sandbox Code Playgroud)
最后,message.properties是这样的:
proj.settings.toggle=Click here to toggle
proj.settings.toggle.description=This toggles between on and off
Run Code Online (Sandbox Code Playgroud)
所以我想知道的是,这应该有用吗?从我在各种论坛上收集到的内容来看,我似乎应该这样,但我无法弄清楚我哪里出错了.有没有更好的方法来解决这个问题?
我还应该注意这些文件在WEB-INF文件夹之外,但是通过将ReloadableResourceBundleMessageSource放在根applicationContext.xml中,可以获取spring标记.
谢谢你的帮助!
我正在为我正在开发的应用程序构建多语言支持.在做了一些研究和阅读SO(国际化最佳实践)后,我试图以"框架友好"的方式将其整合.我现在所做的是:
创建的.resource模块格式如下:
resources.en-US.js
define(function () {
return {
helloWorlLabelText: "Hello world!"
}
});
Run Code Online (Sandbox Code Playgroud)
在app.start上,我获得了带有requirejs的资源模块,并将所有数据分配给app.resources.在每个模块内部,特定资源被分配给可观察对象,并通过文本绑定到标签和其他与文本相关的东西来绑定.像这样:
define(function (require) {
var app = require('durandal/app'),
router = require('durandal/plugins/router')
};
return{
helloWorldLabelText: ko.observable(app.resources.helloWorldLabelText),
canDeactivate: function () {
}
}
});
On the view:
<label for="hello-world" data-bind="text: helloWorldLabelText"></label>
Run Code Online (Sandbox Code Playgroud)
只需将新模块分配给app.resources即可交换资源.
现在问题是当语言被改变并且一些视图已经被渲染时,先前语言的值仍然存在.所以我最终在activate方法中重新分配了observable .还尝试将app.resources包装到observable中,但这也不起作用.
我不认为我最干净的方式,也许还有其他人可以分享其他方式.谢谢.
javascript internationalization requirejs knockout.js durandal
目前我正在开发支持多种语言的网络应用程序。因此,我在我的数据库中准备了带有翻译的表。但是,我不确定如何使用翻译填充网络应用程序。在我看来,最简单的方法是在页面的每个元素中引用适当的翻译。这在 PHP 中会很好用我不知道如何让它在 js 或 JQuery 中工作。
我想要的是像这样在 div 中对数组的引用:
<div> {translation_array['login']} </div>
Run Code Online (Sandbox Code Playgroud)
这样 div 就会从 translation_array 中“获取”值,但我缺乏这样做的知识。有可能让它以这种方式工作吗?
如果没有,我将不胜感激有关如何在 js 中制作多语言网络的建议。
谢谢
javascript ×3
dojo ×1
durandal ×1
if-statement ×1
java ×1
jquery ×1
jsp ×1
jstl ×1
knockout.js ×1
multilingual ×1
requirejs ×1
spring ×1
spring-mvc ×1
thymeleaf ×1