• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using a Java constant in EL?

 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to access java constants in an EL expression? I want to access a data bean in the session context. In my servlet I store the bean as session attribute "myitems" and use the constant KEY (defined in class Constants) to avoid hardcoding the name in the servlet. In the jsp, using the old way I could then do something like



where Constants.KEY has the value "myitems". But this doesn't seem to work with EL:



This produces a jsp compilation error. Is there a way to use the value from the Constants file instead of hardcoding the id of the bean?
 
Ramkumar Subburaj
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the error message you are getting?
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


So apparently this construct isn't allowed here. Of course I can just type id="myitems" (or whatever the actual value of MESSAGES_KEY is) but I'd rather use the same constant in my servlet and jsps.
 
Tim Holloway
Saloon Keeper
Posts: 18300
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this particular case (id attribute), no, I don't think you can use EL. It's partly because "id=" has a mystical significance in XML to begin with, but also because JSP tag constructs are defined as either static (constant) or dynamic (EL). And I'd expect id to be static, since, like I said, it has certain restrictions.
 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new to JSP etc, but it looks like part of the problem is that you can't use EL to access static (class) variables directly. For example, in my JSP:

In the HeadFirst book (my sole source of JSP wisdom right now!), it says the thing on the left of the EL dot operator, i.e. Constants in this case, must be a bean or a Map. If you're using Constants just to provide a bunch of static (class) variables without instantiating it, then I guess you don't have a Bean/Map instance so EL doesn't like it.

Seeing as how these are constants, you could define them as parameters in your XML instead, then let EL access them via the implicit "initParam" Map instead. For example, in web.xml:

In the JSP you can then access the value of the parameter with EL:

But as Tim says, it looks like you still can't put this into your <jsp:useBean> "id" attribute.

As a JSP newbie, can I ask if it is common to mix beans into JSP views like this? My limited understanding of MVC would cause me to instantiate the bean somewhere else e.g. in a servlet, then pass it into the JSP as an attribute, rather than creating it in the JSP view itself. But what do I know, right?
 
Tim Holloway
Saloon Keeper
Posts: 18300
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chris webster wrote:

As a JSP newbie, can I ask if it is common to mix beans into JSP views like this? My limited understanding of MVC would cause me to instantiate the bean somewhere else e.g. in a servlet, then pass it into the JSP as an attribute, rather than creating it in the JSP view itself. But what do I know, right?


it's common, alas, but it doesn't scale well. Which is why major JEE projects are usually done with a MVC framework like Struts or JSF. The useBean construct doesn't actually result in a MVC-like architecture - the model, view, and controller are all basically the same object (the JSP with an embedded bean). The results can get really ugly on complex projects. I had to rewrite one that did that, and it's virtually unmaintainable in its original form. One JSP "page" printed out as its source file ran 42 pages, half being HTML and half being embedded Java code.

EL is based on the standard JavaBean concept of a class instance with accessor (get/set) methods. So yes, there's a problem with accessing a static property in EL. You can get around that one by making member accessors that refer to the class property, however.

The restriction against EL for the id attribute, however, is another matter, and I don't think there's a way around that.
 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Tim - thanks for the helpful explanation!
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks for the answers!

chris webster wrote:As a JSP newbie, can I ask if it is common to mix beans into JSP views like this? My limited understanding of MVC would cause me to instantiate the bean somewhere else e.g. in a servlet, then pass it into the JSP as an attribute, rather than creating it in the JSP view itself. But what do I know, right?


Actually, jsp:useBean doesn't instantiate the bean if you use the type attribute. It only tells the JSP to look in the given context for an (existing) bean of that type with the given id. My ide (IntelliJ) tells me when there are undeclared variables or references to beans in the JSP (for example in EL expressions), and with jsp:useBean they become known. Maybe there is another way to "declare" objects used in EL expressions?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote this article on this exact subject.
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that looks awesome!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic