aspose file tools*
The moose likes JSP and the fly likes Using a Java constant in EL? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Using a Java constant in EL?" Watch "Using a Java constant in EL?" New topic
Author

Using a Java constant in EL?

D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
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

Joined: Sep 07, 2007
Posts: 83
what is the error message you are getting?


SCJP 1.5, SCWCD 1.4.
Hanging between Web Services and EJB
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214


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

Joined: Jun 25, 2001
Posts: 16305
    
  21

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1871
    
  16

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?


No more Blub for me, thank you, Vicar.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

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

Joined: Mar 01, 2009
Posts: 1871
    
  16

@Tim - thanks for the helpful explanation!
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
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

Joined: Jan 10, 2002
Posts: 61761
    
  67

I wrote this article on this exact subject.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
Thanks, that looks awesome!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using a Java constant in EL?