0

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 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I need to access domain.getDescriptionEn or domain.getDescriptionFr according to the locale, but I NEED to access from the JSP, from within a javascript function.
But you realize that your JSP code runs in the server, before being sent to the client and that the javascript is executed by the client, after it has received the response (with all of the JSP code already executed) by the server. If you need a var in JS with the description, you can to do something like: var description = <%= domain.getDescription(currentLocale) %>;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.