I’m programming an application in Struts and I need to highlight some reminders in a jquery calendar. I have the following question:
In my action watchCalendar.java (which forwards to the jsp im working on called sideCalendar.jsp), I set an attribute on the request scope, namely:
request.setAttribute("reminders",queryResults);
I would like to access this attribute in the request scope from java script (this is my question). Actually I want to access this array of reminders and obtain the startDate and endDate of each reminder, so that I can then highlight the range within them on my calendar. This way, the user will know how many days he has on his calendar to fulfill the request in his reminder.
I tried using:
var arrayOfReminders = document.getElementById("reminders");
So that I could copy the array of reminders in the request scope, to the javascript area of my view component.
Then I printed it on screen using:
document.write(reminders);
Yet it only prints “null” on the view.
I have searched a lot on the internet, and have not found any further information beyond using the getElementById() function.
I’ll keep investigating, but I would really appreciate some guidance.
Thank you very much for reading!!
P.D: I can obtain the array of reminders from the database, and I set it on the action with no problems. I think my main problem is how to access this attribute from javascript in view.
The request scope that you’re setting in Struts is server-side only, so Javascript has no way to obtain it. One approach around that is to render something in your JSP. For example:
Now you can access
remindersvariable anywhere in the page from Javascript. For example:A better approach would be to set the request attribute from the server (Struts) side to JSON format first:
Then, render it into your JSP and assign it directly to a variable: