intellij not resolving el variables within JSP code inspection or autocomplete

There's a standard way to do this, which is not IntelliJ-specific.

<jsp:useBean id="someModel" scope="request" type="foo.bar.SomeModelClass"/>

The type attribute here does not need to be a concrete class, it can be an interface type as well. Typically you'd put these declarations at the start of your JSP/JSPX files, to provide something like a "declaration of model inputs".

Using JSPs in such a declarative way was recommended in the original book on Spring, in fact (Expert One-on-One J2EE Design and Development.). IntelliJ has been providing full code completion for such pages since at least 7 years.

Note that there are additional relevant convenience features in IntelliJ: if an EL variable reference is marked as undefined, you can press Alt-Enter to select a QuickFix, which will insert a declaration like above. It will even try to figure out the actual type, based on the properties you're accessing.


As I understand Spring, there is no declaration for definitions of variables that you may put into your model. The call model.addAttribute() may add an object to the model, either identified by a parameter or automatically generated by the class name of the object.

So imagine the following case where you have more than one method:

@RequestMapping("foo") public String foo(Model model) { 
    model.addAttribute("model", new Foo());
    return new Random().nextBoolean() ? "page" : "someOtherPage";
}
@RequestMapping("bar") public String bar(Model model) { 
    model.addAttribute("model", new Bar());
    model.addAttribute("model", new Foo());
    model.addAttribute("model", new Bar());
    return new Random().nextBoolean() ? "page" : "someOtherPage";
}

and the JSP would be something like

<c:out ${model.value} />

Since there is no proper mapping of which controllers may under some circumstances forward to which views, nor what exactly lies within the model, your IDE has no real chance to provide you with proper information.

But to support the IDE in suggesting you some useful information, you can use type hints. Therefore, you have to copy the whole reference of an object, e. g. foo and add a JSP comment like:

<%--@elvariable id="foo" type="com.mycompany.SomeObject"--%>

The warning will vanish and the full IDE support is on your side, allowing you to traverse the fields of foo. One of the nicest things is that the unused getter warnings will vanish, too. You can directly call the show usages action directly from the JSP or the POJO.

This also works with JSF and particularly within JSF components. Pretty neat feature to have this kind of code completion, showing warnings and errors.

Hope that helps you with your switch to Intellij Idea.

Edit: I also reported this finding to a friend wo wrapped the whole thing into a nice blog entry. Maybe you're interested in reading it: open link