Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JSTL and scriplets together doesn't work  RSS feed

 
Gerome Kawa
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a scenario where am trying out JSP scriplets and JSTL on the same JSP. The problem is when am trying to access a variable declared using scriplets through JSTL it does not work (i.e. does not access at all)

<%
boolean namesFound = false;
%>

<%
Collection namesCollection = (Collection) request.getAttribute(RequestConstants.NAMES_COLLECTION);

if ( ( null != namesCollection ) && ( namesCollection .size() > 0 ) )
{
// we have names
namesFound = true;
}
%>

Now. when am trying to display the value of namesFound it displaying blank

<c ut value="${namesFound }"> </c ut>


Any idea why it is so ?
Note: I have got all JSTL tag imports right
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your "namesFound" variable you declare in your scriptlet just becomes a local variable inside the _jspService() method. JSTL has no way to know about any variables inside the actual code unless they are added to a one of the JSP scopes (application, session, page or request).
 
Vivek Mongolu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this

<c ut value="${pageScope.namesFound}"> </cout>

I donno whether its gonna work. If it doesnt then as Nathan pointed out you have to set the varible to a scope
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I donno whether its gonna work.


It won't.

One of the goals of the JSTL and EL is to eliminate scriptlets and scriplet expressions from JSP pages -- so it makes sense that they don't mix well.

The JSTL and EL work with 'scoped variables'. If you are serious about bringing your JSP pages kicking and screaming into the modern era with JSTL and EL, you need to start thinking differently and not in terms of on-page scriptlets.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, let's assume that the value of RequestConstants.NAMES_COLLECTION is "namesCollection".

The EL-friendly version of the OP example would be:



But why create the boolean variable in the first place would be a question I would have. It's rather superfluous when the test is so easy to perform:

 
Gerome Kawa
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the responses Bear, Vivek and Nathan !
All of you are correct in your reasonings. What I was trying to figure out is what happens when we mix JSTL and scriplets !

Actually I got the following to work :

It sets the with
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!