• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to pass Java Script variable to JSTL

 
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all..

is their any way that I can assign JavaScript variable to JSTL or use in JSP.

My Requirement is,
I should greet user according to time... For that I should get user machine time(not server time)... for that I should use JavaScript.

Every thing is fine,,, But the catch is I am using spring Internalization for messages.

So I should get the client machine time and greet using spring spring internalization.

Any ideas please...
 
Bartender
Posts: 1845
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, there is no direct way to assign javascript variable to JSTL/JSP.

Java runs on the server, produces an HTML page.
Javascript runs on the client.

The only way to pass information from client to server is to make an HTTP request - which is the basis of ajax.

My suggestion for a workaround would be to do your i18n in Spring, providing the message "The time is currently [insertTimeHere]" or "It is [insertTimeHere] where you are right now"
That message can get output onto the html page in a javascript variable.
You then get the current time from the client, and use string functions to replace [insertTimeHere] with the time from the client.

So you will be able to localize the message part of "The time is currently..." with Spring but not the actual time string (got from the client)
Does that suffice?

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read this article to understand how JSP operates and why that is impossible.
 
Kiran Gaddam
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for you Reply Bibeault, Evans..

But still I am struggling with the requirement. Any more ideas are welcome...
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if you read and understood the article it would be clear to you that it's impossible for Javascript to affect processing which was done at an earlier time on a different computer. But that doesn't necessarily mean that you can't fulfil your requirement; it just means you can't do it the way you originally thought. So put that idea out of your mind and consider the actual situation.

You have an HTML page in a browser. And you want to take the time from the computer where the browser is running, and have Spring format that time according to the user's I18N rules. So how to do that? Here's what you want to do, approximately:

(1) Get the time from the browser using the well-known Javascript function.
(2) Send it to Spring to be formatted according to the user's rules.
(3) Receive the formatted version and display it in the right place in the HTML.

Does that suggest something which you can implement?
 
Kiran Gaddam
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok...got it... I will make AJAX call to spring controller..

first I need to send time from javascript to spring controller through an AJAX call
take that value and process it in java.
send it back to JSP

Will put the code once I am done..
may help some others

Thanks for your time and response guys
 
Kiran Gaddam
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found an other way around for the issue...

it is ...




just dont forget to include jquery library...

Thats it...
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's even better than using AJAX.

(When I was travelling in South America I noticed that people didn't start saying "Buenas tardes" -- Good afternoon -- until at least 2 o'clock in the afternoon. And they didn't say "Buenas noches" -- Good evening -- until at least 8 PM. Does your Spring internationalization take that sort of cultural factor into account?)
 
Kiran Gaddam
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats a good point Paul..

In this case I was told to use these timings..
and we are not taking cultural factors into account.

Again, that is an interesting point you made
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic