簡體   English   中英

如何通過JavaScript處理JSTL標簽

[英]How to proceed JSTL tags via javascript

我必須在javascript response-handler函數中進行JSTL標記(fmt :)。 例如:

  1. 我需要通過AJAX切換語言,所以我需要通過javascript更新工具面板,並在響應處理函數(javascript)中進行fmt:setLocale
  2. 我必須通過ajax更新我的選擇下拉菜單,並且它們必須翻譯適當的本地化,所以我必須在javascript中使用fmt:message

但是 ,jsp引擎無法處理.js文件,並且我的代碼無法正常工作:

document.findElementById("div1").innerHTML = "<fmt:message key="word_from_DB"/>";
document.findElementById("div2").innerHTML = "<fmt:setLocale value="en"/>";

問題 :如何從javascript繼續處理這些標記,或者在哪里搜索替代標記?

如何准確實現所需的目標取決於JSP和JS文件的內容,但這是一種潛在的解決方案:

JS:

function setDivInnerHTML(html1, html2) {
    document.findElementById("div1").innerHTML = html1;
    document.findElementById("div2").innerHTML = html2;
}

JSP:

...
<script>
setDivInnerHTML("<fmt:message key="word_from_DB"/>", "<fmt:setLocale value="en"/>");
</script>
...

根據評論進行編輯

JSP(URL =“ / cities”):

String country = request.getParameter("country");
String locale = request.getParameter("locale");
List<City> cities = getCitiesFromDB(country, locale);
out.println(objectToJSON(cities));

JS(使用jQuery):

function createOption(val, html) {
    return $("<option></option>").html(html).val(val);
}

function populateCities(cities) {
    var $select = $("#city_select");
    $select.empty();
    for (var i = 0; i < cities.length; i++) {
        var $opt = createOption(cities[i].name, cities[i].name);
        $select.append($opt);
    }
}

function getCities(country, locale) {
    var url = "/cities";
    return $.ajax(url, {
        data: {
            country: country,
            locale: locale
        }
    });
}

// call this to load the initial cities
// and do the same thing when a new country or locale is selected
getCities(country, locale).then(populateCities);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM