how to debug JSF/EL
Asked Answered
J

2

52

How to debug EL in the JSF page? I'd like to watch variable values, function calls an so on. The best solution would be an eclipse plugin, but any other possibility is better than guessing "Why this expression failed to render correctly?".

Jeffryjeffy answered 25/11, 2011 at 9:23 Comment(0)
G
76

Closest what you can get in JSF/Facelets is placing an <ui:debug /> somewhere in the view:

<ui:debug />

Pressing CtrlShiftD should then show a popup window with debug information about the component tree and all available request parameters and request/view/flash/session/application scoped variables. It's basically a representation of the content of all those maps.

The hotkey is by the way configureable by hotkey attribute so that you can choose another whenever it clashes with browser default hotkeys, as it would do in Firefox; CtrlShiftD would by default show the Add bookmarks dialogue. Here's how you could make it to listen on CtrlShiftX instead:

<ui:debug hotkey="x" />

You'd usually also like to hide it in non-development stage, so add a rendered condition like that:

<ui:debug hotkey="x" rendered="#{facesContext.application.projectStage == 'Development'}" />

In the shown debug information, the information provided about scoped variables isn't that great as you would expect. It only shows the Object#toString() outcome of all scoped variables which defaults to com.example.Bean@hashcode. You can't explore their properties and the values of their properties directly like as you could do in debug view of Eclipse's debugger. You'd need to implement toString() on the class accordingly so that as much as possible relevant information is returned (if necessary, you can even let Eclipse autogenerate it by rightclick source code > Source > Generate toString()):

@Override
public String toString() {
    return String.format("Bean[prop1=%s,prop2=%s,prop3=%s]", prop1, prop2, prop3);
}

As to method calls, just put a breakpoint on the Java source code the usual way. Eclipse will kick in there as well when EL calls the method. If it's a managed bean, you'll also just see its properties in the Eclipse debugger.

Goatish answered 25/11, 2011 at 13:4 Comment(5)
Finally I use normal eclipse debugger end ui:debug.Jeffryjeffy
+1 for getting <ui:debug> to work (for a rather sad definition of work)Slump
Hi BalusC, I was thinking of asking a similar question but it seems better to just post it here... I followed your Debug JSF lifecycle article on your blog. Other than the debugging mechanisms you suggest here and on that article, is it possible to dive into JSF implementation source? I have a problem on my view with the tags. The rendering isn't as expected and I can give no meaning as to why it is the way it is. I was thinking of diving into JSF source. Would it make sense and if it would, would you be able to point me to a resource to get started?Orbiculate
@Murat: start with a breakpoint on encodeAll() method of the desired UIComponent class.Goatish
Hi BalusC, I have tried the steps you provided here but it is not working.I put <html><ui:debug hotkey="x" rendered="#{facesContext.application.projectStage == 'Development'}" /><h:body>code</h:body></ui:debug></html>. am I doing anything wrong?Seriate
B
8

If you are really having problems then if you can get the source for the EL implementation (easy enough for the RI) then you can use Eclipse to set breakpoints in the EL implementation methods. You need to get an understanding of how the EL code works, but it isn't that complicated. Not for the very faint hearted though.

Another possibility would be to create and evaluate the EL programatically. There are examples of how to do this around. You can then use the debugger to fiddle around with what the expression is and what the result is until you've worked out where your problem lies.

Buckwheat answered 26/11, 2011 at 3:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.