JSP with Spring: Cannot resolve symbol 'command'

已回答

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)

 

0

Looks like a bug. Please report at https://youtrack.jetbrains.com/newIssue?project=IDEA and attach a small sample project to reproduce it.

0

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.

0

It would be easier to help if you could share the minimal reproducible example (zipped sample project).

0

Understood, will do, although it will be a while before I have time to create it.

0

UPDATE:   While working on producing the simple example I figured out the cause, although not the fix:   

  • When IntelliJ tries to validate the commandName, it relies on the name of the view for “something”.
  • But… if the view name is generated by a method instead of being a constant, it chokes and gives the error “Cannot resolve symbol 'command'” on commandName="command", even though there is no error and the application works perfectly.

For example, 

If the JSP's form contains:

    <form:form id="x4Form" name="x4Form" method="post"
      commandName="command" action="searchSubmit">

 

And the Controller returns the view with something like:

    String myView = viewName("home", request);
    return myView;
  }

… it fails to understand the link the Controller (and its “command”) to the JSP, and triggers the error.

More specifically:

  • With ‘  return “home”;  ’  it does not flag commandName="command" as an error.
  • With ‘  return myView;  ’ it triggers the IDE error.

 

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.

 

 

0

Oh, and workaround:  This will silence the error for “command” and any of its uses

<%--@elvariable id="command" type="command"--%>

 

0

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.

0

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.

<c:set var="assetsPath"   value="https://${pageContext.request.serverName}/web-assets" />

  <link href="${assetsPath}/css/bootstrap.min.css" rel="stylesheet" type="text/css">

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.

0

Sure, no problem.  The test case project includes both issues.  Will submit the bug report after lunch.

0

请先登录再写评论。