Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

accessing values of context attribute in jquery

 
stephen r davidson
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code in MyContextListener.java

String []lookupAccountRegion = {"one","two","three"};
servletContext.setAttribute("case.account.region__c",lookupAccountRegion);

and am trying to use the variable as the SOURCE in an autocomplete statement in my jsp/javascript.
Just for testing, I try to access the context attribute like this:

var lookupValues2 = $("case.account.region__c");

and my javascript terminates here.

If I remove my attempted reference to the context attribute, and add a variable in the javascript like this:
var lookupValues = ["this", "that", "other"];

then I can use lookupValues in my source statement and autocomplete works.

So it doesn't work with the array I am passing in....

I know this is likely some woodenhead moment on my part, but wonder if you can suggest a path forward?



 
J. Kevin Robbins
Bartender
Posts: 1759
19
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those context attributes are on the server. Javascript runs on the client and has no concept of application context.

You have a couple of options. Make an Ajax call to a servlet that will retrieve those attributes and send them to the client. Or store them in the jsp as hidden fields and then the Javascript can retrieve them when the html reaches the client.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless they need to be submitted as part of a form later, I wouldn't use hidden fields. Just create a JavaScript variable that contains the value.

E.g.
 
stephen r davidson
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm. Bear. Are you saying that I CAN access my context attribute with a javascript variable?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. I'm saying that you can create the markup for a JavaScript variable in the JSP.

Remember JSP is processed on the server. It will make the substitution, and what will be sent to the browser would be:

There is no access from JavaScript. None. Nada. Nil. It's just text substitution.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic