Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Using JSTL inside Javascript

843840Jan 5 2009 — edited Jan 6 2009
Hi,

I am trying to dynamically create html code and insert it into a div tag in my page. The javascript I am using to create the html string is:

function(){
var str="";
str +='<c\:forEach var="statesDetail" items="${statesList}">';
str +='<c\:out value="${statesDetail}"><\/c:out>';
str +='<\/c:forEach>';

code to insert this HTML string to a div tag somewhere in the page-
}

The '\' in the above code is used to escape certain characters. The statesList is an arraylist set in the HttpServletRequest object as an attribute. I can verify that the statesList is not empty. It has 50 entries for all the US states. I loop this list and capture the information using the loop variable statesDetail. I am trying to print the value of statesDetail in the page, but cannot print anything.

If I paste the same code in the page as HTML instead of using javascript to dynamically create it, it works fine. So I am sure something is wrong with the code syntax above. Please help!!!

Comments

794117
Java != javascript.
They are two very different things.

Think of the JSP lifecycle.
JSP is invoked. It runs Java/JSTL, produces an HTML page.
Java stops running.
HTML page arrives at your client. Javascript runs.
The only way to run java/jstl again is to make a request: click a link, submit a form... and get a new response.

I can't exactly figure out what you're doing there with your mix of javascript and JSTL.

You can use JSTL to produce javascript code.
For instance
function myFunction(){
  var stateArray = new Array();
  <c:forEach var="state" items="${stateList}" varStatus="status">
    stateArray[${status.index}] = "${state}";
  </c:forEach>
}
When this run it executes the JSTL and would produce something like
function myFunction(){
  var stateArray = new Array();  
  stateArray[0] = "North Carolina";
  stateArray[1] = "SouthCarolina";
  stateArray[2] = "North Dakota";
  stateArray[3] = "South Dakota";
  ...
}
Does that make things clearer?
843840
Evnafets,

Thanks for the good explanation. I understand that javascript runs on the client side. As you said, the JSTL is converted into HTML on the server side and sent to the client. If you look at my function, I have some JSTL code inside the javascript string. I understand from your code that the JSTL functions you use inside the javascript will be converted into actual javascript code when the HTML page is rendered. I have one question though. Will this conversion take place when the HTML is initially created (OR) will it take place only when the function is invoked by the client. I am almost sure the first option is right, but I want to double check with you.

Thanks again!!
794117
The first option is right. It happens at the server end when the JSP is requested.
Basically if you "view source" on the generated page, thats what your javascript has to work with.
I have some JSTL code inside the javascript string
And thats where things where going wrong. You're mixing it badly.
var str="";
str +='<c:forEach var="statesDetail" items="${statesList}">';
str +='<c:out value="${statesDetail}"></c:out>';
str +='</c:forEach>';
The "escape" characters that you put in there stopped the tags being evaluated.
Nesting the <c:forEach> loop in a String is incorrect.
I can understand the <c:out> tag being in the string - that bits fine.
But not the <c:forEach>

Anything that is not JSP/JSTL tags is just template text and gets output as is.
843840
Great!...i get it now...thanks for the prompt response.
843840
Hi,

I tried pasting a similar code into a javascript function like this:

var stateArray = new Array();
<c:forEach var="state" items="${stateList}" varStatus="status">
stateArray[${status.index}] = "${state}";
</c:forEach>

But I get an error message saying that:

"Character data not allowed in element head". What cud be the reason for this?
843840
That's an XML error. You apparently declared your JSP as XHTML instead of HTML.

You need to wrap special chatacters in a CDATA block. If you know XML, you should know how.
1 - 6
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Feb 3 2009
Added on Jan 5 2009
6 comments
8,428 views