0

In JSP, is there a way to set the class of the iteration variable of a c:forEach?

What I mean is, say I want to iterate over an array of com.acme.Item, but I wish my IDE to know the class, so it can provide me with code completion, error detection etc. In jsp:useBean I would do something like

<jsp:useBean id="myItem" class="com.acme.Item" />

and any subsequent code can be checked for correct property names, etc. But with <c:foreach> there is no class attribute. Is there another way to achieve this? Or is there some technical difficulty making this is impossible?

To further clarify, say my Item class has a name property (so it has getName, setName etc. )

I want that if I do something like:

<c:forEach var="item" items="${myItems}">
    ${item.nam}
</c:forEach>

that my IDE/Compiler will tell me I have an error (nam instead of name), but currently the only way I have is to run it on the server and catch the runtime-exception.

Itai
  • 6,023
  • 3
  • 22
  • 48
  • 1
    Are you open to using CDI (@Named/@Inject/etc) to manage beans instead of manually managing them? Recent IDEs have builtin support for @Named beans in EL. The jsp:useBean way is soo 00's. – BalusC Mar 02 '16 at 12:31
  • I'm just starting with JSP, so I really don't know! Could you point me to a resource about that? Preferably one about JavaEE, not Spring (I'm taking one step at a time, and would like to avoid Spring for the time being). – Itai Mar 02 '16 at 12:33
  • CDI is part of Java EE. Spring is competitor of Java EE and already doesn't make sense to be used on a real Java EE server (i.e. not Tomcat). Just to be sure, which IDE and server are you using? – BalusC Mar 02 '16 at 12:36
  • IntelliJ IDE, Tomcat 8 . – Itai Mar 02 '16 at 12:37
  • 1
    Recent IntelliJ for Java EE should have CDI support. Tomcat .. Well, this would be the next step: http://stackoverflow.com/q/18995951 – BalusC Mar 02 '16 at 12:38
  • Thank you. Just for completion's sake - I'm guessing the answer to the original question is that it's impossible? – Itai Mar 02 '16 at 12:41
  • Boils down to "impossible", yes. This is not exactly JSP's fault. It's just the tooling's own incapability. It's technically easier for the IDE to detect beans in EL if beans are defined/managed via a standard framework such as CDI, JSF, Spring, etc instead of manually, so the IDE could simply insect via its configuration (which follows a defined standard). – BalusC Mar 02 '16 at 12:42

0 Answers0