JSP with Spring: Cannot resolve symbol 'command'
Answered
I know this must be a trivial issue, and I've ignored it for easily a year as it has no effect on the app itself, but the time has come to tackle it:
My controller display method declares its command object with:
public String display(@ModelAttribute("command") SchedulerJobCmd cmd,And ends with:
return viewName("schJob", request);schJob.jsp declares the form as:
<form:form id="schJobForm" name="schJobForm" method="post"
commandName="command" action="schJobSubmit">… and that is where the problem occurs.
- “command” shows in red in the editor with the error “Cannot resolve symbol ‘command’”.
- It executes fine, even in debug mode, but it causes a sea of annoying yellow warnings “Cannot resolve variable ‘command’”.
How do I make IntelliJ recognize the Spring command object?
NOTE: I have a few other symbols and variables that it says it cannot resolve… but work just fine. I am guessing that solving the above issue will solve these as well (except for any that are truly broken and I have yet to notice as so)
Please sign in to leave a comment.
Looks like a bug. Please report at https://youtrack.jetbrains.com/newIssue?project=IDEA and attach a small sample project to reproduce it.
I doubt it is a bug, it has always happened and this is so blatantly obvious that others would have reported by thousands of users.
I really believe that it is a matter of me failing to do something.
It would be easier to help if you could share the minimal reproducible example (zipped sample project).
Understood, will do, although it will be a while before I have time to create it.
UPDATE: While working on producing the simple example I figured out the cause, although not the fix:
For example,
If the JSP's form contains:
And the Controller returns the view with something like:
… it fails to understand the link the Controller (and its “command”) to the JSP, and triggers the error.
More specifically:
As I said earlier, this does NOT affect execution, but it triggers a warnings in the JSP if you then reference command in something like ${command.myProperty}. In a moderately complex page this ends up with a mess of fake warnings that completely obfuscate the valid ones, like seen in the screenshot below:
Let me know if you still want the sample module. Since you can reproduce the problem by simply adding a method that returns the string “home” of home.jsp in the Controller for any of your projects… it seems like overkill. But I'll attach it if you still want it.
Oh, and workaround: This will silence the error for “command” and any of its uses
We'd appreciate a bug report at https://youtrack.jetbrains.com/newIssue?project=IDEA with the small test case project to reproduce attached. Thank you.
In a similar/related topic, something like the following JSTL pattern - which is valid and executes fine - causes the “Cannot resolve symbol '${assetsPath}/css/bootstrap.min.css'” warning. It might be something they can probably fix when they are working on the other issue as they likely involve the same code. Mentioning it just as FYI, this one is annoying but nowhere as disruptive as the other bug.
The use of a (perfectly valid) variable instead of a straight constant string confuses the IDE in the <link> tag and makes it generate the warning.
Sure, no problem. The test case project includes both issues. Will submit the bug report after lunch.