首页 > 解决方案 > 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?

标签: jqueryhtmljspfrontend

解决方案


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.


推荐阅读