Need help Using EL in javascript to get value from model

荒凉一梦 提交于 2020-01-07 03:58:08

问题


I have a an object stored in the model called "domain", which has two methods, getDescriptionEn() and getDescriptionFr().

I need the description depending on the current locale.

Here is my problem, I have the following:

var locale = "${currentLocale}"; // This returns either "En" or "Fr"

var method = "{domain.getDescription".concat(locale).concat("}"); // This is "{domain.getDescriptionEn}" or "{domain.getDescriptionFr}"

var expr = "$".concat(method); // This is going to be "${domain.getDescriptionFr}"

now I would like to evaluate this expressoin but it does not seem to work.

I have tried parsing the strings many different ways,

I also tried:

var method = "domain.getDescription".concat(locale); // This is "domain.getDescriptionEn" or "domain.getDescriptionFr"

var expr = "${".concat(method).concat("}");

回答1:


By the time your javascript gets a chance to run on the client the JSP has already been evaluated (along with any EL) and you won't be able to access model that way.

However, you can do all that you want directly in the JSP. Use ${pageContext.request.locale} to access the locale of the client's request and pass it on to your method.



来源:https://stackoverflow.com/questions/9843401/need-help-using-el-in-javascript-to-get-value-from-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!