需要帮助在JavaScript中使用EL从模型中获取价值 [英] Need help Using EL in javascript to get value from model

查看:85
本文介绍了需要帮助在JavaScript中使用EL从模型中获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在模型中存储了一个名为域"的对象,该对象具有两种方法,getDescriptionEn()和getDescriptionFr().

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}"

现在我想评估这种expressoin,但它似乎不起作用.

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

我尝试了许多不同的方式来解析字符串,

I have tried parsing the strings many different ways,

我也尝试过:

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

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

推荐答案

当您的javascript有机会在客户端上运行时,JSP已被评估(连同任何EL),您将无法这样访问模型.

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.

但是,您可以直接在JSP中完成所有您想做的事情.使用${pageContext.request.locale}访问客户端请求的语言环境,并将其传递给您的方法.

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.

这篇关于需要帮助在JavaScript中使用EL从模型中获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆