File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to pass Java Script variable to JSTL

 
Kiran Gaddam
Ranch Hand
Posts: 35
1
  • 0
  • Mark post as helpful
  • send pies
  • 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...
 
Stefan Evans
Bartender
Posts: 1640
9
  • 1
  • Mark post as helpful
  • send pies
  • 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?

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for you Reply Bibeault, Evans..

But still I am struggling with the requirement. Any more ideas are welcome...
 
Paul Clapham
Sheriff
Pie
Posts: 20161
23
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 20161
23
MySQL Database
  • 1
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic