tags:

views:

44

answers:

2
public class Foo extends Properties {
    public String getVal(){
         return "2";
    }
}

In my HttpServlet class' doGet(..) method I am doing this,

Foo foo = new Foo();
request.setAttribute("key", foo);

Then in the .jsp is this code,

1  ${key}
2  ${key.val}
3  <%=request.getAttribute("key")%>
4  <%=((Foo)request.getAttribute("key")).getVal()%>

And this is the output,

1  {}
2  
3  {}
4  2

Could anyone tell my why ${key.val} doesn't work?

[Edit] I am only interested on the one property from the foo class, since it seems there is no way to access the getVar() call using EL(Right?), would a viable alternative be to jsut put?

request.setAttribute("key_val", foo.getVal());

Foo is a sub class of a subclass of the Properties class so there is no way for me to decouple them easily.

+2  A: 

Maybe you have EL disabled? Check these two answers for possible reasons and solutions:

Another possibility is that you have key bound to something different in your other contexts. This code:

${key}

Is equivalent to:

<%= page.findAttribute("key") %>

and not <%= request.getAttribute("key") %>. It searches for in pageContext first, then request, session and application context at last.

Peter Štibraný
But ${key} does get resolved, so I know it is enabled.
Andrew
What would be the correct syntax for getting foo from the request object then?
Andrew
If you only want to use object from request scope, you need to use `${request.key}` or `${request['key']}`. But typically, `${key}` is what you want. I just wrote that answer as a possibility.
Peter Štibraný
Both ${request.key} and ${request['key']} return nothing when I use them. When you say ${key} is what I want, do you mean that my syntax should be correct?
Andrew
@Andrew: yes, your syntax looks OK. I'm sorry, it should have been ${requestScope.key} or ${requestScope["key"]}.
Peter Štibraný
+1  A: 

Your Foo class has a toString() method which prints like {} and the val probably called the wrong getter. Add some debug lines/breakpoints to the getter calls. If in vain, post more detail of how the Foo class look like.

BalusC
This would indeed explain the behaviour.
Peter Štibraný
If Foo implements Map, ${key.val} could be translated to Foo.get("val") too. Alternatively, JavaBeans info may define different method for "val" property, although I've never seen that in practice.
Peter Štibraný
I overrode the toString() method and added a break point to the getter, The toString() method is indeed what produces the "{}", however the getVal method is only called for the call on line 4.
Andrew
Is it a `public` method? Really, we need more detail about the `Foo` class.
BalusC
It extends Properties, but the getVal() method was implemented by me, it is public, and it is able to return the right value, you can see that in line 4 of my example.
Andrew
`Properties` in turn implements `Map`. Its `get()` method is preferred over this in EL. That's why you're seeing this behaviour. I'd suggest to have `Foo` compose instead of inherit it.
BalusC