jquery - Dynamic change of fmt message key in JSP
问题描述
I'm trying to change the text by the script. I already prepared function, but don't know how to change the key.
<script type="text/javascript">
jQuery(function($) {
let attachmentMaxSizeLimitKilobytes = ${sizeLimit};
let hasSpecificRole = ${hasSpecificRole};
$(function() {
if(hasSpecificRole){
//the key inside label should be "text:test:displayNormalText"
} else {
//the key inside label should be "text:test:differentText"
}
});
};
</script>
<test:main>
//boilerplate omitted
<label for="conditions"><fmt:message key="text:test:displayNormalText" bundle ="${bundle}/></label>
</test:main>
Any ideas how can I do that?
解决方案
You can't.
JS is evaluated on the client, JSP is evaluated on the server.
You would need to create something in JS that maps keys to strings, which could also be done in your JSP, e.g., render a JS object with key names to the rendered <fmt:message>
strings, keeping in mind they'd need to be JavaScript-escaped.
推荐阅读
- node.js - 如何在使用 GridFS 和 NodeJS 将图像存储到 MongoDB 之前对其进行压缩?
- typescript - 使用打字稿接口将数组从一种类型映射到另一种类型
- r - 如何找出哪个符号用作小数/列表分隔符?
- sql - 如何从具有特定前缀的表中获取数据?
- css - 通过 CSS 隐藏 Google recaptcha 徽章并显示法律文本?
- sql - 如何在 PL SQL 中以特定格式设置日期值?
- javascript - getStaticPaths 抛出“TypeError:segment.replace 不是函数”
- angular - Angular - 从承诺中获取价值
- tensorflow - 为什么在 GPU 中分配批量大小?
- mysql - 连接 4 个表 mySQL